The C++ program below should return a stricly positive value. However, it returns 0.
What happens ? I suspect an int-double conversion, but I can't figure out why and how.
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<double> coordinates;
coordinates.push_back(0.5);
coordinates.push_back(0.5);
coordinates.push_back(0.5);
cout<<inner_product(coordinates.begin(), coordinates.end(), coordinates.begin(), 0)<<endl;
return 0;
}
This is because you provided zero as an integer constant. The resultant operations are all in integers, so the final value (0.75) is truncated to an int as well.
Change zero to 0.0 to make it work:
cout << inner_product(coord.begin(), coord.end(),coord.begin(), 0.0) << endl;
This produces 0.75 on ideone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With