synkro
0xdeadbeef
Code:
#ifndef __fixedmath_h__
#define __fixedmath_h__
typedef long fixed; // 16.16 fixed point
typedef unsigned short Iangle; // Integer angle (0..1023)
#define MAX_TRIG 1024
#define MAGIC_TRIG 162.974 // degree precision for LUTs
// trigo LUTs
fixed SinTab[MAX_TRIG];
fixed CosTab[MAX_TRIG];
// fixed conversion
#define INT2FIXED(x) ((x) << 16)
#define FIXED2INT(f) ((f) >> 16)
//#define FIXED2FLOAT(f) (((double) (f)) * 1.52587890625e-5)
#define FIXED2FLOAT(f) ((float) ((f) / 65536.0))
#define FLOAT2FIXED(f) ((fixed) ((f) * 65536.0))
#define DOUBLE2FIXED(x) ((x) * 65536.0)
// fixed constants
#define FIXED_ZERO (INT2FIXED(0))
#define FIXED_ONE (INT2FIXED(1))
#define FIXED_ONE_HALF (FIXED_HALF(FIXED_ONE))
#define FIXED_PI 205887L
#define FIXED_2PI 411775L
#define FIXED_HALF_PI (FLOAT2FIXED(1.57079633))
#define FIXED_E 178144L
#define FIXED_ROOT2 74804L
#define FIXED_ROOT3 113512L
// fixed functions
#define FIXED_DIV(a,b) ((fixed) (((double) (a) / (double) (b)) * 65536.0))
#define FIXED_MUL(a,b) ((fixed) ((double) (a) * ((double) (b)) * 1.52587890625e-5))
#define FIXED_ABS(f) ((f) < 0 ? -(f) : (f))
#define FIXED_TRUNC(f) ((f) & 0xffff0000)
#define FIXED_SIGN(f) ((unsigned int) (f) >> 31)
#define FIXED_PRODUCT_SIGN(f, g) ((unsigned int) ((f) ^ (g)) >> 31)
#define FIXED_HALF(f) ((f) / 2)
#define FIXED_DOUBLE(f) ((f) << 1)
#define FIXED_ROUND2INT(f) (((f) & 0x0000FFFF) > 32768 ? ((f) >> 16) + 1 : (f) >> 16)
// output
#define Printfx(x) sprintf(string, "%ld.%ld", x >> 16, 10000 * (unsigned long) ((x) & 0xFFFF) >> 16)
#define NDPrintfx(x) sprintf(string, "%ld", x >> 16)
#endif
I have trouble with my fixed point math (16.16). It seems
that I have a heavy lost in precision on some operations.
So the error must be in the float<->fixed conversion or in
the MUL and DIV. I give you an example, because sometimes
it works perfect and sometimes not....
Code:
fixed fx1 = FLOAT2FIXED(-20.333);
fixed fx2 = INT2FIXED(100);
fixed fx3 = FIXED_MUL(fx1,fx2);
Printfx(fx3);
gp_drawString (1, 1, 10, string, GP32RGB(30,30,30), framebuffer[nflip]);
Buit when I enter:
Code:
fixed fx1 = FLOAT2FIXED(-20.333);
fixed fx2 = INT2FIXED(100);
fixed fx3 = FIXED_MUL(fx1,fx2);
fx3 = -2034.7007 (where do I loose that much?)
but even the DIV sometimes works perfect, sometimes
not...
Code:
fixed fx1 = FLOAT2FIXED(270.5);
fixed fx2 = INT2FIXED(20);
fixed fx3 = FIXED_DIV(fx1,fx2);
but when I try to calculate 1/100
Code:
fixed fx1 = INT2FIXED(1);
fixed fx2 = INT2FIXED(100);
fixed fx3 = FIXED_DIV(fx1,fx2);
Help! I must doing something wrong...