Java: Get the next floating-point adjacent in the direction of positive and negative infinity from a given float/double number
Next Floating-Point Adjacent to Infinity
Write a Java program to get the next floating-point adjacent to positive and negative infinity from a given floating/double number.
Sample Solution:
Java Code:
Sample Output:
Initial floating number: 0.2 Float 0.2 next down is 0.19999999 Float 0.2 next up is 0.20000002 Initial double number: 0.2 Double 0.2 next down is 0.19999999999999998 Double 0.2 next up is 0.20000000000000004
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Java program to compute the next floating-point number toward positive infinity for a given double without using Math.nextUp().
- Write a Java program to determine the next floating-point value in the direction of negative infinity using bit-level manipulation.
- Write a Java program to compare the results of Math.nextUp() and Math.nextAfter() for various edge-case floating-point values.
- Write a Java program to find the adjacent floating-point number of a given value and verify the result by comparing its bit representation.
Go to:
PREV : Extract Primitive Value from BigInteger.
NEXT : Conditional Statement exercises
Java Code Editor:
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.