Trigonometric Functions in Java


The java.lang.Math class contains methods for trigonometric operations like cos(), sin(), tan(), tanh(), cosh(), atan(), etc.

Let us work around some of these trigonometric functions in Java −

static double asin(double a)

The java.lang.Math.asin(double a) returns the arc sine of an angle, in the range of -pi/2 through pi/2.

Let us now see an example −

Example

import java.util.*;
public class Main {
   public static void main(String args[]) {
      // get a variable x which is equal to PI/2
      double x = Math.PI / 2;
      // convert x to radians
      x = Math.toRadians(x);
      // get the arc sine of x
      System.out.println("Math.asin(" + x + ")=" + Math.asin(x));
   }
}

Output

Math.asin(0.027415567780803774)=0.02741900326072046

static double atan(double a)

The atan() method returns the arc tangent of an angle, in the range of -pi/2 through pi/2.

Let us now see an example to implement the atan() method in Java −

Example

import java.util.*;
public class Main {
   public static void main(String args[]) {
      // get a variable x which is equal to PI/2
      double x = Math.PI / 2;
      // convert x to radians
      x = Math.toRadians(x);
      // get the arc tangent of x
      System.out.println("Math.atan(" + x + ")" + Math.atan(x));
   }
}

Output

Math.atan(0.027415567780803774)0.0274087022410345

static double cosh(double x)

The java.lang.Math.cosh(double a) returns the hyperbolic cosine of a double value.

Let us now see an example to implement the cosh() method in Java −

Example

import java.util.*;
public class Main {
   public static void main(String args[]) {
      // get two double numbers
      double x = 45.0;
      double y = 180.0;
      // convert them to radians
      x = Math.toRadians(x);
      y = Math.toRadians(y);
      // print their hyperbolic cosine
      System.out.println("Math.cosh(" + x + ")=" + Math.cosh(x));
      System.out.println("Math.cosh(" + y + ")=" + Math.cosh(y));
   }
}

Output

Math.cosh(0.7853981633974483)=1.3246090892520057
Math.cosh(3.141592653589793)=11.591953275521519

Updated on: 23-Sep-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements