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

Tutoriel de base Java

Contrôle de flux Java

Java tableau

Java orienté objet (I)

Java orienté objet (II)

Java orienté objet (III)

Gestion des exceptions Java

Java Liste (List)

Java Queue (file d'attente)

Java Map collection

Java Set collection

Java entrée/sortie (I/O)

Java Reader/Writer

Autres sujets Java

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

Java Math mathematical methods

La méthode Java Math copySign() copie le signe du deuxième paramètre et l'attribue au premier paramètre.

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

Math.copySign(arg1, arg2)

Note:La méthode copySign() est une méthode statique. Par conséquent, nous pouvons appeler cette méthode directement en utilisant le nom de la classe Math.

Paramètres de copySign()

  • arg1 - Le premier paramètre à remplacer par son symbole

  • arg2 - Le deuxième paramètre, dont le symbole est copié dans arg1

notemeaning: data type arg1and arg2should be float or double.

copySign() return value

  • returns the first parameter, arg1with the second parameter arg2the sign

Note: for the parameter(arg1,-arg2), this method returns-arg1.

Example: Java Math.copySign()

class Main {
  public static void main(String[] args) {
    //  Copy the sign of the double parameter
    double x = 9.6d;
    double y = -6.45;
    System.out.println(Math.copySign(x, y));  // -9.6
    // Copy the sign of the float parameter
    float a = -4.5f;
    float b = 7.34f;
    System.out.println(Math.copySign(a, b));  // 4.5
  }
}

As you can see, here the copySign() method assigns the sign of the second variable (y and b) is assigned to the first variable (x and a)

Java Math mathematical methods