📜  Java中的 StrictMath nextDown() 方法

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

Java中的 StrictMath nextDown() 方法

nextDown(双数)

nextDown(double num)是Java StrictMath 类的内置方法,用于获取与num相邻的负无穷大方向的浮点值。当参数为 NaN 时,结果为 NaN。当num为负无穷大时,它返回负无穷大。当参数为零时,结果为 Double.MIN_VALUE。 nextDown() 方法等效于 nextAfter( num, Double.NEGATIVE_INFINITY ) 但 nextDown() 比其等效的 nextAfter 调用运行得更快。

句法:

public static double nextDown(double num)

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

返回值:该方法返回更接近负无穷大的相邻浮点值。

例子 :

Input: num = 7.16
Output: 7.159999999999999

下面的程序说明了 nextDown() 方法:

方案一:

Java
// Java program to illustrate the
// Java.lang.StrictMath.nextDown() Method
 
import java.lang.*;
 
public class Geeks {
    public static void main(String[] args)
    {
 
        // Get a double number
        double num1 = 62.9;
 
        // Get the floating-point value adjacent to num
        double nextDown_Value = StrictMath.nextDown(num1);
 
        // Print the result
        System.out.println("The Next down value of "
                           + num1 + " = " + nextDown_Value);
 
        // Get a double number
        double num2 = 16.6226;
 
        // Get the floating-point value adjacent to num
        nextDown_Value = StrictMath.nextDown(num2);
 
        // Print the result
        System.out.println("The Next down value of "
                           + num2 + " = " + nextDown_Value);
 
        // Get a double number
        double num3 = 0.0;
 
        // Get the floating-point value adjacent to num
        nextDown_Value = StrictMath.nextDown(num3);
 
        // Print the result
        System.out.println("The Next down value of "
                           + num3 + " = " + nextDown_Value);
    }
}


Java
// Java program to illustrate the
// Java.lang.StrictMath.nextDown() Method
 
import java.lang.*;
 
public class Geeks {
    public static void main(String[] args)
    {
 
        // Get a float number
        float num1 = 16.622f;
 
        // Get the floating-point value adjacent to num
        float nextDown_Value = StrictMath.nextDown(num1);
 
        // Print the result
        System.out.println("The Next down value of "
                           + num1 + " = " + nextDown_Value);
 
        // Get a float number
        float num2 = 91.11f;
 
        // Get the floating-point value adjacent to num
        nextDown_Value = StrictMath.nextDown(num2);
 
        // Print the result
        System.out.println("The Next down value of "
                           + num2 + " = " + nextDown_Value);
 
        // Get a float number
        float num3 = 0.0f;
 
        // Get the floating-point value adjacent to num
        nextDown_Value = StrictMath.nextDown(num3);
 
        // Print the result
        System.out.println("The Next down value of "
                           + num3 + " = " + nextDown_Value);
    }
}


输出:
The Next down value of 62.9 = 62.89999999999999
The Next down value of 16.6226 = 16.622599999999995
The Next down value of 0.0 = -4.9E-324

nextDown(浮点数)

nextDown(float num)是Java中 StrictMath 类的内置方法,用于获取与num相邻的负无穷大方向的浮点值。当参数为 NaN 时,结果为 NaN。当num为负无穷大时,它返回负无穷大。当参数为零时,结果为 Float.MIN_VALUE。 nextDown() 方法等效于 nextAfter( num, Float.NEGATIVE_INFINITY ) 但 nextDown() 比其等效的 nextAfter 调用运行得更快。

句法:

public static float nextDown(float num)

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

返回值:该方法返回更接近负无穷大的相邻浮点值。

例子 :

Input: num = 1.2f
Output: 1.1999999

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

方案一:

Java

// Java program to illustrate the
// Java.lang.StrictMath.nextDown() Method
 
import java.lang.*;
 
public class Geeks {
    public static void main(String[] args)
    {
 
        // Get a float number
        float num1 = 16.622f;
 
        // Get the floating-point value adjacent to num
        float nextDown_Value = StrictMath.nextDown(num1);
 
        // Print the result
        System.out.println("The Next down value of "
                           + num1 + " = " + nextDown_Value);
 
        // Get a float number
        float num2 = 91.11f;
 
        // Get the floating-point value adjacent to num
        nextDown_Value = StrictMath.nextDown(num2);
 
        // Print the result
        System.out.println("The Next down value of "
                           + num2 + " = " + nextDown_Value);
 
        // Get a float number
        float num3 = 0.0f;
 
        // Get the floating-point value adjacent to num
        nextDown_Value = StrictMath.nextDown(num3);
 
        // Print the result
        System.out.println("The Next down value of "
                           + num3 + " = " + nextDown_Value);
    }
}
输出:
The Next down value of 16.622 = 16.621998
The Next down value of 91.11 = 91.10999
The Next down value of 0.0 = -1.4E-45