Advertisement
Guest User

Untitled

a guest
Jun 4th, 2008
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. class FastMath
  2. {
  3.     public static final double PI = Math.PI;
  4.     public static final double TWOPI = PI * 2;
  5.     public static final double HalfPI = PI / 2;
  6.     public static final double OneFourthPI = PI / 4;
  7.  
  8.     /**
  9.     * This forces the trig functiosn to stay
  10.     * within the safe area on the x86 processor
  11.     *(-45 degrees to +45 degrees)
  12.     * The results may be very slightly off from
  13.      * what the Math and StrictMath trig functions
  14.      * give due to rounding in the angle reduction
  15.      * but it will be very very close.
  16.      */
  17.     public static double reduceSinAngle(double radians) {
  18.         radians %= TWOPI; // put us in -2PI to +2PI space
  19.         if (Math.abs(radians)>PI) { // put us in -PI to +PI space
  20.             radians = radians-(TWOPI);
  21.         }
  22.         if (Math.abs(radians)>HalfPI) {// put us in -PI/2 to +PI/2 space
  23.             radians = PI - radians;
  24.         }
  25.  
  26.         return radians;
  27.     }
  28.  
  29.     public static double sin (double radians) {
  30.  
  31.         radians = reduceSinAngle(radians); // limits angle to between -PI/2 and +PI/2
  32.         if (Math.abs(radians)<=OneFourthPI) {
  33.             return Math.sin(radians);
  34.         } else {
  35.             return Math.cos(HalfPI-radians);
  36.         }
  37.     }
  38.  
  39.     public static double cos (double radians) {
  40.  
  41.         return sin (radians+HalfPI);
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement