Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's difference between Math.pow(9, 18) and 9^18

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 ?

like image 431
JavaUser Avatar asked Jun 02 '11 05:06

JavaUser


People also ask

What does POW mean in math?

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.

Is math POW a double?

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.

What is the value of math POW 3 2?

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 .

How is built-in function pow () function different from function math pow ()?

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.


1 Answers

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
like image 168
Josh Lee Avatar answered Oct 06 '22 00:10

Josh Lee