Created
July 30, 2018 13:07
-
-
Save fogleman/05c69acfd0eb02ade06c31bfcdb75ad4 to your computer and use it in GitHub Desktop.
How many floats between -1 and 1?
This file contains 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
#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