one-file-projects/fixed.c

47 lines
767 B
C
Raw Permalink Normal View History

2016-03-17 11:55:46 +01:00
#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;
}