Shop OBEX P1 Docs P2 Docs Learn Events
math calculations with a decimal result — Parallax Forums

math calculations with a decimal result

gc3076gc3076 Posts: 44
edited 2006-01-20 19:09 in BASIC Stamp
is there a way to get a decimal result on a math calculation with the BS2.

simple i.e 1/3= 0.333

Comments

  • edited 2006-01-20 18:26
    Yeah, there's a subroutine in this post: Duty Cycle for More Reliable Accelerometer Measurements

    Look for the Long Division for Duty Cycle Calculations section.

    Post Edited (Andy Lindsay (Parallax)) : 1/20/2006 6:29:34 PM GMT
  • NewzedNewzed Posts: 2,503
    edited 2006-01-20 18:27
    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

    ·
  • edited 2006-01-20 18:33
    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 thread.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2006-01-20 18:45
    You may also want to check out this website dealing with Basic Stamp 2 math:

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

    Thanks.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-01-20 19:09
    Here's a quote from another part of Dr. Tracy Allen's website:

    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
    391 x 36 - 2K
Sign In or Register to comment.