Shop OBEX P1 Docs P2 Docs Learn Events
rounding numbers — Parallax Forums

rounding numbers

DestroyerXDestroyerX Posts: 34
edited 2005-02-22 14:43 in BASIC Stamp
Is there a command in PBASIC 2.5 that will round a given number to an integer?

Thanks,

Jason

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox

Comments

  • BorisBoris Posts: 81
    edited 2005-02-21 18:14
    I think BS only work with integers. How did you get a floating point value?
  • Robert SchwartzRobert Schwartz Posts: 141
    edited 2005-02-21 18:23
    PBASIC doesn't do floating point arithmatic, all numbers are truncated. Also, all numbers are unsigned, so you can't have negative numbes, only whole numbers.
  • DestroyerXDestroyerX Posts: 34
    edited 2005-02-21 19:02
    If I multiply two numbers and get a decimal, then use that number in say the duty for PWM, will the decimal be rounded or will the PWM just refuse to take something that is not a whole number between 0 and 255?

    Thanks again,

    Jason

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Unreal tournament 2004 rox
  • Robert SchwartzRobert Schwartz Posts: 141
    edited 2005-02-21 19:11
    The decimal will be truncated. 1.2 becomes 1, 1.999999 becomes 1. The PWM will take the truncated number.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-02-21 19:16
    When you multiply two numbers on the BASIC Stamp, you get an integer, no matter what.· The only way to work with floating point math is to use a floating-point co-processor, such as the uFPU.· Even then, the PWM command only accepts an integer·value of 0-255 for it's 2 arguments.




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --==<{Chris}>==--
  • DestroyerXDestroyerX Posts: 34
    edited 2005-02-21 19:29
    I'm multiplying decimals and fractions. How would those values affect the BS2?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Unreal tournament 2004 rox
  • achilles03achilles03 Posts: 247
    edited 2005-02-21 20:01
    Back to the original question: how are you getting decimals and fractions in the BS2's programming to begin with? If possible, cut and paste some code and tell us where those numbers are coming from.

    Dave
  • DestroyerXDestroyerX Posts: 34
    edited 2005-02-21 20:53
    I'm taking a value of an input pulse that is between 0 and 205 and multiplying it by 54/41 to get a value between 0 and 255 that can be used for the duty in PWM. For example, if the input pulse is 25, then the new value is 1350/41 (or ~32.93) after multiplying by 54/41.

    I was asking about the rounding to integers before, because the duty for PWM supposedly can't be an integer.

    Thanks,

    Jason

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Unreal tournament 2004 rox
  • BorisBoris Posts: 81
    edited 2005-02-21 21:23
    in BS2:
    25*54=1350
    1350/41=32 NOT (~32.93)
  • DestroyerXDestroyerX Posts: 34
    edited 2005-02-21 21:26
    I'm sorry, I wrote it incorrectly.

    It is 25 * (51/41)

    I meant to originally say 51 instead of 41 and also have (51/41) in parenthesis.

    -jason

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Unreal tournament 2004 rox

    Post Edited (DestroyerX) : 2/21/2005 9:39:03 PM GMT
  • BorisBoris Posts: 81
    edited 2005-02-21 22:13
    ok, instead of retyping the manual here, why dont you just read it?
    http://www.pond.ie/pdf/BStamp.pdf
    read pages 231 (PDF doc page 39) to 234 (PDF doc page 42)
    sorry Parallax people, I searched BS2 manual in google and that was the 1st link·to come up. Its not from Parallax 's website but its the same manual.

    Remember: "google is your friend" [noparse]:)[/noparse]
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-02-21 22:13
    Jason,

    ·· You can do that math in the BS2, but the result will be an integer.· It will be 25, I believe.· So you don't have to worry about the decimal places...



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --==<{Chris}>==--
  • BorisBoris Posts: 81
    edited 2005-02-21 22:23
    the result of this:
    25 * (51/41)
    would be 25*51=1275
    1275/41=31
    "The BS2 solves math problems in the order they are written-from left to right. The results of each operation is fed into the next operation" Pg233
    I dont think parantneses make a difference.
  • DestroyerXDestroyerX Posts: 34
    edited 2005-02-21 22:39
    Oh, okay, thanks! That's valuable information. I think I figured out a way to avoid using demicals in my project and still have similar functionality.

    Thanks for all of your help everyone.

    Jason

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Unreal tournament 2004 rox
  • Robert SchwartzRobert Schwartz Posts: 141
    edited 2005-02-21 22:43
    The bs2 does use parenthases. Bs1 does not. So:
    25 * (51/41)
    25 * (1)
    =25
  • achilles03achilles03 Posts: 247
    edited 2005-02-22 03:26
    Also, if you want to give it round up capabilities, then just determine if the remainder (see the // function) of whatever you're dividing by is greater than half. For instance, if you're doing x*51/41, then you can give it rounding capabilities by doing something like the following:

    x VAR Byte
    y VAR Word

    y=x*51/41
    if (x*51)//41<20 THEN NOROUNDUP
    y=y+1
    NOROUNDUP:
    'next actions go here

    Hope that helps,
    Dave
  • Fe2o3FishFe2o3Fish Posts: 170
    edited 2005-02-22 09:09
    Dave (achilles03),
    Why bother "testing" and then still doing two divides (the modulo op usually does
    one)? Well, it may be more "readable" but try just adding half of your denominator
    to your numerator and then just divide. The answer will be rounded up if
    necessary. For example:

    ' 3*51/41 ==> 3.73... (round up)
    x = 3
    y = (x * 51 + 20) / 41    '(153+20)/41 = 4
    



    To see what this is doing just expand the two terms:

    (x*51/41) + (20/41)

    The second term just adds that "rounding-up 1/2" (well, next best thing since the
    denominator is odd) to the original division. If said division would have wound up
    a candidate for rounding up, then the extra 20/41 would increment the integer
    division's answer for rounding.

    Let's try another example:

    ' 2*51/41 ==> 2.4878... (don't round up)
    x = 2
    y = (x * 51 + 20) / 41   ' (102+20)/41 = 2
    



    It's a math hack but it beats doing two divisions and testing and what-not which
    would slow down the program.

    -Rusty-

    P.S. Sorry folks, I'm not always good at explaining mathmatical trickery.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Rusty-
    --
    Rusty Haddock = KD4WLZ = rusty@fe2o3.lonestar.org
    **Out yonder in the Van Alstyne (TX) Metropolitan Area**
    Microsoft is to software what McDonalds is to gourmet cooking
  • achilles03achilles03 Posts: 247
    edited 2005-02-22 14:43
    Sure, that'd work also. Same concept, different means.

    Dave
Sign In or Register to comment.