View previous topic :: View next topic |
Author |
Message |
MoofCba
Joined: 08 Mar 2004 Posts: 4
|
float Mul |
Posted: Wed Mar 01, 2006 10:34 am |
|
|
Dear Friends:
i have two float numbers. One of them have a lot of decimals.
i just want to mul this two floats, but one of them with only two decimals.
Example
A=12.43233
B=67.2
B=A*B but not 12.43233*67.2, i want 12.43*67.2 how can i do that???.
Thanks i hope you can understand me. I don't have a lot of ROM so if it is a small code is better. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Mar 01, 2006 11:36 am |
|
|
If A is stored as a float it doesn't save any space to truncate it if you still keep it as a float. You could write your own multiplication routine that would only do the first few digits but I doubt you would save any space.
Perhapse a better question is whether you need floating point at all. If you are calculating a temperature of 27.3 degrees you may do all the math in integer hundredths of a degree to get 2730. Then when you print the result you just put a decimal point where it is needed using string manipulation. In my main product I measure the hull of ships in millimeters. I use a 24 bit intger format. If I calculate a ship is 203456 mm long I print the result as 203.456 meters without using floating point math at all.. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah Guest
|
|
Posted: Thu Mar 02, 2006 6:03 am |
|
|
Worth adding here, that the compiler now has it's own ability to insert a decimal point, for output, and integer code, is both smaller, and faster than fp. Ideally of course, if you could 'work back', and not go float at all, since as it stands, there is the overhead of converting to integer. However as it stands:
Code: |
int32 A32;
A32 = a*100;
A32 *= (int32)(b*100);
printf("%6.4w",A32);
|
Best Wishes |
|
|
|