Skip to content

Instantly share code, notes, and snippets.

@mratkovic
Last active October 28, 2015 21:30
Show Gist options
  • Save mratkovic/b273376496bb283c9dec to your computer and use it in GitHub Desktop.
Save mratkovic/b273376496bb283c9dec to your computer and use it in GitHub Desktop.
Approximation of exponential function and pow function in Java
/**
* Approximation of the Exponential Function
*
* http://martin.ankerl.com/2007/02/11/optimized-exponential-functions-for-java/
* http://nic.schraudolph.org/pubs/Schraudolph99.pdf
*/
private static double exp(final double val) {
long tmp = (long) (1512775 * val + (1072693248 - 60801));
return Double.longBitsToDouble(tmp << 32);
}
/**
* Approximation of pow() function
*
* http://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java-and-c-c/
*/
public static double pow1(final double a, final double b) {
final int x = (int) (Double.doubleToLongBits(a) << 32);
final int y = (int) (b * (x - 1072632447) + 1072632447);
return Double.longBitsToDouble(((long) y) << 32);
}
public static double pow2(final double a, final double b) {
final long tmp = Double.doubleToLongBits(a);
final long tmp2 = (long)(b * (tmp - 4606921280493453312L)) + 4606921280493453312L;
return Double.longBitsToDouble(tmp2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment