Search code examples
javaarbitrary-precisionapfloat

ApfloatMath.pow method returns almost the correct value


Here is a sample code:

ApfloatMath.pow(new Apfloat(25, 5), new Apfloat(0.5, 5));

The above code performs the square root of 25.
This returns 4.9999.
I would've thought that this being arbitrary would return the correct value.


Solution

  • From the Javadoc:

    pow

    Arbitrary power. Calculated using log() and exp().

    In other words, this does something like exp(log(x) * y). However, log(25) is irrational, and thus has an infinite number of digits. Thus it has to be approximated at some point.