View Full Version : Multiplying by decimals in pbasic THE */ COMMAND
01-18-2006, 11:11 PM
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
x var byte
x = 8
x = x */·200····· 'here i am trying to multiply 8 by .782
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?
01-18-2006, 11:40 PM
You should check out emesystems.com and read the section on Stamp math...
Parallax Tech Support
01-18-2006, 11:51 PM
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
Do you have a Stamp Tester yet?
01-19-2006, 08:50 PM
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?
01-19-2006, 09:10 PM
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.
Because (200/256) = 0 in integer math.
"SX-Video·Module" Now available from Parallax for only $28.95
"SX-Video OSD module" Now available from Parallax for only·$49.95
Product web site: www.sxvm.com (http://www.sxvm.com)
"Ability may get you to the top, but it takes character to keep you there."
01-19-2006, 11:34 PM
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.
Applications Engineer, Parallax