Trigonometric Functions in Java with Examples
Last Updated :
27 May, 2019
The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
- Java.lang.Math.sin() Method : is an inbuilt method which returns the sine of the value passed as an argument. The value passed in this function should be in radians. If the argument is NaN or an infinity, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument.
Syntax :
Math.sin(double radians)
Parameters :
The method takes one mandatory argument in radians.
Returns :
It returns a double value. The returned value is the sine of the specified double value passed.
Example 1 : Program demonstrating the use of sin()
import java.util.*;
class GFG {
public static void main(String args[])
{
double degrees = 45.0 ;
double radians = Math.toRadians(degrees);
double sinValue = Math.sin(radians);
System.out.println( "sin(" + degrees + ") = " + sinValue);
}
}
|
Output:
sin(45.0) = 0.7071067811865475
-
Java.lang.Math.cos() : is an inbuilt method which returns the cosine of the value passed as an argument. The value passed in this function should be in radians. If the argument is NaN or an infinity, then the result is NaN.
Syntax :
Math.cos(double radians)
Parameters :
The method takes one mandatory argument in radians.
Returns :
It returns a double value. The returned value is the cosine of the specified double value passed.
Example 2 : Program demonstrating the use of cos()
import java.util.*;
class GFG {
public static void main(String args[])
{
double degrees = 45.0 ;
double radians = Math.toRadians(degrees);
double cosValue = Math.cos(radians);
System.out.println( "cos(" + degrees + ") = " + cosValue);
}
}
|
Output:
cos(45.0) = 0.7071067811865476
-
Java.lang.Math.tan() : is an inbuilt method which returns the tangent of the value passed as an argument. The value passed in this function should be in radians. If the argument is NaN or an infinity, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument.
Syntax :
Math.tan(double radians)
Parameters :
The method takes one mandatory argument in radians.
Returns :
It returns a double value. The returned value is the tangent of the specified double value passed.
Example 3 : Program demonstrating the use of tan()
import java.util.*;
class GFG {
public static void main(String args[])
{
double degrees = 45.0 ;
double radians = Math.toRadians(degrees);
double tanValue = Math.tan(radians);
System.out.println( "tan(" + degrees + ") = " + tanValue);
}
}
|
Output:
tan(45.0) = 0.9999999999999999
Share your thoughts in the comments
Please Login to comment...