English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Java

Java 流程控制

Java 数组

Java 面向对象(I)

Java 面向对象(II)

Java 面向对象(III)

Gestion des exceptions Java

Java 列表(List)

Java Queue(队列)

Java Map集合

Java Set集合

Java 输入输出(I/O)

Java Reader/Writer

Java 其他主题

Java Math acos() 使用方法及示例

Java Math mathematical methods

Java Math acos()方法返回指定值的反余弦值。

反余弦是余弦函数的反函数。

acos()方法的语法为:

Math.acos(double num)

acos()参数

  • num - 要返回其反余弦的数字,它应始终小于1。

acos()返回值

  • 返回指定数字的反余弦值

  • 如果指定数是NaN或大于1,返回NaN

Note:返回值是介于0.0 à pi entre les angles.

Example1:Java Math acos()

import java.lang.Math;
class Main {
  public static void main(String[] args) {
    //Create variables
    double a = 0.5;
    double b = 0.79;
    double c = 0.0;
    //Print the inverse cosine value
    System.out.println(Math.acos(a));  // 1.0471975511965979
    System.out.println(Math.acos(b));  // 0.6599873293874984
    System.out.println(Math.acos(c));  // 1.5707963267948966
  }
}

Dans l'exemple précédent, nous avons importé le paquet java.lang.Math. Si nous devons utiliser les méthodes de la classe Math, c'est très important. Notez l'expression

Math.acos(a)

Here, we directly used the class name to call the method. This is because acos() is a static method.

Example2: The mathematical acos() returns NaN

import java.lang.Math;
class Main {
  public static void main(String[] args) {
    //Create variables
    double a = 2;
        //The square root of a negative number.
        //The result is not a number (NaN)
    double NaN = Math.sqrt(-5);
    //Print the inverse cosine value
    System.out.println(Math.acos(a));  // NaN
    System.out.println(Math.acos(NaN));  // NaN
  }
}

Here, we created two variables named a and b.

  • Math.acos(a) - Because the value of a is greater than1, returns NaN.

  • Math.acos(b) - Because negative numbers (-5). The square root is not a number, returns NaN.

Note: We useMath sqrt()A method to calculate the square root of a number.

Java Math mathematical methods