Optimized Exponential Functions for Java

Usually microoptimization is only done in C or C++, but it works quite well in Java too. For a project I needed very fast log() and exp() calculations, and Java’s Math.log() and Math.exp() just doesn’t cut it. After a bit of research I have found the following approximations that are good enough for me:

UPDATE This pow() approximation is obsolete. I have a much faster and more accurate approximation version here.

Fast Exponential Function in Java

The paper “A Fast, Compact Approximation of the Exponential Function” describes a C macro that does a good job at exploiting the IEEE 754 floating-point representation to calculate e^x. I have transformed the macro into Java code:

public static double exp(double val) {
    final long tmp = (long) (1512775 * val + 1072632447);
    return Double.longBitsToDouble(tmp << 32);
}

This code is 5.3 times faster than Math.exp() on my computer. Beware that it is only an approximation, for a detailed analysis read the paper.

Fast Natural Logarithm in Java

I have found the following approximation here, and there is not much information about it except that it is called “Borchardt’s Algorithm” and it is from the book “Dead Reconing: Calculating without instruments”. The approximation is not very good (some might say very bad…), it gets worse the larger the values are. But the approximation is also a monotonic, slowly increasing function, which is good enough for my use case.

public static double log(double x) {
    return 6 * (x - 1) / (x + 1 + 4 * (Math.sqrt(x)));
}

This approximation is 11.7 times faster than Math.log().

Fast Power Calculation

Equiped with these optimized functions, it is possible to do several other optimizations. For example you can replace

Math.pow(a, b)

with

Math.exp(b * Math.log(a))

And then use the approximation functions for a highly optimized pow calculation. You can even combine the calculations and simplify it into this:

public static double pow(double a, double b) {
    final long tmp = (long) (9076650 * (a - 1) / (a + 1 + 4 * (Math.sqrt(a))) * b + 1072632447);
    return Double.longBitsToDouble(tmp << 32);
}

This is 8.7 times faster than the Math.pow(a, b).

Accuracy

The above functions are very inaccurate, especially the log calculation. So before you use this code you have to test it if the approximation is good enough for you!

Have fun

10 Responses to “Optimized Exponential Functions for Java”

  1. Martin Ankerl on February 12th, 2007 8:35 am

    Here are some benchmarks from a Pentium IV, doing 20 million calculations. On this machine I get an even better performance than stated above. I use Sun’s JRE 1.5.0_08.

    6.233 sec, Math.log(val)
    0.531 sec, 6*(x-1)/ (x + 1 + 4*(Math.sqrt(x)))

    5.920 sec, Math.exp()
    1.108 sec, exp optimized with IEEE 754 trick

    15.967 sec, Math.pow(a, b)
    11.014 sec, e^(b * log(a))
    7.607 sec, e^(b * log(a)) + IEEE 754 trick
    2.109 sec, e^(b * log(a)) + IEEE 754 trick + LOG approximation
    1.827 sec, simplified everything

  2. Exponential Functions: Benchmarks, 11 Times Faster Math.pow() &mdash; Martin Ankerl on February 12th, 2007 3:26 pm

    [...] I have updated the code for the Math.pow() approximation, now it is 11 times faster on my Pentium IV. Read it this. [...]

  3. DoctorEternal on February 22nd, 2007 1:12 am

    Thanks for this. Always good to get more performance tweaking out of Java. I use Processing for game dev in Java, and even with it’s use of Jikes, it could still use a boost.

    Dr.E
    http://www.turingshop.com/reports/01Java/

  4. Optimized pow() approximation for Java and C / C++ by Martin Ankerl on October 4th, 2007 10:48 pm

    [...] I have already written about approximations of e^x, log(x) and pow(a, b) in my post Optimized Exponential Functions for Java. Now I have more . In particular, the pow() function is now even faster, simpler, and more accurate. Without further ado, I proudly give you the brand new approximation: [...]

  5. John on February 24th, 2009 3:04 pm

    I have used the same trick for float, not double, with some slight modification to the constants to suite IEEE754 float format. The first constant for float is 1<<23/log(2) and the second is 127<<23 (for double they are 1<<20/log(2) and 1023<<20).

    You don’t need to do the addition as floating point, I have move the braces around…

    public static double exp(double val) {
        final long tmp = (long) (1512775 * val) + (1072693248 - 60801);
        return Double.longBitsToDouble(tmp << 32);
    }

    Additionally if you analyse the error there is a correlation to the mantissa of the result, which you can then correct for. The error is approximately (mantissa – mantissa^2)/2.9. Doing this all as integer math in fixed point format you get.

    public static double exp(double val) {
        final long tmp = (long) (1512775 * val) + 1072693248;
        final long mantissa = tmp & 0x000FFFFF;
        int error = mantissa >> 7;   // remove chance of overflow
        error = (error - a * a) / 186; // subtract mantissa^2 * 64
                                       // 64 / 186 = 1/2.90625
        return Double.longBitsToDouble((tmp - error) << 32);
    }

    PS hope this is valid Java, I converted from C.

    The explain the shifting. The mantissa has 20 bits to the right of the decimal place. a is formed by shift right 7, leaving 13 bits to the right. When a is squared you double the number of decimal bits arriving at 26. Which is 20 bits times the scaling factor or 64 (6 bits). Now a squared matches error so the subtraction is valid.

  6. Michel Hummel on June 11th, 2009 3:42 pm

    @John
    Hello,
    i’m very interested by the accuracy optimization you suggested but i don’t undertand what is variable “a” in the expression :
    error = (error – a * a) / 186;

    Could you explain it please

    Thanks
    Michel Hummel

  7. Nosredna on August 16th, 2009 3:12 pm

    I think a is just mantissa. I’m guessing John started changing the variable name and forgot what he was doing.

  8. Axeia on January 1st, 2010 4:36 pm

    The exp functions also seems to be working quite for J2ME which is lacking J2ME.

    So if you’re writing say a game with projectiles traveling a certain trajectory it’s good enough :)

  9. Jarek on January 21st, 2010 9:22 pm

    I confirm that those work with J2ME. Thanks to your math approximations I’ve been able to run Speex voice decoding on a mobile with a decent performance :)

  10. Martin Ankerl on January 21st, 2010 9:34 pm

    glad that it’s working :)

Leave a Reply