Multiplying by decimals in pbasic THE */ COMMAND
rtmorris
Posts: 31
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
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
You should check out emesystems.com and read the section on Stamp math...
Ryan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Ryan Clarke
Parallax Tech Support
RClarke@Parallax.com
.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
·
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
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
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."
·
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