Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FastMath
- {
- public static final double PI = Math.PI;
- public static final double TWOPI = PI * 2;
- public static final double HalfPI = PI / 2;
- public static final double OneFourthPI = PI / 4;
- /**
- * This forces the trig functiosn to stay
- * within the safe area on the x86 processor
- *(-45 degrees to +45 degrees)
- * The results may be very slightly off from
- * what the Math and StrictMath trig functions
- * give due to rounding in the angle reduction
- * but it will be very very close.
- */
- public static double reduceSinAngle(double radians) {
- radians %= TWOPI; // put us in -2PI to +2PI space
- if (Math.abs(radians)>PI) { // put us in -PI to +PI space
- radians = radians-(TWOPI);
- }
- if (Math.abs(radians)>HalfPI) {// put us in -PI/2 to +PI/2 space
- radians = PI - radians;
- }
- return radians;
- }
- public static double sin (double radians) {
- radians = reduceSinAngle(radians); // limits angle to between -PI/2 and +PI/2
- if (Math.abs(radians)<=OneFourthPI) {
- return Math.sin(radians);
- } else {
- return Math.cos(HalfPI-radians);
- }
- }
- public static double cos (double radians) {
- return sin (radians+HalfPI);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement