Site icon TestingDocs.com

Java Trigonometric Functions with Examples

Overview

This tutorial will teach you different Java trigonometric functions with some code examples. These functions are defined in the java.lang.Math class.

Java Trigonometric Functions

The following table lists the different Trigonometric functions in the Java Math class. Math is a final class. We cannot instantiate an object for this class and there is no need to create an object for this class. These functions are static and can be accessed with the class name.

 

Function Description
sin(x) This function returns the sine of the angle x. The argument x should be in radians.
cos(x) This function returns the cosine of the angle x. The argument x should be in radians.
tan(x) This function returns the tangent of the angle x. The argument x should be in radians.
asin(y) This function returns the angle in radians whose sine is y.
acos(y) This function returns the angle in radians whose cosine is y.
atan(y) This function returns the angle in radians whose tangent is y.
atan2(x,y) This function takes two arguments. It returns the angle in radians whose tangent is x/y.

Example

Let’s write a simple Java program to calculate and print the sin, cosine, and tangent of 60 degrees.

/*
 * Java Trigonometric Functions Examples
 * Java Tutorials - www.TestingDocs.com
 */

public class Trigonometric {

	public static void main(String[] args) {
		// π radians = 180 degrees
		System.out.println("Sin(60) degrees =" +
				Math.sin(Math.PI/3));
		System.out.println("Cos(60) degrees =" + 
				Math.cos(Math.PI/3));
		System.out.println("Tan(60) degrees =" + 
				Math.tan(Math.PI/3));
	}

}

 

Notice how we have simply invoked the static function with the class name.

For example, Math.sin(Math.PI/3);

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version