/**
*
*/
package math;
/**
* @author Link
*
*/
public class Main {
private static final double pi180 = Math.PI / 180;
private static final int T1 = 6, T2 = 120, T3 = 5_040, T4 = 362_880, T5 = 39_916_800;
private static final long T6 = 6_227_020_800L, T7 = 1_307_674_368_000L, T8 = 355_687_428_096_000L,
t9 = 121_645_100_408_832_000L;
public static void main(String... args) {
long t = System.currentTimeMillis();
System.out.println("Math.tan(x) = " + Math.tan(pi180 * 140d));
System.out.println("time (ms): " + (System.currentTimeMillis() - t));
t = System.currentTimeMillis();
System.out.println("tangent(x) = " + tan(140d));
System.out.println("time (ms): " + (System.currentTimeMillis() - t));
System.out.println("Math.tan(x) = " + Math.tan(pi180 * 135.5d));
System.out.println("time (ms): " + (System.currentTimeMillis() - t));
t = System.currentTimeMillis();
System.out.println("tangent(x) = " + tan(140d));
System.out.println("time (ms): " + (System.currentTimeMillis() - t));
}
/**
* Faster sine algorithm. Limit -360 to 360.
*
* @param x
* x in radians
* @return the sine of x
*/
public static double sine(double x) {
final double x1 = x, x2 = x1 * x1;
// numerators
final double n1 = x1 * x2, n2 = n1 * x2, n3 = n2 * x2, n4 = n3 * x2, n5 = n4 * x2, n6 = n5 * x2, n7 = n6 * x2,
n8 = n7 * x2, n9 = n8 * x2;
return (x1) - (n1 / T1) + (n2 / T2) - (n3 / T3) + (n4 / T4) - (n5 / T5) + (n6 / T6) - (n7 / T7) + (n8 / T8)
- (n9 / t9);
}
public static double sin(double x) {
return sine(pi180 * x);
}
public static double cosine(double x) {
return sine(Math.PI / 2 - x);
}
public static double cos(double x) {
return cosine(pi180 * x);
}
public static double tangent(double x) {
return cosine(x) / sine(x);
}
public static double tan(double x) {
return tangent(pi180 * x);
}
}
Comments