Multiply high question
max72
Posts: 1,155
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
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
Y := Z ** (X * 2)
or
Y := (Z ** X) * 2
The fraction is 31 bits, plus sign if necessary.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
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
Isn't original minus original shifted right by two bits even better, much quicker ?
.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
At the moment of writing I was playing with the numbers, so I didn' even consider the possibility to use shifts.
Massimo