View Full Version : Multiplying by decimals in pbasic THE */ COMMAND

rtmorris

01-19-2006, 12:11 AM

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

Ryan Clarke

01-19-2006, 12:40 AM

Rtmorris,

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

Ryan

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Ryan Clarke

Parallax Tech Support

RClarke@Parallax.com (mailto:RClarke@Parallax.com)

Newzed

01-19-2006, 12:51 AM

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

·

rtmorris

01-19-2006, 09: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?

-Rob Morris

Bruce Bates

01-19-2006, 10:10 PM

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

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 (http://www.sxvm.com)

"Ability may get you to the top, but it takes character to keep you there."

·

Jon Williams

01-20-2006, 12:34 AM

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