Reading the Javadocs, I see that Math.E is "The double value that is closer than any other to e, the base of the natural logarithms.". The printed value for Math.E is 2.718281828459045 while the value of Math.exp(1.0), which should be the same value is: 2.7182818284590455 (one more 5 at the end).
From the docs, it sounds like the bits in Math.E have been "manually adjusted" to get closer to the actual value of e than the calculation produced by Math.exp(1.0). Is this correct, or am I reading the docs incorrectly?
If that is correct, then is using Math.pow(Math.E, n) more accurate than Math.exp(n), or less? I've Googled and search SO, but can't find anything on this particular issue.
The Java Math exp() method returns the Euler's number e raised to the power of the specified value. That is, Math. exp(4.0) = e4.0 . The syntax of the exp() method is: Math.exp(double a)
The java. lang. Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
The package java. math is new as of Java 1.1. It contains two classes that support arithmetic on arbitrarily large integers and floating-point numbers.
The actual value to 16 decimal places is 2.7182818284590452; 2 is closer to 0 than to 5, so the constant is closer.
Note that when doing floating point calculations with either number it's quite likely the error in the floating point representation of your answer will make which one you use largely irrelevant.
Math.E
2.718281828459045
Math.exp(1.0)
2.7182818284590455
So this is the value from Wikipedia, 2.7182818284590452 The only difference I can see is a rounding error on the last digit of the Math.exp(1.0) where the value is 5 instead of 2. So strictly speaking Math.E is more accurate but unless you're doing some really crazy stuff, it won't matter for precision.
There may be speed considerations for using the precalculated Math.E instead of Math.exp(1.0). You might want to check that out too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With