Just a simple utility class I created for my current project. It keeps a lookup table of all the trig values and square root values, etc. so that it is only a simple lookup operation and it doesn’t have to calculate anything. You can adjust the resolution of the trig and square root functions and the maximum argument the square root function can handle depending on your memory limitations. Feel free to extend it. Let me know what you think
public final class FastMath{
public static final float PI=3.1415926535897932384626433832795028841971f;
public static final float DEGRAD=PI/180f, RADDEG=180f/PI;
private static final float TRIG_RES=DEGRAD*0.1f, SQRT_RES=0.1f, SQRT_MAX=10000f;
private static float[] sin, cos, tan, asin, acos, atan, sqrt;
private static boolean initialized=false;
public static final void initialize(){
sin=new float[(int)(2f*PI/TRIG_RES)];
cos=new float[(int)(2f*PI/TRIG_RES)];
tan=new float[(int)(2f*PI/TRIG_RES)];
asin=new float[(int)(2f*PI/TRIG_RES)];
acos=new float[(int)(2f*PI/TRIG_RES)];
atan=new float[(int)(2f*PI/TRIG_RES)];
for(int i=0;i<sin.length;i++){
sin[i]=(float)Math.sin(i*TRIG_RES);
cos[i]=(float)Math.cos(i*TRIG_RES);
tan[i]=(float)Math.tan(i*TRIG_RES);
asin[i]=(float)Math.asin(i*TRIG_RES);
acos[i]=(float)Math.acos(i*TRIG_RES);
atan[i]=(float)Math.atan(i*TRIG_RES);
}
sqrt=new float[(int)(SQRT_MAX*(1f/SQRT_RES))];
for(int i=0;i<sqrt.length;i++){
sqrt[i]=(float)Math.sqrt(i*SQRT_RES);
}
initialized=true;
}
public static final float sin(float i){
if(!initialized) initialize();
return sin[(int)(i/TRIG_RES)%sin.length];
}
public static final double sin(double i){
if(!initialized) initialize();
return (double)sin[(int)(i/TRIG_RES)%sin.length];
}
public static final float cos(float i){
if(!initialized) initialize();
return cos[(int)(i/TRIG_RES)%sin.length];
}
public static final double cos(double i){
if(!initialized) initialize();
return (double)cos[(int)(i/TRIG_RES)%sin.length];
}
public static final float tan(float i){
if(!initialized) initialize();
return tan[(int)(i/TRIG_RES)%sin.length];
}
public static final double tan(double i){
if(!initialized) initialize();
return (double)tan[(int)(i/TRIG_RES)%sin.length];
}
public static final float asin(float i){
if(!initialized) initialize();
return asin[(int)(i/TRIG_RES)%sin.length];
}
public static final double asin(double i){
if(!initialized) initialize();
return (double)asin[(int)(i/TRIG_RES)%sin.length];
}
public static final float acos(float i){
if(!initialized) initialize();
return acos[(int)(i/TRIG_RES)%sin.length];
}
public static final double acos(double i){
if(!initialized) initialize();
return (double)acos[(int)(i/TRIG_RES)%sin.length];
}
public static final float atan(float i){
if(!initialized) initialize();
return atan[(int)(i/TRIG_RES)%sin.length];
}
public static final double atan(double i){
if(!initialized) initialize();
return (double)atan[(int)(i/TRIG_RES)%sin.length];
}
public static final float abs(float i){
return (i<0?-i:i);
}
public static final double abs(double i){
return (i<0?-i:i);
}
public static final int abs(int i){
return (i<0?-i:i);
}
public static final float sqrt(float i){
if(!initialized) initialize();
return sqrt[(int)(i/SQRT_RES)%sqrt.length];
}
public static final double sqrt(double i){
if(!initialized) initialize();
return (double)sqrt[(int)(i/SQRT_RES)%sqrt.length];
}
public static final float toDegrees(float rad){
return rad*180f/PI;
}
public static final double toDegrees(double rad){
return rad*180f/PI;
}
public static final float toRadians(float deg){
return deg*PI/180f;
}
public static final double toRadians(double deg){
return deg*PI/180f;
}
}