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



描述

Java StrictMath max(float a, float b) 方法返回两个浮点值中较大的一个。也就是说,结果是更接近正无穷大的参数。如果参数值相同,则结果是该值。如果任一值为 NaN,则结果为 NaN。与数值比较运算符不同,此方法认为负零严格小于正零。如果一个参数为正零,另一个参数为负零,则结果为正零。

声明

以下是java.lang.StrictMath.max() 方法的声明

public static float max(float a, float b)

参数

  • a − 一个参数

  • b − 另一个参数

返回值

此方法返回 a 和 b 中较大的一个。

异常

两个正浮点值的比较示例

以下示例演示了如何使用 StrictMath max() 方法比较两个正值。

package com.tutorialspoint;

public class StrictMathDemo {

   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("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 float numbers
      float x = -60984.1f;
      float y = 497.99f;
   
      // 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 float numbers
      float x = -60984.1f;
      float y = -497.99f;
   
      // 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
广告
© . All rights reserved.