Last active
October 28, 2015 21:30
Approximation of exponential function and pow function in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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