Created
January 17, 2014 09:13
-
-
Save yfuruyama/8470452 to your computer and use it in GitHub Desktop.
convert fixed point number to float point number
This file contains hidden or 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 <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