Shop OBEX P1 Docs P2 Docs Learn Events
Math function */ - how to reverse Multiply Middle — Parallax Forums

Math function */ - how to reverse Multiply Middle

DavetiefDavetief Posts: 16
edited 2014-12-07 19:26 in BASIC Stamp
Hi all,

I am trying to figure out how to do this -

I have this calculation in a program: F = K */ $01CC

Example 53988 = 30046 */ $01CC

In another part of the program, I need to do the reverse of this calculation. The question is how? Using / doesn't work.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-12-06 20:12
    I'd think you'd divide with "/" and then multiply by 256.

    You lose the bottom 8-bits with the multiply middle operator and you can't get them back unless you preserved in some other way.

    So in your example, $01CC will end up just $0100.

    If this doesn't answer your question, can you give an example with the above numbers of what you want to do?
  • SapphireSapphire Posts: 496
    edited 2014-12-06 20:20
    By reverse, do you mean

    K = F */ $008F

    30157 = 53988 */ $008F which is close but not exactly the original 30046 due to rounding in determining $008F


    To get $008F, take $01CC, and convert to decimal: 1 + 204/256 = 1.79687.

    1/1.79687 = 0.55652 and that times 256 is 142.46956. Round up to 143 and convert to hex gives $008F, your new divisor.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-12-06 20:21
    In this case, $1CC is the same as 460 in decimal, and the meaning of the expression is y = x * (460/256). The division by 256 is implicit in the Stamp and is written, y = x */ 460 or y = x */ $1CC.

    Maybe the inverse you want is 256/460. Calculate: factor = 256 * (256/460) = 142. Then x = y */ 142. Note that 142/256 = 256/460 approximately, the best that can be done with 256 as an implicit denominator.

    53988 */ 142 = 29946
    which is close to your starting value, 30046. Not exact, because there are a couple of round-offs involved.

    PS: Oh, I see you got a quick shower of answers!
  • DavetiefDavetief Posts: 16
    edited 2014-12-06 21:07
    Yes, the thing I am looking for is the way to get K when I have F. Looks like I have a couple answers above. I was hoping to get closer to the original value, but if the bits are gone, not much I can do about that.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-12-06 21:15
    In case you're not aware of it, Tracy has a lot of great information about math with the Basic Stamp on his website.

    Edit: I fixed the link. The URL tag was missing a quote.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-12-06 22:16
    Another option, the ** operator, more accurate, 16 bit instead of 8 bit.

    460/256 = 1 + 204/256 = 1 + 52224/65536, and on the Stamp that becomes,
    y = x + (x ** 52224) division by 65536 being implied. The implied fraction involving ** has to be less than one.

    30046 + (30046 ** 52224) = 53988

    The inverse is 256/460 = 36472/65536, and
    x = y ** 36472

    53988 ** 36472 = 30045.

    Much better inverse, less roundoff.

    Here's a direct link to this material on my website, http://emesystems.com/BS2math1.htm
    (Thanks Erco!)
  • ercoerco Posts: 20,257
    edited 2014-12-07 07:00
    You're most welcome, Tracy.









    Now what did I do again? :)
  • DavetiefDavetief Posts: 16
    edited 2014-12-07 19:26
    Than you! This is what I was looking for.

    Thank you to the others for the other ideas also. I also had tried them before seeing this one. I could have made due, but things will be simpler now.
Sign In or Register to comment.