diff --git a/fixed.c b/fixed.c new file mode 100644 index 0000000..4ff5ddb --- /dev/null +++ b/fixed.c @@ -0,0 +1,46 @@ +#include + +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; +}