Fixed-point division in C

Status
Not open for further replies.

Elnaz

Newbie level 5
Joined
Mar 22, 2007
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,346
Hi all,

Does anyone know of a division function in C for fixed-point numbers?
I am using ac_fixed header but apparently that doesn't work since I'm getting zeros when I do 1/a where "a" is a fixed-type with values between 30-60.

Thank you,
Elnaz
 

Hi Elnaz,

If a is of type INT (int8, int16 or int32) in the 30-60 range, the result of 1/a will always give zero (e.g. 1/35 = 0,0285714, but INT(0,0285714)=0 ) because of implicit conversion (operation with two INT will give the result of type INT).
If you need to store the result into the variable named Result of type FLOAT, you need to perform explicit conversion:

int a;
float Result;
...
a = 35;
...
Result = (float)(1/a)
...

------------

Regards
 

Hi;
i think
Result = (float)(1/a)
this will give 0 again (difference it is 0.000..)
it should be like that;
Result = 1/(float)a

or

Result = 1.0/a
 

Hi emresel,

You're right.

Result = (float)(1/a)

is not good.
Both

Result = 1/(float)a

and

Result = 1.0/a

are correct.

Regards
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…