Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Floating point C to Fixed point C conversion

Status
Not open for further replies.

gurpreet

Newbie level 4
Joined
Aug 31, 2005
Messages
7
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,396
Can anyone tell me, How can I convert a floating point C-program to fixed point one?

~gurpreet
 

It can be as simple as declaring all your variables as int instead of float but many other things need to be taken into account before you proceed doing this.
Like If your program contains multiplications involving decimals less than one then you need to scale them up by some constant factor do your manipulations then scale down the result.
This is possible only if your input is integer and intermediate manipulations involve decimal constants.
This scaling would obviously introduce errors in the form of rounding off to the nearest integer. This error can be minimised by scaling your decimals appropriately. But too much scaling would also cause problems like overflow and saturation so you need to decide what your fixed point precision needs to be based on your scaling factor.
So all you have is error due to rounding off (when you have a low scaling value) and saturation and overflow errors due to Fixed point precision (due to high scaling) and a balance needs to be striked in choosing the scale factor based on your application.
Further high scaling value may also force you to use higher precision like long etc. but this may involve more computation.
Hope an example would clarify it more,
Assume a module which calculates the 'y' in y = mx+c given x where m and c are decimal constants , m = .15, c = 30.5.

Let input x always be an integer say = 250,
Then y if using the floating point would be = 250*.15+30.5 = 68

NOw converting constants to integers by scaling factor(sf) = 100,so c = 3050 and m = 15 (thus m and c are in integers).
Calculate y1 = (15*250+3050) = 6800.
Since the constants are scaled the out put needs to be scaled down = 6800/100 (this too is done as integer) = 68.
Luckily in this case there was no rounding off errors. But in most cases it would accur.

Hope this was useful,
KJN
 

Thanks for this. But I'm mainly interested in some cross compiler or like that. Because you see it will create problem for long and complex programs.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top