7 ms·
I'm curious why some old architectures didn't use two's complement for signed numbers. What advantage did one's complement or signed magnitude have over two's c
by fred256 8y ago
I'm curious why some old architectures didn't use two's complement for signed numbers. What advantage did one's complement or signed magnitude have over two's complement?
- sunfish 8y agoTwo's complement has the bizarre property of being asymmetric about zero. So things like `abs` can overflow, among several other oddities. It's not unambiguously better.
- 0xcde4c3db 8y agoIt can be useful to distinguish between positive and negative zero in some cases, for example when dealing with values that have been rounded to zero or limits approaching zero.
- lopmotr 8y agoWas that ever really a reason for signed magnitude, or did people just make use of the 2nd representation of zero because because it was available and they couldn't be bothered putting that information in another variable or using floating point or fixed point, or anything else that would have achieved the same result?
- userbinator 8y agoI have a feeling signed-magnitude predates binary and complement arithmetic --- it is, after all, the "natural" way humans work with numbers. A lot of the early non-binary computers used some form of sign-magnitude, all the way back to punch card formats: https://en.wikipedia.org/wiki/Signed_overpunch https://en.wikipedia.org/wiki/Signed_overpunch On the other hand (no pun intended), early mechanical (decimal) manual adding machines made use of complement arithmetic too: https://en.wikipedia.org/wiki/Comptometer https://en.wikipedia.org/wiki/Comptometer https://en.wikipedia.org/wiki/Method_of_complements https://en.wikipedia.org/wiki/Method_of_complements
- Phrodo_00 8y agoTIL about signed overpunch Except the "natural" way also recognizes a single zero with no sign, so it's still not accurately modeling that. If you wanted to model natural arithmetic accurately you'd need 2 bits for the sign (positive, negative, unsigned). At that point, all of single bit signed magnitude, and complements are compromises.
- Animats 8y agoBurroughs 5xxx and 6xxx machines used signed-magnitude. Burroughs had a unique numeric representation. Numbers were 48 bits. Sign, sign of exponent, exponent, mantissa, with the binary point at the low end. Integers were thus valid floating point numbers. The math operations would maintain a value as an integer, with a zero exponent, if possible. IEEE floating point also maintains integer values as integers until they don't fit, but the representation is not integer-like.
- daveFNbuck 8y agoThat's true for limits approaching any number, so if that's important you'll need more than negative zero.
- paulddraper 8y agoDon't forget negabinary! https://en.m.wikipedia.org/wiki/Negative_base https://en.m.wikipedia.org/wiki/Negative_base Negabinary operations are extremely simple and elegant. Like 2s complement and 1s complement, it suffers from asymmetry in its range, though even more so.
- evincarofautumn 8y agoI always felt like negative bases are just strange enough, yet just practical enough, that they almost could have arisen as a system of numbers in a natural language. For example, phrasing 11 as 191, “one more than 90 less than 100”, would be unusual for such a small number but definitely sounds “naturalistic”, like phrasing 1990 as “a thousand, a hundred less than a thousand, ten less than a hundred” in Roman numerals, 99 as “four-twenty ten-nine” in French, or 9 as “five four” in Khmer.
- garmaine 8y agoIn addition to what's mentioned in the already great sibling comments, it's worth noting that IEEE floating point is signed-magnitude.
- jovial_cavalier 8y agoIs there a good way to represent floating points in order to do complement arithmetic?
- garmaine 8y agoYes, see JavaScript: https://www.w3schools.com/js/js_numbers.asp https://www.w3schools.com/js/js_numbers.asp
- hamiltonkibbe 8y agoIf you know a bit about the range you need to support you could use a fixed point representation
- userbinator 8y agoSign-magnitude for the significand, and offset-binary for the exponent. The reason for this odd combination is probably historical.
- p1mrx 8y agoIt doesn't seem odd to me. When describing things in nature, (+x, -x) tend to have more symmetry than (x, 1/x).
- sasaf5 8y agoWith one's complement it is easier to multiply by minus one: just invert all bits. It is also symmetrical around the zero, so sequences of random numbers will truly tend to average to zero.
- CJefferson 8y agoThe big one (for me) is that it's really annoying having one more negative value than positive value. Most software doesn't handle this properly, they don't realise abs doesn't always return a positive number (as abs(INT_MIN)=INT_MIN), and many other similar problems. In an ideal world, I would only use unsigned when you care about things like being able to use all bit representations, then have made the all-1s number something like NaN, for ints.