Shop OBEX P1 Docs P2 Docs Learn Events
Rounding Numbers with the Propeller — Parallax Forums

Rounding Numbers with the Propeller

coryco2coryco2 Posts: 107
edited 2011-03-23 19:12 in Propeller 1
Is there any simple way to round a run-time variable floating point number to the nearest integer on the Propeller? I'm looking for something that will work like ROUND does for constants.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-03-23 12:28
    For positive numbers, add 0.5 to it before truncating to an integer; for negative numbers, whether you add or subtract 0.5 depends on whether conversion to an integer truncates up or down.

    -Phil
  • coryco2coryco2 Posts: 107
    edited 2011-03-23 14:10
    Uh...well then how do I truncate it to an integer? TRUNC also is only for compile-time constants, not run-time variables, correct?
  • SSteveSSteve Posts: 808
    edited 2011-03-23 15:09
    The Propeller doesn't have built-in floating point support. Which floating point package are you running?
  • Martin_HMartin_H Posts: 4,051
    edited 2011-03-23 15:53
    In order to do any run time floating point math on the propeller you'll need to use a math library. I am using this one:

    http://forums.parallax.com/showthread.php?127302-F32-Concise-floating-point-code-for-the-Propeller

    and I like it. For that one you would use code like "rounded = f32.FRound( inputVal )"
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-03-23 16:31
    Oh, my bad. I assumed he was already using a floating point library.

    -Phil
  • coryco2coryco2 Posts: 107
    edited 2011-03-23 18:22
    Ah, that explains it. I misunderstood and assumed the Propeller had floating point support built-in. So what does the Propeller on its own do with fractional results for math operations? Just truncate/discard them, or round them or what?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-23 18:29
    The Propeller Tool can do floating point calculations with constants, but the resulting values look like large 32-bit numbers at run-time because the Propeller Tool doesn't automatically convert them back to ordinary integers. It leaves them as floating point numbers which use the IEEE format (see here).
  • SSteveSSteve Posts: 808
    edited 2011-03-23 19:11
    coryco2 wrote: »
    So what does the Propeller on its own do with fractional results for math operations? Just truncate/discard them, or round them or what?

    When you divide it truncates. Use the modulus operator (//) to get the fractional part.
  • Martin_HMartin_H Posts: 4,051
    edited 2011-03-23 19:12
    coryco2, the built in math operations are integer and the results are truncated to the next lower integer multiple. Even if you convert the result to float the truncation error can produce bad results. This happened to me with a ACos call which I pointed out in this thread.

    http://forums.parallax.com/showthread.php?130486-Spin-Pop-Quiz-what-s-wrong-with-the-SetPosition-method.

    Another thing to be careful about is that the Propeller will happily use integer operations on floating point numbers. Since Spin is loosely typed it won't complain, but the results will be wrong. If you look in that code in my thread you'll see I carefully convert back and forth when I need to. As Mike points out, you can pre-compute floating point constants using the Spin complier. I did that for various radian angle constants as they are all fractions of Pi.
Sign In or Register to comment.