In Java, Math.sin(a) returns the sine of the angle a. The angle must be in radians, not degrees. Math.sin(Math.PI / 2) is 1.0, and the result always stays between -1.0 and 1.0.
Use it for waves and cycles, for the height of a point on a circle (y = r * sin(angle)), for slopes, and for animation that swings up and down. If your angle is in degrees, convert it with Math.toRadians first.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.sin(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass an angle in radians as a double. The sine comes back as a double.
Java
double top = Math.sin(Math.PI / 2); // 1.0
double half = Math.sin(Math.toRadians(30)); // about 0.5
double flat = Math.sin(0.0); // 0.0
double y = radius * Math.sin(angleInRadians); // height on a circle
Call
What it does
Math.sin(Math.PI / 2)
A quarter turn: returns 1.0.
Math.sin(Math.toRadians(90))
Same angle written in degrees: returns 1.0.
Math.sin(-0.0)
Returns -0.0 (zero keeps its sign).
Math.sin(Double.POSITIVE_INFINITY)
Returns NaN.
There is only a double version. An int argument widens to double and still means radians: Math.sin(1) is the sine of 1 radian (about 0.8415), not of 1 degree.
Library declarations
Java
// java.lang.Math
public static double sin(double a)
Parameters
Parameter
Description
a
The angle in radians. Smaller number types are widened to double.
Return Value
Return Value
Description
double
The sine of a, a value from -1.0 to 1.0. The result is NaN if a is NaN or an infinity. For a zero argument the result is a zero with the same sign.
sin(PI) = 1.2246467991473532E-16
sin(PI) == 0? false
close to 0? true
sin(-0.0) = -0.0
sin(Infinity) = NaN
sin(NaN) = NaN
How It Works
A tiny leftover.Math.PI misses the true pi by about 1.2E-16, and the sine near pi is almost a straight line, so that error shows up as the answer 1.2246467991473532E-16.
Test with a tolerance.== asks for exact zero and answers false; Math.abs(atPi) < 1e-9 asks "is it practically zero?" and answers true.
No sine for these inputs. Negative zero keeps its sign, but infinity and NaN are not angles on a circle, so the method returns NaN without throwing.
Example 3 — The Height of a Wave Every 30 Degrees
Scale the sine to a wave with amplitude 5 and print its height at six angles.
Scale the unit wave. The sine only runs from -1 to 1, so multiplying by amplitude stretches it to a wave that runs from -5 to 5.
A mirror image. 30 and 150 degrees are the same distance from the peak at 90, so both give a height of 2.50.
Rounding hides the noise. At 180 degrees the true height is 0, but the double holds about 6.1E-16; %.2f prints it as 0.00, and 270 degrees reaches the bottom at -5.00.
Frequently Asked Questions
It returns the sine of an angle given in radians, as a double between -1.0 and 1.0. For example, Math.sin(Math.PI / 2) returns 1.0.
Radians. To work with degrees, convert first: Math.sin(Math.toRadians(30)) returns about 0.5, while Math.sin(30) is the sine of 30 radians.
Math.PI is only the double closest to pi, so the sine of it is a tiny leftover, 1.2246467991473532E-16. Round the result or compare it with a small tolerance instead of using ==.
It returns NaN. An infinite angle has no sine, and a NaN input stays NaN. No exception is thrown.
Use Math.asin(x), the inverse of sine. It returns the angle in radians between -PI/2 and PI/2.