Fixed Point Problems

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fixed Point Problems

581 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Caradhras on Tue Apr 03 01:44:43 MST 2012
Hi,

i got a simple question, regarding a fixed point division on a LPC11C24. I think the problem should be device independant and sitting before my keyboard.

I got this simple example code, i found on another arm-forum:
typedef int32_t fx_t;

#define FX_FRAC 12        // define position of decimal point
// explanation: FX_FRAC = 12 ==> 20 Bit for Integer-Value, 12 for fractional value

#define fx_float(a) (a / (float)(1LL<<FX_FRAC))
#define fx_make(a)  ((fx_t)(a * (1LL<<FX_FRAC)))
#define fx_add(a,b) (a + b)
#define fx_sub(a,b) (a - b)
#define fx_mul(a,b) ((fx_t)(((int64_t)a * b) >> FX_FRAC))
#define fx_div(a,b) ((fx_t)(((int64_t)a << FX_FRAC) / b))

uint32_t zahl = 934;
zahl = fx_make(zahl);
uint32_t teiler = 33;
teiler = fx_make(teiler);
uint32_t ergebnis = 0;
ergebnis = fx_div(zahl, teiler);
The integer-part of the result is fine, it is indeed 28. But the fractional part does not have anything in common, what my TI-pocket-calculator says... :confused:

Im pretty new to fixed point math, so is an overflow or something? I got 20 Bits for Integer and 12 Bits for fractional part as precision is not so important.

Thanks and regards!
0 Kudos
Reply
2 Replies

564 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Caradhras on Mon Apr 09 15:44:09 MST 2012
Hi,

sorry for not reporting back.  Thanks for the help, it was just that easy. :rolleyes:
0 Kudos
Reply

564 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Leji on Tue Apr 03 08:57:40 MST 2012
it does work as expected for me, I get 0x1C as integer and 0x4D9 as fractional part, which is:
2^-2+2^-5+2^-6+2^-8+2^-9+2^-11 ~= 0.303
which is what my TI calculator says.

what's the issue exactly ?
0 Kudos
Reply