I am confused about using expm1 function in java
The Oracle java doc for Math.expm1 says:
Returns exp(x) -1. Note that for values of x near 0, the exact sum of
expm1(x) + 1 is much closer to the true result of ex than exp(x).
but this page says:
However, for negative values of x, roughly -4 and lower, the algorithm
used to calculate Math.exp() is relatively ill-behaved and subject to
round-off error. It’s more accurate to calculate ex – 1 with a
different algorithm and then add 1 to the final result.
should we use expm1(x) for negative x values or near 0 values?
The implementation of
doubleat the bit level means that you can storedoubles near 0 with much more precision thandoubles near 1. That’s whyexpm1can give you much more accuracy for near-zero powers thanexpcan, becausedoubledoesn’t have enough precision to store very accurate numbers very close to1.I don’t believe the article you’re citing is correct, as far as the accuracy of
Math.expgoes (modulo the limitations ofdouble). TheMath.expspecification guarantees that the result is within 1 ulp of the exact value, which means — to oversimplify a bit — a relative error of at most 2^-52, ish.