View previous topic :: View next topic |
Author |
Message |
roykyn
Joined: 25 Nov 2006 Posts: 9
|
floating point subtraction |
Posted: Thu Nov 30, 2006 2:31 pm |
|
|
can any one help me....a simple floating point subtraction is consuming 20% of rom...i am using pcm and 16f72
float a,b,c;
a=b-c; |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Nov 30, 2006 5:05 pm |
|
|
All starting software engineers run into the same problems using floating point arithmetic when creating their first program on a processor with little memory. They discover one or more of the following:
A) Floating point arithmetic takes a lot of program space.
B) Floating point arithmetic is slow.
C) You get unexpected rounding errors, for example a value of 0.999999 when expecting 1.00000.
A common way to avoid these problems is to apply a technique called 'scaled integers'. This means that you are doing all the arithmetic in integers by multiplying the original data with a factor 10, 1000 or whatever number of digits accuracy you need. Only in displaying the data you add a decimal point.
For example when calculating the distance between two points you can say it is 1.45m + 12,23m = 13.68m
But calculating in centimeters it is all integers: 145cm + 1223cm = 1368cm = 13.68m
Working with integers like this is using less memory, is faster and you get predictable rounding errors.
Also have a look at http://www.ccsinfo.com/forum/viewtopic.php?t=28946&highlight=scaled+integer |
|
|
roykyn
Joined: 25 Nov 2006 Posts: 9
|
|
|
Sherpa Doug Guest
|
|
Posted: Sun Dec 03, 2006 8:58 pm |
|
|
Usually the best way is to avoid using floating point at all. Tell us more about your application and we can probably help you. Also read ckielstra's message carefully. Scaled integers are very powerful and very fast. |
|
|
roykyn
Joined: 25 Nov 2006 Posts: 9
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Dec 04, 2006 8:36 pm |
|
|
That looks like an interesting little device. Basically an oscillator whose frequency is a function of the device being measured. So your measurement enters the software world as an integer count. And you want to end up with integer digits on a LCD display. Why not keep it in integers in the middle? Can you calculate inductance in nHy or uHy depending on the mode? How about calculating capacitance in centi-pF. 100,000 cpF = 1mFd. Then you just have to find out where to put the decimal point on the display. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
roykyn
Joined: 25 Nov 2006 Posts: 9
|
|
Posted: Sat Dec 09, 2006 12:19 pm |
|
|
i have 2 do all the calculations...to find the value of L or C it has only one subtraction.... |
|
|
|