Skip to content

Instantly share code, notes, and snippets.

@fogleman
Created July 30, 2018 13:07
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 fogleman/05c69acfd0eb02ade06c31bfcdb75ad4 to your computer and use it in GitHub Desktop.
Save fogleman/05c69acfd0eb02ade06c31bfcdb75ad4 to your computer and use it in GitHub Desktop.
How many floats between -1 and 1?
#include <stdint.h>
#include <stdio.h>
typedef union {
uint32_t a;
float b;
} float_or_int;
int main() {
float_or_int u;
uint32_t between_n1p1 = 0;
uint32_t between_p2p4 = 0;
uint32_t i = 0;
while (1) {
u.a = i;
if (u.b >= -1 && u.b <= 1) {
between_n1p1++;
}
if (u.b >= 2 && u.b <= 4) {
between_p2p4++;
}
if (i == 0xffffffff) {
break;
}
i++;
}
printf("%d\n", between_n1p1);
printf("%d\n", between_p2p4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment