Java - StrictMath max(double x, double y) 方法



描述

Java StrictMath max(double a, double b) 方法返回兩個雙精度浮點數中較大的一個。也就是說,結果是更接近正無窮大的引數。如果引數的值相同,則結果為該相同的值。如果任一值是 NaN,則結果為 NaN。與數值比較運算子不同,此方法認為負零嚴格小於正零。如果一個引數是正零,另一個引數是負零,則結果是正零。

宣告

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

public static double max(double a, double b)

引數

  • a − 一個引數

  • b − 另一個引數

返回值

此方法返回 a 和 b 中較大的一個。

異常

獲取兩個正雙精度浮點數的最大值示例

以下示例顯示了兩個正值的 StrictMath max() 方法的使用。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = 60984.1;
      double y = 497.99;
   
      // call max and print the result
      System.out.println("StrictMath.max(" + x + "," + y + ")=" + StrictMath.max(x, y));
   }
}

輸出

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

StrictMath.max(60984.1,497.99)=60984.1

獲取一個正雙精度浮點數和一個負雙精度浮點數的最大值示例

以下示例顯示了一個正值和一個負值的 StrictMath max() 方法的使用。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = -60984.1;
      double y = 497.99;
   
      // call max and print the result
      System.out.println("StrictMath.max(" + x + "," + y + ")=" + StrictMath.max(x, y));
   }
}

輸出

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

StrictMath.max(-60984.1,497.99)=497.99

獲取兩個負雙精度浮點數的最大值示例

以下示例顯示了兩個負值的 StrictMath max() 方法的使用。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = -60984.1;
      double y = -497.99;
   
      // call max and print the result
      System.out.println("StrictMath.max(" + x + "," + y + ")=" + StrictMath.max(x, y));
   }
}

輸出

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

StrictMath.max(-60984.1,-497.99)=-497.99
java_lang_strictmath.htm
廣告