Shop OBEX P1 Docs P2 Docs Learn Events
Multiplying by decimals in pbasic THE */ COMMAND — Parallax Forums

Multiplying by decimals in pbasic THE */ COMMAND

rtmorrisrtmorris Posts: 31
edited 2006-01-19 16:34 in BASIC Stamp
I am confused as to why I must first multiply a decimal by 256 before multiplying by a constant.

HERE IS SOME SAMPLE CODE THAT REPRESENTS· WHAT I AM DOING, BUT
NOT UNDERSTANDING

x var byte
x = 8

x = x */·200····· 'here i am trying to multiply 8 by .782

Debug ?x


THE CODE ABOVE SHOULD HAVE THE EFFECT OF MULTIPLYING 8....TO DO THIS i MULTIPLIED .782 BY 256, AND THEN ROUNDED OFF THE NUMBER LEAVING ME WITH 200

I UNDERSTAND THAT THE BS2 CAN ONLY TO INTEGER MATH, AND I AM ESSENTIALLY CONVERTING .782 INTO A DECIMAL, BUT WHY AND HOW IS THIS BEING DONE?

-Rob Morris

Comments

  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2006-01-18 16:40
    Rtmorris,

    You should check out emesystems.com and read the section on Stamp math...

    Ryan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Ryan Clarke
    Parallax Tech Support

    RClarke@Parallax.com
  • NewzedNewzed Posts: 2,503
    edited 2006-01-18 16:51
    Rob, you should read the Help section on the */ operator.

    .786 x· 256 = 200
    200 converted to HEX = $C8

    You are multiplying x by less then 1 so

    x = x*/$00C8

    If you multiplying by 1.786 then

    x = x*/$01C8

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • rtmorrisrtmorris Posts: 31
    edited 2006-01-19 13:50
    Thanks for all of your help!! The emesystems.com web site does an excellent job explaining stamp math. Just one last question:

    If I am trying to multiply 0.786 by a constant, I now know that I must multiply 0.786 x 256 resulting in 200. Since the */command is essentially taking 200 and dividing it by 256 before multiplying by the constant, then couldn't I simply multiply 200/256 by the constant and get the same result:

    In other words isn't

    x*/200 the same as x*(200/256) ???


    If so why would I chose one method over the other?

    -Rob Morris
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-01-19 14:10
    Rob -

    The smartest thing you could is to try it for yourself, and then examine the results.

    I believe what you should find is that the second method will show rounding errors where the first should not. In the first example there is only one operation. In the second, there are two; therein the difference.

    Regards,

    Bruce Bates
  • BeanBean Posts: 8,129
    edited 2006-01-19 14:10
    Rob,
    Because (200/256) = 0 in integer math.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    "Ability may get you to the top, but it takes character to keep you there."
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-01-19 16:34
    Math operations use an internal 32-bit result, but / (division)·only ever grabs the lower 16 bits -- this is why 200/256 returns 0.· The */ operator returns the middle (1 and 2) bytes·of the result, hence the operation is like multiplying by units of 1/256.· The ** operator returns the upper bytes (2 and 3) of the result, so it works like multiplying by units of 1/65536.

    Dr. Tracy Allen -- a very cool guy (coolest doctor you'll ever meet) and a friend of Parallax -- is·a math wizard and has a very nice explanation on his web site.· You can find that here: http://www.emesystems.com/BS2math1.htm.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.