A poc for fixed point calculations
This commit is contained in:
parent
ee8728f2d7
commit
28346edaf8
1 changed files with 46 additions and 0 deletions
46
fixed.c
Normal file
46
fixed.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include <stdio.h>
|
||||
|
||||
typedef int fixed;
|
||||
#define FIXD (sizeof(fixed)*4)
|
||||
|
||||
fixed mpy(fixed a, fixed b) {
|
||||
return (fixed) (((long)a * (long)b) >> FIXD);
|
||||
}
|
||||
|
||||
fixed div(fixed a, fixed b) {
|
||||
return (fixed) (((long)a << FIXD) / b);
|
||||
}
|
||||
|
||||
fixed add(fixed a, fixed b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
fixed sub(fixed a, fixed b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
fixed fromdouble(double a) {
|
||||
return (fixed) (a * (long)(1 << FIXD));
|
||||
}
|
||||
|
||||
double todouble(fixed a) {
|
||||
return (double)a / (long)(1 << FIXD);
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] ) {
|
||||
|
||||
double af = 1.0f;
|
||||
fixed a = fromdouble(af);
|
||||
double bf = 2.0f;
|
||||
fixed b = fromdouble(bf);
|
||||
|
||||
fixed c = div(a,b);
|
||||
c = div(c,b);
|
||||
double cf = todouble(c);
|
||||
|
||||
printf("%f\n",cf);
|
||||
|
||||
printf("%f\n",todouble(1));
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue