Java Math sin()
sin() accepts angle in radians as an argument and returns sine of the argument value. The returned value is of type double.
If the argument is NaN or infinity, then sin() returns NaN.
Following is the syntax of sin() method.
double value = sin(double angle)
Since the definition of sin() function has double datatype as argument, you can pass int, float or long as arguments as these datatypes will implicitly promote to double.
Example 1 – Math.sin(double)
In the following example, we pass angle in radians as argument to sin() method and find its value.
Java Program
public class MathExample {
public static void main(String[] args) {
double angle = 0.5; //radians
double result = Math.sin(angle);
System.out.println(result);
}
}
Output
0.479425538604203
0.5 radians is approximately equal to 28.6 degrees. And sin(28.6 degrees) is equal to 0.47 approximately. Our program gives a precise result.
Example 2 – Math.sin(int)
In the following example, we pass an int value for the argument to sin() method.
Java Program
public class MathExample {
public static void main(String[] args) {
int angle = 2; //radians
double result = Math.sin(angle);
System.out.println(result);
}
}
Output
0.9092974268256817
Similarly, you can provide a float or long value as argument to sin() method.
Example 3 – Math.sin(NaN)
In the following example, we pass Double.NaN as argument to sin() method. As per the definition of the function, sin() should return NaN value.
Java Program
public class MathExample {
public static void main(String[] args) {
double angle = Double.NaN;
double result = Math.sin(angle);
System.out.println(result);
}
}
Output
NaN
Example 4 – Math.sin() – With Infinity as Argument
In the following example, we pass Double.POSITIVE_INFINITY as argument to sin() method. As per the definition of the sin() method in Math class, sin() should return NaN value.
Java Program
public class MathExample {
public static void main(String[] args) {
double angle = Double.POSITIVE_INFINITY;
double result = Math.sin(angle);
System.out.println(result);
}
}
Output
NaN
Conclusion
In this Java Tutorial, we learned about Java Math.sin() function, with example programs.