when I use Math.pow(9, 18) =150094635296999136
when I use web Calculator 9^18 = 150094635296999121 (http://web2.0calc.com/)
when I use Google calculator 9^18 = 1.50094635 × 10^17
why it is different ?
pow() is used to calculate a number raise to the power of some other number. This function accepts two parameters and returns the value of first parameter raised to the second parameter.
pow() is used to return the value of first argument raised to the power of the second argument. The return type of pow() method is double.
The expression math:pow(2, 3) returns 8.0e0 . The expression math:pow(-2, 3) returns -8.0e0 . The expression math:pow(2, -3) returns 0.125e0 . The expression math:pow(-2, -3) returns -0.125e0 .
The built-in pow(x,y [,z]) function has an optional third parameter as modulo to compute x raised to the power of y and optionally do a modulo (% z) on the result. It is same as the mathematical operation: (x ^ y) % z. Whereas, math. pow() function does not have modulo functionality.
At that range, the difference between successive double
values is 32. 150094635296999121 is the correct answer as an integer, but that number cannot be exactly represented as a double
.
You can use BigInteger
to get the exact answer:
Math.pow(9, 18) == 150094635296999136
BigInteger.valueOf(9).pow(18) == 150094635296999121
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