Code [GCC, compiled with -O2 flag]
int main()
{
vector< vector<int> > matrixa(8);
int ff = 5;
int s = pow(ff, matrixa.size());
int ss = pow(double(ff), int(matrixa.size()));
vector< vector<int> > comb(s);
cout << ff << "^" << matrixa.size() << " = " << s << endl;
cout << ss << endl;
return 0;
}
Output
5^8 = 390624
390625
I’m wondering why s = 390624 when it should be 390625. If I compile the code without -O2 flag, then s = 390625. Also, the casting for ss seems to correct the problem.
What’s going on?
My OS is Windows 7 Ultimate x64. Not sure about the GCC version, it comes with Code::Blocks 10.05.
Because floating-point arithmetic is not perfect and when you do
the result of
powis actually something more like390624.99999and when you truncate it tointit effectively is flattened down to 390624. If you expect an integer value there (a floating-point with .0 fractional part), you should probably round the result ofpow.