floating point math
JazzD
Posts: 13
In my project i need to do some calculations using floating point math. Which are for example calculating the speed of a car from a frequency value (PI is used, so you need floats to get a decent precision). Well it will also feature getting ADC values, reading NMEA(GPS) data, VGA, and the main program. Now before i start putting stuff together im looking at the number of cogs that I need to program this. As I'm still quite new to spin I dont always get when a cog is used. Is it only when you call cognew in the code or can some files still use a cog? Now as I'm planning my source I see that i already use like 6-7Cogs without the float math. So I looked at the floating point math code by chip gracey and im not really sure if you NEED a cog for it? Can somebody explain this to me. Btw, the floating point operations i need is multiplication,division,addition,subtraction and of course precision!
Comments
This code here gives tenth mph precision from 0 to 180mph or so, from a vss input for example. ppm=pulses per mile / pulselength=4 pulses
360000/(data[noparse][[/noparse]PPM]*pulselength/4000)
The output will be in tenths. Adding an artificial decimal point is no problem.
Now I can duck the flying shoes while someone else extols the virtues of floating point.
1000/300*20=60 in integer math = 66.66 in decimal math
1000*20/300=66 in integer math
You always want to do your multiplication first, if possible, if the intermediate result is not greater than 31 bits.
When it comes right down to it, binary is still either a 1 or 0 , not .%010101 (duck the shoes here)
For simple requirements, you do not need to dedicate a cog for floating point, since Chip has written a SPIN based fp library (called FloatMath) that is distributed with the Prop Tool. It does the basics, plus sqr, some conversions. It won't have the speed of the PASM code, but it will get the job done without dedicating hardware to the task.
Does this help?
Cheers!
Paul Rowntree