When to use expm1 instead of exp in java

I am confused about using expm1 function in java. Oracle java doc for Math.expm1 says:

Returns exp (x) -1. Note that for values ​​of x near 0, the exact sum expm1 (x) + 1 is much closer to the true result ex than exp (x).

but this page says:

However, for negative x values, approximately -4 or lower, the algorithm used to calculate Math.exp () behaves relatively poorly and obeys the rounding error. More precisely calculate ex-1 using a different algorithm, and then add 1 to the final result.

should expm1 (x) be used for negative x values ​​or around 0 values?

+6
source share
2 answers

Implementing double at the bit level means that you can store double about 0 with much greater accuracy than double about 1. That expm1 can give you much more accuracy for near-zero powers than exp can, because double does not have enough accuracy for storing very accurate numbers very close to 1 .

I do not believe that the article you are quoting is true as far as the accuracy of Math.exp goes (modulo double constraints). The Math.exp specification guarantees that the result is within 1 ulp of the exact value, which means - to simplify the bit - the relative error is not more than 2 ^ -52, ish.

+9
source

You are using expm1(x) for anything close to 0. Positive or negative.

The reason is that exp(x) everything that is close to 0 will be very close to 1. Therefore, exp(x) - 1 will suffer from destructive cancellation if x close to 0.

expm1(x) correctly optimized to avoid this devastating failure.


On the mathematical side: if exp is implemented using its Taylor series, then expm1(x) can be done simply by omitting the first +1 .

+5
source

Source: https://habr.com/ru/post/923145/


All Articles