Java sin sin(double radians)

Here you can find the source of sin(double radians)

Description

Get the sine of an angle

License

Apache License

Parameter

Parameter Description
radians The angle

Return

The sine of the angle

Declaration

public static double sin(double radians) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   w  w  w . j a v a 2 s.co  m
     * Get the sine of an angle
     * 
     * @param radians The angle
     * @return The sine of the angle
     * @author JeffK
     */
    public static double sin(double radians) {
        radians = reduceSinAngle(radians); // limits angle to between -PI/2 and +PI/2
        if (Math.abs(radians) <= Math.PI / 4) {
            return Math.sin(radians);
        } else {
            return Math.cos(Math.PI / 2 - radians);
        }
    }

    /**
     * Fast Trigonometry functions for x86.  
     * This forces the trigonometry functions 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 trigonometry functions give due to
     * rounding in the angle reduction but it will be very very close.
     * 
     * @param radians The original angle
     * @return The reduced Sin angle
     * @author JeffK
     */
    private static double reduceSinAngle(double radians) {
        radians %= Math.PI * 2.0; // put us in -2PI to +2PI space

        if (Math.abs(radians) > Math.PI) { // put us in -PI to +PI space
            radians = radians - (Math.PI * 2.0);
        }
        if (Math.abs(radians) > Math.PI / 2) {// put us in -PI/2 to +PI/2 space
            radians = Math.PI - radians;
        }

        return radians;
    }

    /**
     * Get the cosine of an angle
     * 
     * @param radians The angle
     * @return The cosine of the angle
     * @author JeffK
     */
    public static double cos(double radians) {
        return sin(radians + Math.PI / 2);
    }
}

Related

  1. sin(double a)
  2. sin(double anAngle)
  3. sin(double d)
  4. sin(double... v)
  5. sin(final double value)
  6. sin(final float angle)
  7. sin(float angle)