What is Math.sin() in Java?

Java has a built-in Math class, defined in the java.lang package, which contains important functions to perform basic mathematical operations. The class has the sin() method, which is used to compute a specified angle’s trigonometric sine.

Syntax

public static double sin(double angle);

Parameter

  • angle represents the double value corresponding to the angle in radians, of which we have to find the sine.

Return value

  • The function returns the double value corresponding to the trigonometric sine of the angle.

If the value of angle is infinity or NaNnot a number, then the returned value is NaN.

Code

To convert an angle measured in degrees to an equivalent angle measured in radians, we can use the built-in method Math.toRadians(double angle_in_degrees).

import java.lang.Math;
class HelloWorld {
public static void main( String args[] ) {
double param = Math.toRadians(30);
double result = Math.sin(param);
System.out.println("sin(" + param + ") = " + result);
param = Math.toRadians(45);
result = Math.sin(param);
System.out.println("sin(" + param + ") = " + result);
param = Math.toRadians(60);
result = Math.sin(param);
System.out.println("sin(" + param + ") = " + result);
param = Math.toRadians(90);
result = Math.sin(param);
System.out.println("sin(" + param + ") = " + result);
result = Math.sin(Double.POSITIVE_INFINITY);
System.out.println("sin(∞) = " + result);
result = Math.sin(Double.NaN);
System.out.println("sin(NaN) = " + result);
}
}