Search code examples
javascriptjavamathexp

Math.exp in Java calculates incorrectly?


In Javascript, I tried Math.exp(-1800/2000) and the result is 0.4065696597405991. However, in Java, the same function returns 1.0? I read the documentations and Math.exp in Java and Javascript basically do the same thing.

In Java, Math.exp(a) returns:

the value e^a, where e is the base of the natural logarithms. //in Java

In Javascript, Math.exp(x) returns:

A number representing e^x, where e is Euler's number and x is the argument.

Maybe it's just a beginner's mistake.


Solution

  • In Java, -1800/2000 results in an integer (0). You need to cast to a suitable type (double).

    Try:

    Math.exp(-1800.0/2000.0)
    

    Or in your case:

    multiplier1 = Math.exp((double)-frequency / 1300.0) * 1700.0;