News for hackers. Stuff that matters.
Re:Signed Integers Are Two’s Complement by fred256 on Wednesday May 30, 2018 @09:48PM (#17193368)
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?
Re:Signed Integers Are Two’s Complement by sunfish on Wednesday May 30, 2018 @10:41PM (#17193552)
Two'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.
Re:Signed Integers Are Two’s Complement by sasaf5 on Thursday May 31, 2018 @01:54AM (#17194254)
With 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.
Re:Signed Integers Are Two’s Complement by 0xcde4c3db on Wednesday May 30, 2018 @10:52PM (#17193603)
It 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.
Re:Signed Integers Are Two’s Complement by daveFNbuck on Thursday May 31, 2018 @02:02AM (#17194273)
That's true for limits approaching any number, so if that's important you'll need more than negative zero.
Re:Signed Integers Are Two’s Complement by lopmotr on Wednesday May 30, 2018 @11:38PM (#17193776)
Was 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?
Re:Signed Integers Are Two’s Complement by userbinator on Wednesday May 30, 2018 @11:54PM (#17193823)
I 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

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/Method_of_complements

Re:Signed Integers Are Two’s Complement by Animats on Thursday May 31, 2018 @01:55AM (#17194256)
Burroughs 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.

Re:Signed Integers Are Two’s Complement by Phrodo_00 on Thursday May 31, 2018 @01:22AM (#17194150)
TIL 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.

Re:Signed Integers Are Two’s Complement by CJefferson on Thursday May 31, 2018 @04:39AM (#17194895)
The 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.

Re:Signed Integers Are Two’s Complement by garmaine on Wednesday May 30, 2018 @11:18PM (#17193708)
In addition to what's mentioned in the already great sibling comments, it's worth noting that IEEE floating point is signed-magnitude.
Re:Signed Integers Are Two’s Complement by userbinator on Thursday May 31, 2018 @12:06AM (#17193867)
Sign-magnitude for the significand, and offset-binary for the exponent. The reason for this odd combination is probably historical.
Re:Signed Integers Are Two’s Complement by akrasuski1 on Thursday May 31, 2018 @12:36AM (#17193989)
It's done so that the bits will compare the same way whether treated as float or int. (modulo NaNs and stuff)
Re:Signed Integers Are Two’s Complement by nhaehnle on Thursday May 31, 2018 @04:54AM (#17194950)
That is only true for non-negative floating point numbers. It's still useful though.

Interestingly, posits as originally proposed do have this property (except for infinity).

Re:Signed Integers Are Two’s Complement by garmaine on Thursday May 31, 2018 @04:05PM (#17199713)
Would have been nice if IEEE stored the complement sign.
Re:Signed Integers Are Two’s Complement by exDM69 on Thursday May 31, 2018 @02:38AM (#17194394)
It's also practical for hw implementation and has other nice qualities. E.g. comparisons and sorting are easy. Radix sort works for floating point (with some bit magic). Terms and conditions may apply (oddities w.r.t. INF, Nan, etc).
Re:Signed Integers Are Two’s Complement by p1mrx on Thursday May 31, 2018 @12:22AM (#17193929)
It doesn't seem odd to me. When describing things in nature, (+x, -x) tend to have more symmetry than (x, 1/x).
Re:Signed Integers Are Two’s Complement by jovial_cavalier on Wednesday May 30, 2018 @11:49PM (#17193807)
Is there a good way to represent floating points in order to do complement arithmetic?
Re:Signed Integers Are Two’s Complement by garmaine on Wednesday May 30, 2018 @11:56PM (#17193828)
Re:Signed Integers Are Two’s Complement by dbaupp on Thursday May 31, 2018 @12:02AM (#17193851)
JS is IEEE754, not a (one's or two's) complement-based representation.
Re:Signed Integers Are Two’s Complement by garmaine on Thursday May 31, 2018 @04:04PM (#17199700)
Ah I misunderstood the question.
Re:Signed Integers Are Two’s Complement by hamiltonkibbe on Thursday May 31, 2018 @12:38AM (#17193997)
If you know a bit about the range you need to support you could use a fixed point representation
Re:Signed Integers Are Two’s Complement by paulddraper on Wednesday May 30, 2018 @11:15PM (#17193693)
Don't forget negabinary! 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.

Re:Signed Integers Are Two’s Complement by evincarofautumn on Thursday May 31, 2018 @03:42AM (#17194657)
I 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.