This is not a problem of accuracy. The Math.pow method approximates the result. To get the correct result, use the following code.
long b = 13; for(int i = 0; i != 14; i ++) { b = b * 13; } System.out.println(b);
The result is the expected result 51185893014090757L.
More generally, the use of the Math.pow method should be avoided when the exponent is an integer. Firstly, the result is approximate, and secondly, it is more expensive to calculate.
The implementation of Math.pow (and most of the other methods in the Math class) is based on the netlib network library as the package "Freely Distributable Mathematical Library" (see StrictMath javadoc ). A C implementation is available at e_pow.c .
Pierre-Nicolas Mougel Feb 18 '14 at 11:51 2014-02-18 11:51
source share