Java 中的三角函数

java 8object oriented programmingprogramming更新于 2025/11/29 23:40:17

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[]) {
      // 获取等于 PI/2 的变量 x
      double x = Math.PI / 2;
      // 将 x 转换为弧度
      x = Math.toRadians(x);
      // 获取 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[]) {
      // 获取等于 PI/2 的变量 x
      double x = Math.PI / 2;
      // 将 x 转换为弧度
      x = Math.toRadians(x);
      // 获取 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) 返回 double 值的双曲余弦值。

现在让我们看一个在 Java 中实现 cosh() 方法的示例−

示例

import java.util.*;
public class Main {
   public static void main(String args[]) {
      // 获取两个双精度数
      double x = 45.0;
      double y = 180.0;
      // 将它们转换为弧度
      x = Math.toRadians(x);
      y = Math.toRadians(y);
      // 打印它们的双曲余弦值
      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

相关文章