Friday 3 June 2022

Trigonometric functions in Java with example

Java Math class provides various functions to support trigonometric calculations. Below table summarizes the methods.

 

Method

Description

acos(double a)

Return arc cosine of a value.

asin(double a)

Return arc sine of a value.

atan(double a)

Return arc tangent of a value.

cos(double a)

Returns the trigonometric cosine of an angle. Angle ‘a’ is specified in radians.

cosh(double x)

Returns the hyperbolic cosine of a double value.

sin(double a)

Returns the trigonometric sine of an angle. Angle ‘a’ is specified in radians.

sinh(double x)

Returns the hyperbolic sine of a double value.

tan(double a)

Returns the trigonometric tangent of an angle. Angle ‘a’ is specified in radians.

tanh(double x)

Returns the hyperbolic tangent of a double value.




TrignometricFunctionsDemo.java

package com.sample.app;

public class TrignometricFunctionsDemo {
	public static void main(String[] args) {
		double degrees = 45;
		double radians = Math.toRadians(degrees);

		double acosValue = Math.acos(0.5);
		double asinValue = Math.asin(0.5);
		double atanValue = Math.atan(0.5);
		double cosValue = Math.cos(radians);
		double coshValue = Math.cosh(0.5);
		double sinValue = Math.sin(radians);
		double sinhValue = Math.sinh(0.5);
		double tanValue = Math.tan(radians);
		double tanhValue = Math.tanh(0.5);

		System.out.println("degrees : " + degrees);
		System.out.println("radians : " + radians);
		System.out.println("acosValue : " + acosValue);
		System.out.println("asinValue : " + asinValue);
		System.out.println("atanValue : " + atanValue);
		System.out.println("cosValue : " + cosValue);
		System.out.println("coshValue : " + coshValue);
		System.out.println("sinValue : " + sinValue);
		System.out.println("sinhValue : " + sinhValue);
		System.out.println("tanValue : " + tanValue);
		System.out.println("tanhValue : " + tanhValue);
	}

}

 

Output

degrees : 45.0
radians : 0.7853981633974483
acosValue : 1.0471975511965979
asinValue : 0.5235987755982989
atanValue : 0.4636476090008061
cosValue : 0.7071067811865476
coshValue : 1.1276259652063807
sinValue : 0.7071067811865475
sinhValue : 0.5210953054937474
tanValue : 0.9999999999999999
tanhValue : 0.46211715726000974

 


Previous                                                 Next                                                 Home

No comments:

Post a Comment