PDA

View Full Version : math calculations with a decimal result

gc3076
01-21-2006, 01:17 AM
is there a way to get a decimal result on a math calculation with the BS2.

simple i.e 1/3= 0.333

Andy Lindsay (Parallax)
01-21-2006, 01:26 AM
Yeah, there's a subroutine in this post: Duty Cycle for More Reliable Accelerometer Measurements (http://forums.parallax.com/showthread.php?p=524815)

Look for the Long Division for Duty Cycle Calculations section.

Post Edited (Andy Lindsay (Parallax)) : 1/20/2006 6:29:34 PM GMT

Newzed
01-21-2006, 01:27 AM
There a couple of ways to handle decimals with a Stamp.· One is the */
operator, another is the ** operator.· Both are explained in the Help file.

For the example you posted, you could write:

x = 1000/3
debug ".", dec x· 'debug would display .333

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

·

Andy Lindsay (Parallax)
01-21-2006, 01:33 AM
Yes, it depends on whether you want to do division with an unknown denomenator or not and also how much precision you need.· An example of using ** to multiply a variable by 0.3425 in your BS2 serin/serout (http://forums.parallax.com/showthread.php?p=567113) thread.

T&E Engineer
01-21-2006, 01:45 AM
You may also want to check out this website dealing with Basic Stamp 2 math:

http://www.emesystems.com/BS2math6.htm

Thanks.

Paul Baker
01-21-2006, 02:09 AM
Here's a quote from another part of Dr. Tracy Allen's website:

http://forums.parallax.com/attachment.php?attachmentid=73925

These are probably two of the most useful math operators available on the BASIC Stamp. · These operators allow the BASIC Stamp to do fractions, breaking the barrier of integer math. ·These operators will come up time and time again as we come up with solutions to practical calculation problems using the BASIC Stamp and as we·move on to "higher" math with the BASIC Stamp.

...

The way the BS2 manual explains the */ command is technical:

The result of the */ operator, Y *? Z, is the middle 16 bits of the 32-bit product of the two 16-bit input values, X and Z

It helps to recall (or to learn!) that dropping the 8 least significant bits of a binary number is equivalent to dividing the number by 28=256. That is precisely what the */ operator does. It implicitly performs the division by 256

The ** operator is similar, in that it takes the top 16 bits of the product. It drops the 16 least significant bits of the 32 bit product, in effect performing an implicit division by 216=65536. In the Stamp manual, the ** operator is explained in the context of double precision math. That is one use of it. This fractional multiplication is another. If you read through these math pages, ** turns up in all sorts of surprising places.

The full text is here: http://www.emesystems.com/BS2math1.htm

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10