Skip to content

Instantly share code, notes, and snippets.

@cwvh
Created August 11, 2010 21:21
Show Gist options
  • Save cwvh/519799 to your computer and use it in GitHub Desktop.
Save cwvh/519799 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/* IEEE-754 values taken from page 2 of:
* "Introduction to Floating point calculations and IEEE 754 standard"
* Jamil Khatib, August 10, 2000
*/
typedef uint32_t uint;
const uint shift = 32-8-1; /* 4*sizeof(float) - exp bits - sign bits */
const uint mask = 0xff;
const uint bias = 126; /* exp bias - 1 */
int main(int argc, char **argv)
{
float f, frac;
int x, e, n;
f = 3.14159f;
if (argc > 1)
f = atof(argv[1]);
x = *(uint*)&f;
e = (x>>shift)&mask - bias;
if (e < 32-8)
x &= ~(1<<(32-8-e) - 1);
n = *(float*)&x;
frac = f - n;
printf("%d, %f\n", n, frac);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment