Skip to content

Instantly share code, notes, and snippets.

@yfuruyama
Created January 17, 2014 09:13
Show Gist options
  • Save yfuruyama/8470452 to your computer and use it in GitHub Desktop.
Save yfuruyama/8470452 to your computer and use it in GitHub Desktop.
convert fixed point number to float point number
#include <stdio.h>
#include <math.h>
#define FIXED_BIT 12
float fix2float(unsigned short int n)
{
float ret = 0.0;
int integer_part;
int i;
integer_part = (n >> 12);
ret += integer_part;
for (i = 0; i < FIXED_BIT; i++) {
ret += ((n >> (FIXED_BIT - i - 1)) & (0x01)) * (1.0 / pow(2, i+1));
}
return ret;
}
int main()
{
unsigned short int n;
n = 10240; // 2.5
printf("%d => %f\n", n, fix2float(n));
n = 10752; // 2.625
printf("%d => %f\n", n, fix2float(n));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment