Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish between 1 and zero floating-point values?

I know it might be a noob question but since it's not covered in the e-book I'm studying I'm gonna ask it. In IEEE standard binary floating-point format actually representing numbers by scientific notation I know that an integer value of one is always assumed to be added to the fractional part illustrated by the significand part of the binary and is not included in the binary, So what confuses me is how to How to distinguish between 1 and zero floating-point values because I assume both have a totally zero significand and I guess the differentiation here should be done by exponent part but don't know how!

like image 455
Pooria Avatar asked Dec 18 '25 17:12

Pooria


2 Answers

For the zeroes (there are two, a positive and a negative zero that differ in the sign bit but must be considered equal), the significand and the exponent are all 0-bits, whereas for non-zero values at least one of them has a 1-bit (for a value of 1, the exponent is all 1-bits except for the most significant one).

The Wikipedia article on the IEEE 754 standard lists the exact bit patterns.

like image 188
Michael Borgwardt Avatar answered Dec 21 '25 10:12

Michael Borgwardt


This is a good question. I always wondered about this, too. And your supposition is correct: The classification is done by the exponent value.

In the IEEE-754 floating-point formats there are, depending on how you count them, up to five or six different "kinds" or "types" or "classes" of values. And the exponent field is simultaneously the biased exponent, and a sort of selector to help determine which kind of value you have.

Basically, the exponent value can be 0, or it can be its maximum value, or it can be somewhere in between. And the significand (also known as "mantissa") can either be zero or nonzero. So that's 3 × 2 = 6 different possibilities. And if we arrange them in a table, everything becomes reasonably clear:

table of floating-values, classified

When both the significand and the exponent are 0, we have a true zero value. When the exponent has (almost) any other value, other than its maximum, we have an ordinary floating-point number, with an implicit 1 bit. When the exponent is 0 but the significand is nonzero, we have a "subnormal" number, without the implicit 1 bit. (The story of subnormal numbers is reasonably fascinating in its own right, but I'm not going to go into it here.) And when the exponent has its maximum value — 255 for single-precision, or 2047 for double-precision — this indicates we don't have a finite, numerical floating-point value at all, but rather a special 'infinity' or 'not-a-number' (NaN) marker.

I like this table a lot, but it's worth noting that it is misleading in one respect. It makes it look like the six "classes" are of roughly equal size, but of course they're not. There are two "special" exponent values, but there are a lot more — either 254 or 2046 — that are ordinary. And there's only one significand value that is ever special, but a whole lot more — either 223-1, or 252-1 — that are ordinary. So that middle cell, labeled "ordinary floating-point numbers", actually accounts for at least 99% of the values.

(But it is true, and perhaps surprising, that there is not just one NaN value, but quite a few of them: 8388607 of them in single precision, and a whopping 252-1 of them in double precision.)

Here is another way of arranging the table:

floating-values classified a slightly different way

This shows how the values increase, bottom to top, from zero to subnormal to ordinary, with infinity and the NaNs at the top.

One final point. Within the "ordinary" floating-point values, the ones where the exponent is neither at its minimum or its maximum, is there any significance to whether the significand is zero or nonzero? Only this: if the stored significand is 0, this means that the only 1 bit it has is the implicit one. In other words, any floating-point value with an "intermediate" exponent and a zero significand is going to be a perfect power of two: 1, 2, 4, 8, …, or ½, ¼, ⅛, … .

like image 34
Steve Summit Avatar answered Dec 21 '25 10:12

Steve Summit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!