Java - Math max(float x, float y) 方法



描述

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

宣告

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

public static float max(float a, float b)

引數

  • a − 一個引數

  • b − 另一個引數

返回值

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

異常

獲取兩個正浮點值的較大值示例

以下示例演示了對兩個正值的 Math max() 方法的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

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

輸出

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

Math.max(60984.1,497.99)=60984.1

獲取一個正浮點值和一個負浮點值的較大值示例

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

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

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

輸出

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

Math.max(-60984.1,497.99)=497.99

獲取兩個負浮點值的較大值示例

以下示例演示了對兩個負值的 Math max() 方法的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

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

輸出

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

Math.max(-60984.1,-497.99)=-497.99
java_lang_math.htm
廣告
© . All rights reserved.