Shop OBEX P1 Docs P2 Docs Learn Events
Multiply high question — Parallax Forums

Multiply high question

max72max72 Posts: 1,155
edited 2009-01-28 15:46 in Propeller 1
I would like to ask a question about the multiply high function.
I would like to multiply a number by 0.75, so multiply high should be a perfect candidate to the task.
My problem arises with the signed integers.
I assumed I would have to multiply by 2^32*0.75=#C0000000 or 3_221_225_472.
If I take 1_000_000 and multiply by #C0000000 I get #000B71B0_00000000, which upper 32 bits give #000B71B0 or 750_000.
The propeller gives me back -250_000. I understand it is related to the fact that 2^32*0.75 in binary is b11000000_00000000_00000000_00000000. So the sign bit is set and it upsets everything. In fact if I multiply by the same number with the exception of the most significant bit, take the upper 32 bits (which is 250_000) and I carry the minus sign I can reproduce the result offered by the propeller.
My questions are:
Is my reasoning correct?
Can I multiply high for a number above 0.5? (below 0.5 everything works, above the sign bit is set).
What should I do to do that?

Thanks in advance,
Massimo

Comments

  • AleAle Posts: 2,363
    edited 2009-01-26 14:29
    You can always reduce the range. Say 0xC000 is 0.75 instead of 0xc000_0000 that way you avoid for some range the sign.
  • Tracy AllenTracy Allen Posts: 6,663
    edited 2009-01-26 18:49
    or Z = 0x6000_0000 as 0.75 and

    Y := Z ** (X * 2)
    or
    Y := (Z ** X) * 2

    The fraction is 31 bits, plus sign if necessary.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • max72max72 Posts: 1,155
    edited 2009-01-27 07:34
    Thanks,
    I used the solution
    with Z = (2^32*0.75)/2 or 0X6000_0000
    and
    Y := (Z ** X) * 2
    but I wasn't sure i correctly understood how it works.

    Massimo
  • hippyhippy Posts: 1,981
    edited 2009-01-27 18:42
    max72 said...
    I would like to multiply a number by 0.75, so multiply high should be a perfect candidate to the task.

    Isn't original minus original shifted right by two bits even better, much quicker ?
  • awesomeduckawesomeduck Posts: 87
    edited 2009-01-28 01:01
    I'm not good with math on Prop yet, so let me ask this...
    .75 = 3/4 = 3 / 2 / 2 = times 3, shift right 1, shift right 1 = shr, times 3, shr

    In other words, could you use this formula, with minimal rounding error, and no division, one multiply and 2 shifts:

    Y = (((Z sign extended shift right 1) * 3) sign extended shift right 1)
    or
    Y = ((( Z ~> 1) * 3) ~> 1)

    Just asking? I really have no idea if this would work, but I am curious.

    Andy hippy, you mean Y = Y -( Y ~> 2)... right?

    Post Edited (awesomeduck) : 1/28/2009 1:09:46 AM GMT
  • max72max72 Posts: 1,155
    edited 2009-01-28 15:46
    Thank you for the suggestions.
    At the moment of writing I was playing with the numbers, so I didn' even consider the possibility to use shifts.

    Massimo
Sign In or Register to comment.