strictmath
StrictMath类的nextAfter()方法 (StrictMath Class nextAfter() method)
Syntax:
句法:
public static double nextAfter(double starts , double directions);
public static float nextAfter(float starts , double directions);
nextAfter() method is available in java.lang package.
nextAfter()方法在java.lang包中可用。
nextAfter(double starts, double directions) method is used to return the double floating-point number adjacent to the first parameter (starts) in the direction of the second parameter (directions).
nextAfter(double starts,double direction)方法用于在第二个参数(directions)的方向上返回与第一个参数(starts)相邻的double浮点数。
nextAfter(float starts, double directions) method is used to return the float type floating-point number adjacent to the first parameter (starts) in the direction of the second parameter (directions).
nextAfter(float starts,double direction)方法用于在第二个参数(directions)的方向上返回与第一个参数(starts)相邻的浮点型浮点数。
These methods don't throw an exception. These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.
这些方法不会引发异常。 这些是静态方法,可以通过类名进行访问,如果尝试使用类对象访问这些方法,则不会出现任何错误。
Parameter(s):
参数:
start – it represents the initial or starting floating-point value of float or double type.
start –代表float或double类型的初始或起始浮点值。
directions – it represents the value denoting which of the given first parameter neighbors (start's neighbour) or start is returned.
Directions –它表示一个值,该值指示返回给定的第一个参数邻居(start的邻居)或start。
Return value:
返回值:
The return type of this method is float / double – it returns the float type floating point number adjacent to start in the direction of second argument.
此方法的返回类型为float / double-返回第二个参数方向上与开始位置相邻的float类型浮点数。
Note:
注意:
If we pass the same value in both arguments, the method returns the same value.
如果我们在两个参数中传递相同的值,则该方法将返回相同的值。
If we pass Float.MIN_VALUE / Double.MIN_VALUE as the first argument and the second argument contains another value, the method returns the smaller value.
如果我们将Float.MIN_VALUE / Double.MIN_VALUE作为第一个参数传递,而第二个参数包含另一个值,则该方法将返回较小的值。
If we pass an infinity as the first argument and the second argument contains another value, the method returns the Float.MAX_VALUE / Double.MAX_VALUE with the sign of the first argument.
如果我们传递无穷大作为第一个参数,而第二个参数包含另一个值,则该方法将返回带有第一个参数符号的Float.MAX_VALUE / Double.MAX_VALUE 。
If we pass Float.MAX_VALUE / Double.MAX_VALUE as the first argument and the second argument contains another value, the method returns the largest value with the sign of the first argument.
如果我们将Float.MAX_VALUE / Double.MAX_VALUE作为第一个参数传递,而第二个参数包含另一个值,则该方法将返回带有第一个参数符号的最大值。
Example:
例:
// Java program to demonstrate the example
// of nextAfter() method of StrictMath class
public class NextAfter {
public static void main(String[] args) {
// variable declarations
double d1 = -2.6;
double d2 = 0.0;
double d3 = -0.6;
double d4 = 7.0 / 0.0;
float f1 = -2.6f;
float f2 = 0.0f;
double d5 = -7.0 / 0.0;
// Display previous value of d1,d2,d3 and d4
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);
System.out.println("d3: " + d3);
System.out.println("d4: " + d4);
// Display previous value of f1,f2 and d5
System.out.println("f1: " + f1);
System.out.println("f2: " + f2);
System.out.println("d5: " + d5);
System.out.println("nextAfter(double, double): ");
// Here , we will get (-2.5 (approx.)) because we are
// passing parameter whose value is (-2.6,0.0)
System.out.println("StrictMath.nextAfter (d1,d2): " + StrictMath.nextAfter(d1, d2));
// Here , we will get (-4.9(approx)) and we are
// passing parameter whose value is (0.0,-2.6)
System.out.println("StrictMath.nextAfter (d2,d1): " + StrictMath.nextAfter(d2, d1));
// Here , we will get (Double.MAX_VALUE) and we are
// passing parameter whose value is (7.0/0.0,0.0)
System.out.println("StrictMath.nextAfter (d4,d2): " + StrictMath.nextAfter(d4, d2));
// Here , we will get (largest value) and we are
// passing parameter whose value is (0.0,7.0/0.0)
System.out.println("StrictMath.nextAfter (d2,d4): " + StrictMath.nextAfter(d2, d4));
System.out.println();
System.out.println("nextAfter(float, double): ");
// Here , we will get (-2.5 (approx.)) because we are
// passing parameter whose value is (-2.6f,0.0)
System.out.println("StrictMath. nextAfter (f1,d3): " + StrictMath.nextAfter(f1, d3));
// Here , we will get (Float.MAX_VALUE) and we are
// passing parameter whose value is (0.0f,-7.0/0.0)
System.out.println("StrictMath. nextAfter(f2,d5): " + StrictMath.nextAfter(f2, d5));
// Here , we will get (-2.5 (approx)) and we are
// passing parameter whose value is (-2.6f,0.0)
System.out.println("StrictMath. nextAfter(f1,d2): " + StrictMath.nextAfter(f1, d2));
// Here , we will get (smallest value) and we are
// passing parameter whose value is (0.0f,-7.0/0.0)
System.out.println("StrictMath. nextAfter(f2,d5): " + StrictMath.nextAfter(f2, d5));
}
}
Output
输出量
d1: -2.6
d2: 0.0
d3: -0.6
d4: Infinity
f1: -2.6
f2: 0.0
d5: -Infinity
nextAfter(double, double):
StrictMath.nextAfter (d1,d2): -2.5999999999999996
StrictMath.nextAfter (d2,d1): -4.9E-324
StrictMath.nextAfter (d4,d2): 1.7976931348623157E308
StrictMath.nextAfter (d2,d4): 4.9E-324
nextAfter(float, double):
StrictMath. nextAfter (f1,d3): -2.5999997
StrictMath. nextAfter(f2,d5): -1.4E-45
StrictMath. nextAfter(f1,d2): -2.5999997
StrictMath. nextAfter(f2,d5): -1.4E-45
翻译自: https://www.includehelp.com/java/strictmath-nextafter-method-with-example.aspx
strictmath