views:

149

answers:

5

Why exponent in float is displaced by 127?
Well, the real question is : What is the advantage of such notation in comparison to 2's complement notation?

A: 

The exponent in a 32-bit float consists of 8 bits, but without a sign bit. So the range is effectively [0;255]. In order to represent numbers < 2^0, that range is shifted by 127, becoming [-127;128].

That way, very small numbers can be represented very precisely. With a [0;255] range, small numbers would have to be represented as 2^0 * 0.mantissa with lots of zeroes in the mantissa. But with a [-127;128] range, small numbers are more precise because they can be represented as 2^-126 * 0.mantissa (with less unnecessary zeroes in the mantissa). Hope you get the point.

AndiDog
I believe he's asking why an offset is used instead of, say, 2's complement.
Anon.
Sorry, didn't know what U2 meant.
AndiDog
Hmmm I found somewhere the name 'U2' notation, and I thought that is is some abbreviation :| Now as I google it, I think that it was some kind of mistake. Obvouisly I was thinking about 2's complement notation.
Tomek Tarczynski
+5  A: 

Since the exponent as stored is unsigned, it is possible to use integer instructions to compare floating point values. the the entire floating point value can be treated as a signed magnitude integer value for purposes of comparison (not twos-compliment).

John Knoeller
Wouldn't it worked the same if we used 2's complement notation?
Tomek Tarczynski
This isn't quite true. Two negative floating-point values do not compare properly as signed integers. All the other cases work properly.
Stephen Canon
@Stephen: Yes that's true, floating point numbers can be compared as signed magnitude integers, not twos compliment integers, the sign bit requires some special case handling.
John Knoeller
@John, "the entire floating point value can be treated as a signed integer value" is definitely untrue. Consider changing the answer?
Potatoswatter
@Potatoswatter: I changed/clarified the answer
John Knoeller
A: 

Just to correct some misinformation: it is 2^n * 1.mantissa, the 1 infront of the fraction is implicitly stored.

Evan Huddleston
Yes, it is 1.mantissa.
Tomek Tarczynski
Did you downvote my answer? It is not misinformation. One is only implicitly stored for exponents > -126, but zero is implicitly stored for -126. It's called "denormalised number".
AndiDog
I didn't downvote your answer, but your answer is still basically misleading/incorrect. You mention 2^-126 * 0.mantissa, but if the exponent is -126 then the exponent+bias = 1 so it is not a denormalized number therefore 1 is implicitly stored. Also you only mention "small numbers," so where is the cutoff?
Evan Huddleston
The exponents -127 and 128 (binary 0 and 255) are special ranges for small numbers (including +-0) and infinite/NaN. For binary 1, the formular is `+- 2^-126 * 1.mantissa`, but for binary 0, the formula is `+- 2^-126 * 0.mantissa`, resulting in a coherent range of numbers. I guess by "cutoff" you mean the smallest representable (non-zero) number? - That would be the denormalized number with the last bit of the mantissa set = `2^-126 * 0.mantissa` = `2^-126 * 2^-23`. I can't see anything incorrect in my answer.
AndiDog
I know what you meant by cutoff, I was just saying that your answer wasn't clear about the cutoff. Otherwise, I realize that I was mistaken. I'm used to seeing denormalized numbers represented as 1 - (1 - 0.mantissa) (like (1 - 2^-23) * 2^-126). So when I saw your number as 2^-126 * 0.mantissa, I assumed you had gotten mixed up.
Evan Huddleston
+1  A: 

Note that there is a slight difference in the representable range for the exponent, between biased and 2's complement. The IEEE standard supports exponents in the range of (-127 to +128), while if it was 2's complement, it would be (-128 to +127). I don't really know the reason why the standard chooses the bias form, but maybe the committee members thought it would be more useful to allow extremely large numbers, rather than extremely small numbers.

ysap
The IEEE standard actually supports single-precision exponents from -126 to +127; the exponent encodings that would ordinarily map to -127 and +128 have special meanings (zeros and denormals, infinities and NaNs).
Stephen Canon
+1  A: 

@Stephen Canon, in response to ysap's answer (sorry, this should have been a follow up comment to my answer, but the original answer was entered as an unregistered user, so I cannot really comment it yet).

Stephen, obviously you are right, the exponent range I mentioned is incorrect, but the spirit of the answer still applies. Assuming that if it was 2's complement instead of biased value, and assuming that the 0x00 and 0xFF values would still be special values, then the biased exponents allow for (2x) bigger numbers than the 2's complement exponents.

ysap
Oh, I agree entirely. Here, have some rep so you can comment =)
Stephen Canon