Java - StrictMath copySign(double) 方法



描述

Java StrictMath copySign(double magnitude, double sign) 方法返回第一個浮點數引數,並帶有第二個浮點數引數的符號。

宣告

以下是 java.lang.StrictMath.copySign() 方法的宣告

public static double copySign(double magnitude, double sign)

引數

  • magnitude − 提供結果大小的引數

  • sign − 提供結果符號的引數

返回值

此方法返回一個值,該值的大小為 magnitude,符號為 sign。

異常

獲取第一個雙精度引數複製第二個正雙精度引數的符號示例

以下示例顯示了 StrictMath copySign() 方法的用法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = 125.9;
      double y = -0.4873;
   
      // print a double with the magnitude of x and the sign of y
      System.out.println("StrictMath.copySign(" + x + "," + y + ")=" + StrictMath.copySign(x, y));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

StrictMath.copySign(125.9, -0.4873)=-125.9

獲取第一個雙精度引數複製第二個負雙精度引數的符號示例

以下示例顯示了 StrictMath copySign() 方法在交換引數時的另一種用法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = 125.9;
      double y = -0.4873;
   
      // print a double with the magnitude of y and the sign of x
      System.out.println("StrictMath.copySign(" + y + "," + x + ")=" + StrictMath.copySign(y, x));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

StrictMath.copySign(-0.4873, 125.9)=0.4873

獲取零複製第二個雙精度引數的符號示例

以下示例顯示了 StrictMath copySign() 方法對零值的用法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = 0;
      double y = -0.4873;
   
      // print a double with the magnitude of x and the sign of y
      System.out.println("StrictMath.copySign(" + x + "," + y + ")=" + StrictMath.copySign(x, y));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

StrictMath.copySign(0.0,-0.4873)=-0.0
java_lang_strictmath.htm
廣告