EDIT there is an updated version for MS JVM in this thread, this one will give weird result if used with Microsoft JVM!
Inspired by previous posts : “Fast Math post” and “Jeff reducing angle technic” , I decide to share a class I created for the 3DzzD Web 3D Engine.
I will run about 15 times faster for random angle input than the original Math.cos, it will also give more accurate results.
package dzzd.utils;
/**
* MathX class.
*
* Base class used by 3DzzD Web 3D Engine to include faster and/or smarter maths functions
*
* <u>Some results comparaison between Math.cos and MathX.cos function, MathX.cos seems to be 0.5 to 20 times faster than Math.cos function and give more accurate results especially for PI/2</u>
*
* <B>-PI</B>
* MathX.cos(-Math.PI) =-1.0
* Math.cos(-Math.PI) =-1.0
*
* <B>PI</B>
* MathX.cos(Math.PI) =-1.0
* Math.cos(Math.PI) =-1.0
*
* Root(2)/2=0.7071067811865476
* <B>PI/4</B>
* MathX.cos(Math.PI*0.25) =0.7071067811865476
* Math.cos(Math.PI*0.25) =0.7071067811865476
*
* <B>PI/2</B>
* MathX.cos(Math.PI*0.25) =0.0
* Math.cos(Math.PI*0.25) =6.123233995736766E-17
*
* <B>-PI/4</B>
* MathX.cos(-Math.PI*0.25)=0.7071067811865476
* Math.cos(-Math.PI*0.25) =0.7071067811865476
*
* <B>-PI/2</B>
* MathX.cos(-Math.PI*0.5) =0.0
* Math.cos(-Math.PI*0.5) =6.123233995736766E-17
*
* @author Bruno Augier (DzzD)
* @version 1.0, 01/03/07
* @since 1.0
* @see IRender3D
* @see IMesh3D
* @see DzzD
* @see <a href="http://dzzd.net/">http://dzzd.net/</a>
*/
public class MathX
{
private static double f2=-0.5;
private static double f4=-f2/(3.0*4.0);
private static double f6=-f4/(5.0*6.0);
private static double f8=-f6/(7.0*8.0);
private static double f10=-f8/(9.0*10.0);
private static double f12=-f10/(11.0*12.0);
private static double f14=-f12/(13.0*14.0);
private static double f16=-f14/(15.0*16.0);
private static double f18=-f16/(17.0*18.0);
private static double f20=-f18/(19.0*20.0);
private static double PI=Math.PI;
private static double PI2=2.0*PI;
private static double PI05=0.5*PI;
/**
* Compute and return sinus of its parameter using taylor serie
* @param x angle in radian to
* @return sinus value for the given parameter
*/
public static double sin(double x)
{
return cos(x-PI05);
}
/**
* Compute and return cosinus of its parameter using taylor serie
* @param x angle in radian to
* @return cosinus value for the given parameter
*/
public static double cos(double x)
{
if(x<0.0) x=-x;
if(x<PI2)
{
if(x<PI)
{
double x2=x*x;
return 1.0+x2*(f2+x2*(f4+x2*(f6+x2*(f8+x2*(f10+x2*(f12+x2*(f14+x2*(f16+x2*(f18+x2*f20)))))))));
}
else
{
x-=PI;
double x2=x*x;
return -(1.0+x2*(f2+x2*(f4+x2*(f6+x2*(f8+x2*(f10+x2*(f12+x2*(f14+x2*(f16+x2*(f18+x2*f20))))))))));
}
}
x%=PI2;
x-=PI;
double x2=x*x;
return -(1.0+x2*(f2+x2*(f4+x2*(f6+x2*(f8+x2*(f10+x2*(f12+x2*(f14+x2*(f16+x2*(f18+x2*f20))))))))));
}
}