Skip to content

Instantly share code, notes, and snippets.

@danstur
Created May 2, 2019 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danstur/a59d53fe4cabcf13741aeafb16d801c2 to your computer and use it in GitHub Desktop.
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).
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