Shop OBEX P1 Docs P2 Docs Learn Events
Floating point vs non-floating point — Parallax Forums

Floating point vs non-floating point

grasshoppergrasshopper Posts: 438
edited 2008-12-27 10:32 in Propeller 1
I am writing a program that uses some mathematical calculations and figured I would ask as the subject line states.

I think that it is faster to use non floating point math to make my calculations. This would have to be accomplished like the example:


say I need a number 10.99 ...
...I multiply 10.99 by 100 =  1099 This in effect makes 10.99 without the "."





I would employ this type of "keeping my decimal place" throughout my code. Again is this faster or just a waste of time?

Comments

  • DiverBobDiverBob Posts: 1,108
    edited 2008-12-26 05:25
    That's pretty much the technique I am using for a project I am working on now. I only convert the end value to floating point and divide by 100 just before displaying the value. It just seems easier to deal with the numbers as integers until I need to display them. I don't know if it is faster (logically it seems it should be faster not dealing with the extra steps needed for FP) but it seems to be working so far!

    Bob Sweeney
  • JonathanJonathan Posts: 1,023
    edited 2008-12-26 06:29
    Integer math is always faster. I'm not sure how much faster with the Prop, but floats will always cause a performance hit. In general I try to stay all integer, but the Props floating point is so tempting, especially float to string that I have to admit I use it when I could indeed do integer.

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • soshimososhimo Posts: 215
    edited 2008-12-26 07:04
    You could write some fixed point routines (the technique you described is called fixed point representation). The only drawback behind fixed point is the trade off between storage size, accuracy, and max/min values. For example, if you use 32 bits to store the fixed point number you have to decided how many bits will represent the fractional part. Anything lower than this amount will result in an underflow. The converse is true for the most significant bits and the maximum values. If you decided to use 24 bits to store the non fractional part the largest value you can represent is $7FFFFF (8388607 decimal). This of course only leaves 8 bits for the fractional part. The biggest advantage of fixed point over floating point is the accuracy within it's range. Floating point is notoriously inaccurate, especially the smaller you get. Fixed point maintains the same accuracy across all it's range. Floating point does have a much larger range though.
  • LeonLeon Posts: 7,620
    edited 2008-12-26 11:48
    I used both fixed- and floating-point in a Kalman tracking application for a fixed-point DSP - fixed-point for speed where I didn't need the dynamic range, and floating-point where I did.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • KeezinatorKeezinator Posts: 21
    edited 2008-12-26 14:41
    Fixed point is always a LOT faster. So if you don't need floating point, don't use it if speed is an issue.

    In the prop it is even worse, because the FP is done in software (where your PC has a dedicated FP processor).

    Yet, the huge range, the don't having to worry about the place of the decimal point and the availability of math functions like raising to the power, square roots etc. make me using floating point wherever I don't need the added speed or the extra free memory.

    And in many cases, the speed is not so relevant anyway or you can easily optimize your code to make it irrelevant.

    Just don't put FP math inside a loop needing tight timimg...
  • parskoparsko Posts: 501
    edited 2008-12-26 16:08
    Grasshopper,

    Another thing about it is the level of accuracy. You can lose track of your significant digits when doing it with integers. It helps to lay the whole calculation out in an excel spreadsheet (if it's a long formula), so you can simulate the range of your final results. For instance, if you only use 8 bits from an ADC, you only have 0-255. You can use this as your input and see how the output will look with each sample point after you have gone through your formula.

    With integer, I would work with numbers less than 2^16 (65536?), which will handle (255*100=25500). This assumes you need to keep your values within a size to work with assembly multiplication and divide routines (2^16).

    If you work with spin, you might be able to work with larger numbers.

    -Parsko
  • grasshoppergrasshopper Posts: 438
    edited 2008-12-26 16:11
    So some of you mention the speed but its not computing to me.

    How many clock cycles are added just to do Floating point? Better yet how much speed do you loose using Floating point.

    I am currently using Float32 so in effect its running in a cog. Will this help the speed issue?
  • kwinnkwinn Posts: 8,697
    edited 2008-12-26 17:08
    Grashopper, I am afraid that speed for both integer and floating point math is not as simple as counting clock cycles. The amount of time it takes depends on the operation (add, sub, mult, div) and the actual numbers involved. For instance, to multiply 2 16 bit integer numbers would require anywhere from 0 to 16 shifts and adds.
    Floating point takes even more time since it involves splitting the numeric representation into 2 parts. For instance your number 10.99 would be stored as .1099 (mantissa) and 2 (exponent), that is .1099 x 10^2. (I am using normalized base 10 rather than binary for clarity). Any floating point operation on two numbers involves separate operations on both parts of the number.
    See http://www.psc.edu/general/software/packages/ieee/ieee.php for a more detailed explanation or google "floating point" and have some fun.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-26 17:34
    grasshopper,
    Read the documentation. There's a lot of information there on floating point operation speed.

    Float32 is not designed to function in parallel with other cogs. When your program "asks" the cog to do the floating point operations, it waits until the cog is done before continuing. The use of the extra cog is just to get the speed advantage of the native Prop instruction set.
  • AribaAriba Posts: 2,687
    edited 2008-12-27 10:32
    With Spin it's so easy to make a Testcode to find out the answer for such a question:
    {{ Speed Test }}
    CON
      _clkmode        = xtal1 + pll16x
      _xinfreq        = 5_000_000
    
    VAR
      long  time1, time2
      
    OBJ
      fp  : "Float32"
      trm : "TV_Text"    'or PC_Text or VGA_Text
    
    PUB main | i,f
      trm.start(12)      'or 16 for VGA
      fp.start
      waitcnt(clkfreq>>1+cnt)
    
      time1 := cnt
      i := 199           'write your Integer test formula here
      i := i * 55 + 7
      time1 := cnt - time1
    
      time2 := cnt
      f := 199.0         'write your Float test formula here
      f := fp.FMul(f,55.0)
      f := fp.FAdd(f,7.0)
      time2 := cnt - time2
    
      trm.str(string("Timing results:",13))    'show the results
      trm.dec(time1)
      trm.str(string(" clocks for Integer",13))
      trm.dec(time2)
      trm.str(string(" clocks for Float",13))
      trm.str(string("Faktor: "))
      trm.dec(time2/time1)
      trm.out(".")
      trm.dec(time2*10/time1 //10)
      repeat
    
    



    Andy
Sign In or Register to comment.