Home »
Java programming language
Java Math Class static float nextAfter(float starts , double directions) with example
Java Math Class static float nextAfter(float starts , double directions) method: Here, we are going to learn about the static float nextAfter(float starts , double directions) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 07, 2019
Math Class static float nextAfter(float starts , double directions)
- This method is available in java.lang package.
- This method is used to return the float floating-point number adjacent to the first parameter (starts) in the direction of the second parameter (directions).
- Let suppose both arguments passed in the method are equivalent so in that case the second parameter is returned.
- This is a static method, so it is accessible with the class name too.
- The return type of this method is float, it returns the float floating-point number adjacent to start in the direction of the second argument.
- In this method, we pass two parameters first is of float type and second one is of double, so the first parameter represents the initial or starting floating-point value and the second parameter represents the value denoting which of the given first parameter neighbor (Starts neighbor) or start is returned.
- This method does not throw any exception.
Syntax:
public static float nextAfter(float starts , double directions){
}
Parameter(s):
- starts – represents the initial or starting floating-point value.
- directions – represents the value denoting which of the given first parameter neighbor (starts neighbor).
Return value:
The return type of this method is float, it returns the double floating-point number adjacent to the first parameter (starts) in the direction of second parameter (directions).
Note:
- If we pass "NaN" (Not a Number), it returns the same i.e. "NaN".
- If we pass the same value in both of the parameters, it returns the same value.
- If we pass "float.MIN_VALUE" as the first parameter and second parameter holds another value, it returns smaller value i.e. the same value is with the same sign as the first parameter.
- If we pass infinity as first parameter and second parameter holds another value, it returns the "float.MAX_VALUE" with the same sign as the first parameter.
- If we pass "float.MAX_VALUE" as the first parameter and second parameter holds another value, it returns the largest value with the same sign as the first parameter.
Java program to demonstrate example of nextAfter(float starts , double directions) method
// Java program to demonstrate the example of nextAfter
// (float starts , double directions) method of Math Class.
public class NextAfterFloatTypeMethod {
public static void main(String[] args) {
//declaring variables
float f1 = -2.6f;
float f2 = 0.0f;
double d3 = 0.0;
double d4 = -7.0 / 0.0;
// displaying the values
System.out.println("f1: " + f1);
System.out.println("f2: " + f2);
System.out.println("d3: " + d3);
System.out.println("d4: " + d4);
// Here , we will get (-2.5 (approx.)) because we are passing
// parameter whose value is (-2.6f,0.0)
System.out.println("Math.nextAfter (f1,d3): " + Math.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("Math.nextAfter (f1,d3): " + Math.nextAfter(f2, d4));
// Here , we will get (-2.5 (approx)) and we are passing
// parameter whose value is (-2.6f,0.0)
System.out.println("Math.nextAfter (f1,d3): " + Math.nextAfter(f1, d3));
// Here , we will get (smallest value) and we are passing
// parameter whose value is (0.0f,-7.0/0.0)
System.out.println("Math.nextAfter (f1,d3): " + Math.nextAfter(f2, d4));
}
}
Output
E:\Programs>javac NextAfterFloatTypeMethod.java
E:\Programs>java NextAfterFloatTypeMethod
f1: -2.6
f2: 0.0
d3: 0.0
d4: -Infinity
Math.nextAfter (f1,d3): -2.5999997
Math.nextAfter (f1,d3): -1.4E-45
Math.nextAfter (f1,d3): -2.5999997
Math.nextAfter (f1,d3): -1.4E-45