|
|
View previous topic :: View next topic |
Author |
Message |
jj Guest
|
Fixed Point Division |
Posted: Fri Nov 11, 2005 5:21 pm |
|
|
Could someone give me some hint where to find some information on fixed-point division for ccs - thank you ! |
|
|
Ttelmah Guest
|
|
Posted: Sat Nov 12, 2005 3:45 am |
|
|
CCS, does not have a fixed point arithmetic ability. All CCS has, is a 'fixed point' display. Basically, you can take an integer, and display it, as if it has a number of fixed decimal places.
The actual arithmetic stays normal integer arithmetic. So, you have to handle the 'scaling' involved in multiplication/division yourself.
So if (for instance), you a working to 2DP, you would need:
Code: |
int32 value1=10000;
//100.00 in 2DP format
int32 value2=200;
//2.00 in 2DP format
int32 temp;
//Multiply
temp=(value1*value2)/100L;
printf("%6.2w\n",temp;
//Displays '200.00'
//Divide
temp=(value1*100L)/value2;
printf("%6.2w\n",temp;
//Displays '50.00'
|
Provided you stay inside the maximum value allowed in an int32, even with the extra factor of 100 (for 2DP) involved, then this, by always generating the larger result first (rather than dividing first), should generate the correct values.
Best Wishes |
|
|
jj Guest
|
|
Posted: Sat Nov 12, 2005 4:15 am |
|
|
thank you for the answer - and I'm sorry that I stated not clearly enough my topic .
I have to cope with 16.16 fixed-point arithmetic. To my pleasure ;) the numbers could be unsigned ....
Now : It's rather clear how to multiply two 16.16 fixed-point numbers - anyway it would be much nicer if someone has already written a nice lib and woul be able to share it ;)
But what remains rather unclear to me is how to implement the division of two 16.16 fixed-point numbers and how to implement that ....
jj |
|
|
Ttelmah Guest
|
|
Posted: Sat Nov 12, 2005 4:52 am |
|
|
If by '16.16', you mean 32 decimal digits, then you have a lot of work to do, and the processing times involved are going to be horrendous. You are going to need about 107bit values, to give this sort of accuracy. If instead you mean to split the binary value like this, you should remember that if you need a sign, you only have 31bits in a 32 bit integer. Give that CCS has support for the output in decimal, using the %w format, for a fixed _decimal_ shift, I'd suggest reconsidering, and perhaps looking at working with perhaps .4 decimal, rather than trying to symmetrically split the binary value. However you would probably need to use a more sophisticated method of dealing with the potential overflows. Look at:
<http://www.accu.org/acornsig/public/caugers/volume2/issue6/fixedpoint.html>
Which shows the standard arithmetic re-arrangement needed to avoid this.
The same code, could be used wth a binary split on the numbers, but you will then need to write the output routine yourself.
Best Wishes |
|
|
jj Guest
|
|
Posted: Sat Nov 12, 2005 5:27 am |
|
|
ähem, no - by 16.16 I mean 16 bits for the integer parts - this means around 4-5 decimal digits and 16 bits for the real part this means 4-5 digits after the "colon" ... |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Sat Nov 12, 2005 12:07 pm |
|
|
Fixed point binary is in principle no different than fixed point decimal.
You are looking for an implied binary point so your 32 bit field is split 16.16. Suppose you have decimal 1.5 which is 0...01.10...0 where 0...0 means fifteen zeroes. All input data will need to be normalized to this 16.16 format. You can then use the CCS 32 bit integer math to multiply and divide ,add and substract these normalized to 16.16 numbers. To move back to decimal for displaying results you could roll your own or use CCS functions such as printf being aware of the 2^16 offset in the result due to your 16.16 normalized implied binary point. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|