Skip to content

Instantly share code, notes, and snippets.

@tmathmeyer
Last active August 29, 2015 13:57
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 tmathmeyer/9718294 to your computer and use it in GitHub Desktop.
Save tmathmeyer/9718294 to your computer and use it in GitHub Desktop.
floats are so stupid
#include <stdio.h>
#include <stdlib.h>
char* int2bin(unsigned x){
char* res = malloc(32);
int i = 32;
while(i>0){
i--;
res[31-i] = ((1<<i)&x) ? '1' : '0';
}
return res;
}
union FL_UI_Conv {
unsigned u;
float f;
};
int main(int argc, char* argv)
{
int a = 1065353215;
int b = 1065353217;
union FL_UI_Conv A;
union FL_UI_Conv B;
A.f = (float)a;
B.f = (float)b;
printf("float a binary: %s\n", int2bin(A.u));
printf("float b binary: %s\n", int2bin(B.u));
printf("converted back to int: %i\n", (int)A.f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment