Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is more accurate? java.lang.Math.E or Math.exp(1.0)

Tags:

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.

like image 920
user1663569 Avatar asked Sep 11 '12 17:09

user1663569


People also ask

What does math E mean in Java?

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)

Is Java Lang math a class?

The java. lang. Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

How many classes are listed in the Java Math package?

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.


2 Answers

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.

like image 128
Wooble Avatar answered Feb 02 '23 16:02

Wooble


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.

like image 42
Green Avatar answered Feb 02 '23 16:02

Green