Skip to content

Commit

Permalink
Fix inappropriate approximation of Math.exp
Browse files Browse the repository at this point in the history
FastMath.exp was neither continuous nor strictly monotonically increasing for x < -1 and therefore inappropriate for the intended purpose.
  • Loading branch information
quaelnix committed Jan 27, 2023
1 parent 2387513 commit 56b83b6
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions brouter-util/src/main/java/btools/util/FastMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
*/
public class FastMath {
/**
* Approximation to Math.exp for small negative arguments
* Approximation to Math.exp
*/
public static double exp(double e) {
double x = e;
double f = 1.;
while (e < -1.) {
e += 1.;
f *= 0.367879;
}
return f * (1. + x * (1. + x * (0.5 + x * (0.166667 + 0.0416667 * x))));
e = 1 + e / 64;
e *= e; e *= e;
e *= e; e *= e;
e *= e; e *= e;
return e;
}
}

0 comments on commit 56b83b6

Please sign in to comment.