Created
April 12, 2013 14:34
-
-
Save jfuerth/5372442 to your computer and use it in GitHub Desktop.
A simple Java program for exploring the distance between consecutive values of type double. See the comment below for a formatted example of what this program outputs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DoubleTrouble { | |
| public static void main(String[] args) { | |
| long actualValue = 10000000000000001L; | |
| //long actualValue = 9999999999999999L; | |
| //long actualValue = Long.MAX_VALUE; | |
| double d = actualValue; | |
| System.out.println("Exact value: " + actualValue); | |
| System.out.println("Nearest double: " + (long) d); | |
| double nextUp = Math.nextAfter(d, Double.POSITIVE_INFINITY); | |
| double nextDown = Math.nextAfter(d, Double.NEGATIVE_INFINITY); | |
| System.out.println("Next larger double value: " + nextUp + " .. distance = " + (nextUp - d)); | |
| System.out.println("Next smaller double value: " + nextDown + " .. distance = " + (nextDown - d)); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When
actualValue = 10000000000000001L, this program outputs:When
actualValue = Long.MAX_VALUE, this program outputs: