This is a really nasty little problem which has three(!) causes.
Firstly there is a problem that floating point arithmetic is approximate. If the compiler picks a pow
function returning float or double, then 4**31 is so large that 5 is less than 1ULP (unit of least precision), so adding it will do nothing (in other words, 4.0**31+5 == 4.0**31). Multiplying by -2 can be done without loss, and the result can be stored in a long long
without loss as the wrong answer: -9.223.372.036.854.775.808.
Secondly, a standard header may include other standard headers, but is not required to. Evidently, Visual Studio's version of <iostream>
includes <math.h>
(which declares pow
in the global namespace), but Code::Blocks' version doesn't.
Thirdly, the OP's pow
function is not selected because he passes arguments 4
, and 31
, which are both of type int
, and the declared function has arguments of type unsigned
. Since C++11, there are lots of overloads (or a function template) of std::pow
. These all return float
or double
(unless one of the arguments is of type long double
- which doesn't apply here).
Thus an overload of std::pow
will be a better match ... with a double return values, and we get floating point rounding.
Moral of the story: Don't write functions with the same name as standard library functions, unless you really know what you are doing!