w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
pow() giving wrong result

pow (see reference) is not defined for integers, but only for floating point numbers. If you call pow with int as an argument the result will be a double.

You can in general not assume that the result of pow will be exactly the same as if you would use pure integer math as in the function pow_longlong.

Citation from wikipedia about double precision floating point numbers:

Between 2^52=4,503,599,627,370,496 and 2^53=9,007,199,254,740,992 the representable numbers are exactly the integers. For the next range, from 2^53 to 2^54, everything is multiplied by 2, so the representable numbers are the even ones, etc.

So you get inaccurate results with pow if the result would be bigger than 2^53.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.