📜  Java中的 StrictMath nextUp() 方法

📅  最后修改于: 2022-05-13 01:55:41.427000             🧑  作者: Mango

Java中的 StrictMath nextUp() 方法

  1. nextUp( double num )是Java中 StrictMath 类的一个内置方法,用于获取在正无穷大方向上与num连续的浮点值。 nextup() 方法等价于 nextAfter( num, Double.POSITIVE_INFINITY ) 但 nextup() 运行速度比 nextAfter() 快。

    句法:

    public static double nextUp(double num)

    参数:该方法接受一个double类型的参数num ,即起始浮点值。

    返回值:该方法返回更接近正无穷大的相邻浮点值。它有三种特殊情况:

    • 当参数为 NaN 时,结果为 NaN。
    • num为正无穷大时,它返回正无穷大。
    • 当参数为零时,结果为 Double.MIN_VALUE。

    例子:

    Input: num = 25.18700000
    Output: 25.187000000000005
    

    下面的程序说明了Java.lang.StrictMath.nextUp() 方法:

    // Java praogram to illustrate the
    // Java.lang.StrictMath.nextUp() Method
      
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            double num1 = 67.1230000000;
            double num2 = 21.12, num3 = 0.0;
      
            // Returns floating-point value adjacent to num
            double nextUp_Value = StrictMath.nextUp(num1);
            System.out.println("The Next upper value is : "
                                           + nextUp_Value);
      
            double nextUp_Value1 = StrictMath.nextUp(num2);
            System.out.println("The Next upper value is : " 
                                          + nextUp_Value1);
      
            double nextUp_Value2 = StrictMath.nextUp(num3);
            System.out.println("The Next upper value is : " 
                                          + nextUp_Value2);
        }
    }
    
    输出:
    The Next upper value is : 67.12300000000002
    The Next upper value is : 21.120000000000005
    The Next upper value is : 4.9E-324
    
  2. nextUp(float num)是Java中 StrictMath 类的内置方法,用于获取在正无穷方向上与num相邻的浮点值。Float.POSITIVE_INFINITY) 但 nextup() 运行速度比其等效的 nextAfter 快称呼。

    句法:

    public static float nextUp(float num)

    参数:该方法接受一个float类型的参数num ,即起始浮点值。

    返回值:该方法返回更接近正无穷大的相邻浮点值。它有三种特殊情况:

    • 当参数为 NaN 时,结果为 NaN。
    • num为正无穷大时,它返回正无穷大。
    • 当参数为零时,结果为 Float.MIN_VALUE。

    例子 :

    Input: num = 15.78f
    Output: 15.780000686645508
    

    下面的程序说明了Java.lang.StrictMath.nextUp() 方法:

    // Java praogram to illustrate the
    // Java.lang.StrictMath.nextUp() Method
      
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            float num1 = 73.728f;
            float num2 = 51.71f, num3 = 0.0f;
      
            // Returns floating-point value adjacent to num
            double nextUp_Value = StrictMath.nextUp(num1);
            System.out.println("The Next upper value is : "
                                           + nextUp_Value);
      
            double nextUp_Value1 = StrictMath.nextUp(num2);
            System.out.println("The Next upper value is : "
                                          + nextUp_Value1);
      
            double nextUp_Value2 = StrictMath.nextUp(num3);
            System.out.println("The Next upper value is : "
                                          + nextUp_Value2);
        }
    }
    
    输出:
    The Next upper value is : 73.7280044555664
    The Next upper value is : 51.71000289916992
    The Next upper value is : 1.401298464324817E-45