Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #55962
| From | rickman <gnuarm@gmail.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: n00b confusion regarding the implementation vs. specification of * |
| Date | 2017-03-20 06:56 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <oaocaq$659$1@dont-email.me> (permalink) |
| References | <788f5318-e6c4-499d-838a-89c45a917142@googlegroups.com> <cuidnTwS4ZVdSVPFnZ2dnUU7-KXNnZ2d@supernews.com> <oan2oo$re9$3@dont-email.me> <7-Kdnc0K7-nnBVLFnZ2dnUU7-e-dnZ2d@supernews.com> |
On 3/20/2017 4:53 AM, Andrew Haley wrote:
> rickman <gnuarm@gmail.com> wrote:
>> On 3/19/2017 2:58 PM, Andrew Haley wrote:
>>>
>>> In two's complement representation, for every pair of arguments,
>>> signed or unsigned, the resulting bit pattern is correct when
>>> interpreted as either signed or unsigned. This follows because signed
>>> and unsigned addition don't need special handling, and multiplication
>>> is no more than repeated addition.
>>
>> Try it sometime. A multiplication routine doing shift and add of the
>> multiplicand using the bits in the multiplier as enable flags does not
>> work for a negative multiplier. If your statement were correct, it
>> should work regardless. Instead the multiplier has to be negated along
>> with the result.
>
> Please provide an example of a pair of numbers which demonstrates
> this.
Any non-trivial negative number for a multiplier. Try it, you'll see.
I wracked my brain trying to figure out a way to adjust the result to
make it work. After all, negative numbers just have the sign bit
different, right? Good luck!
Albert, indicated that multiply works the same for unsigned and signed
numbers as long as the unsigned numbers don't overflow. However if you
do a signed multiply using an unsigned number with the msb set, that
screws it up. This is true even if the result is double precision. So
you can't do an unsigned multiply on signed numbers in the general case.
0101 (5) multiplicand
x 1010 (-6) multiplier
Here is each step
Multiplier Product
1010 0000 0000
^--- zero, so don't add multiplicand and shift
0101 0010 1000
^--- one, so add multiplicand and shift
0010 0001 0100
^--- zero, so don't add multiplicand and shift
0001 0011 0010
^--- one, so add multiplicand and shift
Result is 0x32 or 50.
Negate first...
Multiplier Product
0110 0000 0000
^--- zero, so don't add multiplicand and shift
0011 0010 1000
^--- one, so add multiplicand and shift
0001 0011 1100
^--- one, so add multiplicand and shift
0000 0001 1110
^--- zero, so don't add multiplicand and shift
Negate product
0001 1110 --> 1110 0010
Result is 0xE2 or -30.
Using the same binary numbers, but treating them as all unsigned gets
the opposite results. The -6 is now a 10. The unsigned multiply gives
the right result, 50 and the signed multiply gives 226, a wrong result.
I've read that Booth's algorithm works for both signed and unsigned
numbers, but I don't know if they have to be handled differently.
--
Rick C
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
n00b confusion regarding the implementation vs. specification of * Michael Barry <barrym95838@yahoo.com> - 2017-03-19 10:41 -0700
Re: n00b confusion regarding the implementation vs. specification of * Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-03-19 13:58 -0500
Re: n00b confusion regarding the implementation vs. specification of * rickman <gnuarm@gmail.com> - 2017-03-19 19:06 -0400
Re: n00b confusion regarding the implementation vs. specification of * Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-03-20 03:53 -0500
Re: n00b confusion regarding the implementation vs. specification of * rickman <gnuarm@gmail.com> - 2017-03-20 06:56 -0400
Re: n00b confusion regarding the implementation vs. specification of * Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-03-20 07:53 -0500
Re: n00b confusion regarding the implementation vs. specification of * rickman <gnuarm@gmail.com> - 2017-03-20 18:15 -0400
Re: n00b confusion regarding the implementation vs. specification of * Coos Haak <htrofhc@gmail.com> - 2017-03-20 23:30 +0100
Re: n00b confusion regarding the implementation vs. specification of * julian.fondren@endurance.com - 2017-03-20 15:34 -0700
Re: n00b confusion regarding the implementation vs. specification of * "Elizabeth D. Rather" <erather@forth.com> - 2017-03-20 14:40 -1000
Re: n00b confusion regarding the implementation vs. specification of * hughaguilar96@gmail.com - 2017-03-20 19:41 -0700
Re: n00b confusion regarding the implementation vs. specification of * anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-21 17:18 +0000
Re: n00b confusion regarding the implementation vs. specification of * anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-20 15:07 +0000
Re: n00b confusion regarding the implementation vs. specification of * hughaguilar96@gmail.com - 2017-03-19 12:32 -0700
Re: n00b confusion regarding the implementation vs. specification of * hughaguilar96@gmail.com - 2017-03-19 13:54 -0700
Re: n00b confusion regarding the implementation vs. specification of * albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-03-19 21:21 +0100
Re: n00b confusion regarding the implementation vs. specification of * Michael Barry <barrym95838@yahoo.com> - 2017-03-19 16:41 -0700
Re: n00b confusion regarding the implementation vs. specification of * anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-21 17:56 +0000
Re: n00b confusion regarding the implementation vs. specification of * anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-20 08:57 +0000
csiph-web