The pow function in CPP gives output 1350851717672992000 for input pow(3, 38), but the correct output is 1350851717672992080 i.e. output differs by 89. So what is the issue?
Didn't find any solution on the net!! c++ version 7.4.0
pow(3, 38) ==> Correct output is 1350851717672992089, but the function gives output 1350851717672992000 which differs by 89.
Edit- Using the powl function worked! Thank you, everyone!!!
You realize that pow returns double or long double, and those types have limited precision?
Do like this
OR
Use powl. More here
But I don't think any contests problems ask us to print these huge answers generally they will be modulus some prime number.
powl
is still along double
function. Never use it to exp whole numbers.Ok. What about the first approach? Do we get any problem in the contest that ask us to calculate these huge numbers?
Exponentation in loop is nice before big numbers come. It evaluates a^n in O(n), and binpow doing it in O(log n).
Use binary exponentation.
pow
is function, that works withdouble
s, that have limited precision. So even if you call pow of twoint
orlong
arguments, it will evaluate it asdouble
s, and then round toint
(ll
). Same for squares. Never usepow(x,2)
, usex*x
instead