Shop OBEX P1 Docs P2 Docs Learn Events
Simple Math Question. — Parallax Forums

Simple Math Question.

DownsDowns Posts: 30
edited 2009-01-14 00:41 in BASIC Stamp
Hi,

How do you get the BASIC STAMP 2 to multiply a value with 48.92? I tried using Value1 */ $48EB but that's not correct. 48 being the upper byte and Taking .92 * 256 = 235 = ($EB) for the lower byte. I'm lost somewhere! Any help would be greatly appreciated. Thank you.

Comments

  • DownsDowns Posts: 30
    edited 2009-01-13 20:43
    Never mind, figured it out. Thanks anyway!
  • davejamesdavejames Posts: 4,047
    edited 2009-01-13 22:40
    Pray tell - what's the method?

    Thanks,

    DJ
  • ZootZoot Posts: 2,227
    edited 2009-01-13 23:03
    If you are multiplying by a fraction greater than 1, you get the Word value for the "mid-multiply" like this:

    x * 5.2

    = x */ ( 5.2 * 256 )
    = x */ 1331 (decimal) or

    result = x */ $533

    Basically, for a mid-multiply, 5.2 = ( 5 * 256 ) + ( .2 * 256 )

    If you are multiplying by a fraction less than 1, high-multiply gives more precision. It's basically the same thing but with $FFFF (65535 decimal):

    x * .2
    = x ** ( .2 * 65535 )
    = x ** 13107 (decimal)

    result = x ** $3333

    Neither of these simple examples takes into account rounding... a better explicit mid-multiply might go something like this:

    x * 5.2
    = x */ (5.2 * 256)

    result = ( (x * $533) + 128 ) / 256

    Which takes into account rounding during the divide by 256 (mid-multiply does the exact same thing as above in 32bit math space, but doesn't add the 128 for rounding).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 1/13/2009 11:08:24 PM GMT
  • davejamesdavejames Posts: 4,047
    edited 2009-01-14 00:41
    Thanks Zoot.

    DJ
Sign In or Register to comment.