Created
May 2, 2019 16:49
-
-
Save danstur/a59d53fe4cabcf13741aeafb16d801c2 to your computer and use it in GitHub Desktop.
Counts the number of floating point values in [0,0.5) and [0.5,1).
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
private static int count(float start, float end) { | |
int count = 0; | |
float val = start; | |
while (val < end) { | |
count++; | |
val = Math.nextAfter(val, 1); | |
} | |
return count; | |
} | |
public static void main(String[] args) { | |
System.out.printf("There are %d floats in [0,0.5)%n", count(0.0f, 0.5f)); | |
System.out.printf("There are %d floats in [0.5,1)%n", count(0.5f, 1f)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment