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

Tutoriel de base Java

Contrôle de flux Java

Java Tableau

Java Programmation orientée objet (I)

Java Programmation orientée objet (II)

Java Programmation orientée objet (III)

Gestion des exceptions Java

Java List

Java Queue (File d'attente)

Java Map Collections

Java Set Collections

Java Entrée/Sortie (I/O)

Java Reader/Writer

Autres sujets Java

Utilisation et exemple de la méthode Java Math sqrt()

Java Math mathematical methods

La méthode Java Math sqrt() retourne la racine carrée du nombre spécifié.

La syntaxe de la méthode sqrt() est :

Math.sqrt(double num)

Remarque:sqrt() est une méthode statique. Par conséquent, nous pouvons accéder à cette méthode en utilisant le nom de la classe.

Paramètre de sqrt()

  • num -Le nombre à calculer la racine carrée

Valeur de retour de sqrt()

  • Retourne la racine carrée du nombre spécifié

  • Si le paramètre est inférieur à 0 ou NaN, il renvoie NaN

Remarque:Cette méthode renvoie toujours un nombre positif et arrondit correctement.

Exemple : Java Math sqrt()

class Main {
  public static void main(String[] args) {
    //Create a double variable
    double value1 = Double.POSITIVE_INFINITY;
    double value2 = 25.0;
    double value3 = -16;
    double value4 = 0.0;
    //Square root of infinity
    System.out.println(Math.sqrt(value1));  // Infinity
    //Square root of a positive number
    System.out.println(Math.sqrt(value2));  // 5.0
    //Square root of a negative number
    System.out.println(Math.sqrt(value3));  // NaN
    //Square root of zero
    System.out.println(Math.sqrt(value4));  // 0.0
  }
}

In the above example, we used the Math.sqrt() method to calculate the square root of infinity, positive numbers, negative numbers, and zero.

Here, Double.POSITIVE_INFINITY is used to implement positive infinity in the program.

When we pass an int value to the sqrt() method, it automatically converts the int value to a double value.

int a = 36;
Math.sqrt(a);   // Return 6.0

Recommended tutorials

Java Math mathematical methods