commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bill Rossi <>
Subject Re: [math] elementary functions.
Date Tue, 08 Jun 2010 15:29:01 GMT

I've looked at StrictMath, generally Math appears to delegates to 
StrictMath.  StrictMath is implemented by the C fdlibm library.

This is what I've implemented to date, the code will compile and run
on JDK 1.4 and later.  It may work on older JDKs, but I don't have them
available for testing.  Note that functions like expm1 are not available
in earlier JDKs.

public class FastMath extends java.lang.Object{
     public FastMath();
     public static double exp(double);
     public static double expm1(double);
     public static double log(double);
     public static double log1p(double);
     public static double log10(double);
     public static double pow(double, double);
     public static double sin(double);
     public static double cos(double);
     public static double tan(double);
     public static double atan(double);
     public static double atan2(double, double);
     public static double toRadians(double);
     public static double toDegrees(double);
     public static double abs(double);
     public static double ulp(double);
     public static double floor(double);
     public static double ceil(double);
     public static double rint(double);
     public static long round(double);
     static {};

Performance test gives these results:

Function       Time     Result                  Function     Time       Result
StrictMath.log 967      1.5118099917827207E8    FastMath.log 553        1.5118099917827207E8
StrictMath.pow 3199     4.6455095486440872E16   FastMath.pow 1967       4.645509548644088E16
StrictMath.exp 1079     2.2025454782076317E10   FastMath.exp 562        2.2025454782076317E10
StrictMath.sin 1151     1839071.8010869955      FastMath.sin 766        1839071.8010869955
StrictMath.cos 1173     -544020.191353572       FastMath.cos 665        -544020.191353572
StrictMath.tan 1568     -5.024600819552688E7    FastMath.tan 1081       -5.024600819552688E7
StrictMath.atan 1079    1.2403715749052648E7    FastMath.atan 902       1.2403715749052648E7
StrictMath.expm1 727    -9899999.500018543      FastMath.expm1 773      -9899999.500018543

This table shows execution time for 10,000,000 calls in milliseconds.   The result printed
there to prevent the JIT from optimizing away the calculation entirely.

Note that some functions such as exp are nearly twice as fast.   I've seen it 3 times faster

on different processors.   The preformance varies by the relative speed of calculation vs

memory lookups.

The functions are implemented as tables of values in extra precision (approx 70 bits), and

then interpolated with a minimax polynomial.

Typical test code:

       x = 0;
       time = System.currentTimeMillis();
       for (int i=0; i<10000000; i++)
       time = System.currentTimeMillis() - time;
       System.out.print("StrictMath.exp "+time+"\t"+x+"\t");

       x = 0;
       time = System.currentTimeMillis();
       for (int i=0; i<10000000; i++)
       time = System.currentTimeMillis() - time;
       System.out.println("FastMath.exp "+time+"\t"+x);

To test accuracy, I'd compute results and compare them to an aribitrary precision math 

On Tue, 8 Jun 2010, James Carman wrote:

> Have you tried looking at StrictMath?
> On Tue, Jun 8, 2010 at 10:44 AM, Ted Dunning <> wrote:
>> Bill,
>> Which functions do you have?
>> Anything more than the standard sin, cos, exp and log?
>> On Tue, Jun 8, 2010 at 6:52 AM, Bill Rossi <> wrote:
>>> I have developed over the past year a set of elementary functions similar
>>> to those in java.lang.Math, but with the following characteristics:
>>> * Higher performance.
>>> * Better accuracy.  Results are accurate to slightly more that +/- 0.5 ULP.
>>> * Pure Java.  The standard Math class is impleneted via JNI, and thus takes
>>> a performance hit.
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

View raw message