Java中的三角函式


java.lang.Math 類包含三角函式方法,如 cos()、sin()、tan()、tanh()、cosh()、atan() 等。

讓我們來了解一下 Java 中的一些三角函式 −

static double asin(double a)

java.lang.Math.asin(double a) 返回角度的反正弦值,在 -pi/2 到 pi/2 的範圍內。

現在讓我們看一個示例 −

示例

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));
   }
}

輸出

Math.asin(0.027415567780803774)=0.02741900326072046

static double atan(double a)

atan() 方法返回角度的反正切值,在 -pi/2 到 pi/2 的範圍內。

現在讓我們看一個示例,在 Java 中實現 atan() 方法 −

示例

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));
   }
}

輸出

Math.atan(0.027415567780803774)0.0274087022410345

static double cosh(double x)

java.lang.Math.cosh(double a) 返回雙精度的雙曲餘弦值。

現在讓我們看一個示例,在 Java 中實現 cosh() 方法 −

示例

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));
   }
}

輸出

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

更新於: 2019-09-23

2K+ 瀏覽量

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.