r/math•
nomoch

Twos complement in floating point numbers.

I was playing around with floating point numbers and noticed that under IEEE 754 there were two possible values of zero, a negative zero and a positive one. As far as I can tell there is no reason why we can't use twos complement to represent negative floating point numbers such that we avoid that oddness.

Other than making the determination of absolute values a bit more difficult, not the significand + 1 to the lest significant bit, is there a reason why I can't find any implementations that use this?

Also is there a good book/paper/textbook I can read on this?

Posted May 28, 2015, 8:03 AM UTC

44
yas_ticot•

A "good" reason for having positive zero and negative zero is for handling 1.0/0.0. In floating point arithmetic, this does not raise an exception but returns infinity. The sign of the infinity being the product of the signs, therefore 1.0/(+0.0) is +infinity but 1.0/(-0.0) is -infinity.

I've always seen this as a way to handle limits with floating point numbers.

7
stevenxdavis•

Keep in mind that a floating-point representation has to store three things: the sign, the exponent, and the significand. In the IEEE 754 standard, they're stored in that order.

It would be possible to instead combine the sign and value into a single two's complement representation and then put the exponent at the end (or beginning, but that would cancel the benefit of having the sign bit first). Although this would make some operations a bit easier, it wouldn't help all that much in most cases. Furthermore, floating-point operations are very complicated, which is why they are almost always handled separately from other arithmetic operations anyway.

Floating-point numbers store more information than signed integers and need to provide more functionality, including infinite values, not-a-number, and denormalized numbers, which are a special case of extremely small numbers. In many cases, having the sign bit at the beginning and the value at the end makes operations simpler. The IEEE tried to balance all these concerns and chose their system accordingly, and the positive and negative zero was one concern that didn't bother them too much. In fact, u/yas_ticot provided one situation where it can actually be helpful.

6
nomochOP•

That all sounds reasonable, but I'm just surprised I haven't been able to find much literature on it. Do you have links on the trade offs?

The infinities in IEEE 754 are completely handled in the exponent, the nans are handled by turning everything into 1's, these are again features which can be used on a twos complement significand. As for the negative zero with division, that might be useful in some edge cases but I would be very surprised if you can get it reliably during regular subtractions exactly. Most of the time there will not be exact cancellation and you will fly off towards infinity in the right direction and then use tests in your numerical code to see if you're diverging.

2
stevenxdavis•

I can't say I know the full details of the debate when the standard was being developed in the 70s and 80s because I wasn't born until 1989. However, I do know that one benefit of IEEE 754 floating-point numbers is that they can be ordered lexicographically as signed integers, which makes comparisons fairly simple. A two's-complement representation would make that much more difficult to implement

If you want to read more about the historical development of IEEE 754, William Kahan (the main designer of the standard), has published a lot of papers about it: https://www.eecs.berkeley.edu/Pubs/Faculty/kahan.html .

2