I know how computer today stores negative integers, which most of the computers use the 2' complement. I just wast wondering the 2' complement method applies for all kinds of numbers like floating points as well?
2 Answers
No, floating-points does not use 2 complement representation, but as all binary implementations have a sign bit, it is guaranteed that for all values (except NaNs where signs have no sense) the integer representation of a floating-point number can be tested with < 0. This is because integers in 2 complement are also negative if the first bit is set. But neither the significand nor the exponent use 2 complement representation.

- 4,144
- 27
- 41
-
Per the IEEE 754 standard, NaNs do have a sign bit. It cannot be tested by comparison to 0, since every test of an order relation with a NaN returns false, but the sign bit can be examined separately, with a function that depends on the language. – Eric Postpischil Jun 20 '12 at 13:00
-
Yes, NaNs have an internal sign bit, but it carries no meaning. As NaNs are "Not a Number" neither the sign nor the exponent/mantissa have any meaning. You can use the bits of the mantissa (with the exception of the MSB which defines if it is a quiet NaN/ signalling NaN) as an error code (and include the sign bit into the meaning), but an integer comparison < 0 which works on all other numbers does not work on a NaN because there are no "positive" or "negative" NaNs. – Thorsten S. Jun 20 '12 at 13:08
There are different kinds of floating point number representations, but I recall that most are something akin to a sign bit (1 = positive), then a 2s complement exponent value, and then a 2s complement mantissa value with the most significant bit in the 1s position when the exponent is zero.
Notice that in this arrangement, you can use integer comparison for greater/lesser.
EDIT
The above is obviously based on faulty memory, but there's a good explanation over at http://en.wikipedia.org/wiki/Binary32.
Basically, ...
- The first bit is the sign of the number, which is also the sign of the mantissa
- The next several bits are the exponent, which may be unsigned or be signed using 2-s complement.
- The remaining bits are the mantissa, minus an implicit leading "1".
- Zero is a special case...

- 11,725
- 1
- 33
- 43
-
If by “integer comparison”, you mean reinterpreting the encodings of a floating-point number as integers and comparing them, then this fails for negative numbers. It also fails to report that -0 equals +0. – Eric Postpischil Jun 20 '12 at 13:06
-
two's com is used for floating ONLY WHEN the numbers being used for computation, but for storage, they don't use two's comp. please correct your answer. – MartianMartian Feb 24 '19 at 01:19