Shop OBEX P1 Docs P2 Docs Learn Events
floating point math — Parallax Forums

floating point math

JazzDJazzD Posts: 13
edited 2009-01-28 19:49 in Propeller 1
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

  • Erik FriesenErik Friesen Posts: 1,071
    edited 2009-01-28 13:22
    Are you sure you need floating point? If you get your spreadsheet out and do some figuring a lot of times in won't be necessary.

    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.
  • JazzDJazzD Posts: 13
    edited 2009-01-28 13:40
    you are totally right, that's kind of why im asking this. the thing is that im also wanting to calculate the average speed, so i need to save lots of speed records in an array and divide them by the number of records, who knows if the value will still be precise then? Will it?
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2009-01-28 14:28
    2,147,483,648/1800=1,193,046 . What this means to me is that you could store over one million speed records at 180 mph before you would lose precision. The reason I have used a spreadsheet is to plot each calculation under max possibility. You may have to change your formula to maximize the result. For example

    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 smile.gif (duck the shoes here)
  • TreeLabTreeLab Posts: 138
    edited 2009-01-28 17:42
    Jazzed;
    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
  • JazzDJazzD Posts: 13
    edited 2009-01-28 19:49
    okay i will have a look, doesnt look so bad then [noparse]:)[/noparse]
Sign In or Register to comment.