# How many distinct float values?

Discussion in 'C Programming' started by Johnathan Doe, Dec 8, 2004.

1. ### Johnathan DoeGuest

I can google search to find the range of values that can be represented
in a float by reading up on the IEEE std, but is that the same as how
many distinct values that can go in a float type?

For instance, floats can distinguish 0.000001 and 0.000002. If I
started with 0.000001 and kept adding 0.000001 until I hit some maximum
value (FLT_MAX?) and kept a counter of how many times I added 0.000001,
would I have a count of how many distinct values can go in a float? And
would that be the same thing as FLT_MAX - FLT_MIN?

Thanks very much.
Johnathan

Johnathan Doe, Dec 8, 2004

2. ### Gordon BurdittGuest

>I can google search to find the range of values that can be represented
>in a float by reading up on the IEEE std, but is that the same as how
>many distinct values that can go in a float type?

No. The distance between representable floating-point numbers is
not constant because, well, the binary (or decimal) point floats.

If it's a 32-bit floating point number, you can represent AT MOST
2**32 bit patterns. Subtract one for negative zero, which has the
same value as positive zero. Subtract more for NaNs (and this
depends on just how many different NaN values you think are distinct)
and infinities. Subtract more for denormal values which duplicate
non-denormal values or each other. In any case the number of
distinct values is likely to be somewhat more than 98% of 2**32 but
less than 100% of 2**32.

>For instance, floats can distinguish 0.000001 and 0.000002. If I
>started with 0.000001 and kept adding 0.000001 until I hit some maximum
>value (FLT_MAX?) and kept a counter of how many times I added 0.000001,
>would I have a count of how many distinct values can go in a float? And

No. And you would likely NEVER REACH FLT_MAX.

Consider, for example, decimal floating point with exactly 6 decimal
digits of precision. You start off with 0.000001, and keep counting
until you get 1.000000. At that point, 1.000000 + 0.000001 =
1.000000, so you quit getting different answers. This happens even
if you've got enough exponent to represent 1.000000 e+100.

Note that adjacent representable numbers in this decimal floating
point are 1e+94 apart for .999999e+100 and .999998e+100, but
one millionth apart for .999999e+00 and .999998e+00 .

>would that be the same thing as FLT_MAX - FLT_MIN?

No. And on even unreasonable floating point systems,
FLT_MAX - FLT_MIN == FLT_MAX.

Gordon L. Burditt

Gordon Burditt, Dec 8, 2004

3. ### CBFalconerGuest

Johnathan Doe wrote:
>

.... snip ...
>
> For instance, floats can distinguish 0.000001 and 0.000002. If I
> started with 0.000001 and kept adding 0.000001 until I hit some
> maximum value (FLT_MAX?) and kept a counter of how many times I
> added 0.000001, would I have a count of how many distinct values
> can go in a float? And would that be the same thing as FLT_MAX -
> FLT_MIN?

float f, inc;
unsigned long ct, overct;

f = inc = 0.000001;
ct = overct = 0;
while (f < FLT_MAX) {
f = f + inc;
ct++;
if (0 == ct) overct++;
}

can be expected to run for a very long time. Think about it.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

CBFalconer, Dec 8, 2004
4. ### Kevin BraceyGuest

In message <41b6a942\$0\$25784\$>
Johnathan Doe <No-spam-here-johnathan_doe@!!!NOSPAMTHANKS!!!fastmail.com.au> wrote:

> I can google search to find the range of values that can be represented
> in a float by reading up on the IEEE std, but is that the same as how
> many distinct values that can go in a float type?
>
> For instance, floats can distinguish 0.000001 and 0.000002. If I
> started with 0.000001 and kept adding 0.000001 until I hit some maximum
> value (FLT_MAX?) and kept a counter of how many times I added 0.000001,
> would I have a count of how many distinct values can go in a float? And
> would that be the same thing as FLT_MAX - FLT_MIN?

As an additional comment to the other answers you've received, C99 adds
a couple of functions nextafter() and nexttoward() that can be used to
enumerate distinct floating values. This example would count the number of
distinct finite float values:

float f = -FLT_MAX;
long long count = 1;
do
{
f = nextafterf(f, FLT_MAX);
count++;
}
while (f != FLT_MAX);

Anyway, roughly speaking, an IEEE 32-bit single number can hold 2^32
different values, as there are 2^32 different bit patterns for the number.
But more specifically these break down as:

2 zeros (exponent=mantissa=0)
2 infinities (exponent=max, mantissa=0)
16,777,214 NaNs (exponent=max, mantissa<>0)
16,777,214 subnormal numbers (exponent=0, mantissa<>0)
4,261,412,864 normal numbers (0<exponent<max)

The count loop above would count the normals, subnormals and one of the
zeros, so should give 4,278,190,079.

In IEEE, all subnormal and normal numbers are distinct - it's an efficient
representation in that respect. The only "duplicate" coding is +/-0, and that
has its uses in certain infinitesimal convergence calculations - it preserves
the information of which side a cut was approached.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/

Kevin Bracey, Dec 8, 2004
5. ### Keith ThompsonGuest

Kevin Bracey <> writes:
> In message
> <41b6a942\$0\$25784\$> Johnathan
> Doe <No-spam-here-johnathan_doe@!!!NOSPAMTHANKS!!!fastmail.com.au>
> wrote:
>> I can google search to find the range of values that can be represented
>> in a float by reading up on the IEEE std, but is that the same as how
>> many distinct values that can go in a float type?
>>
>> For instance, floats can distinguish 0.000001 and 0.000002. If I
>> started with 0.000001 and kept adding 0.000001 until I hit some maximum
>> value (FLT_MAX?) and kept a counter of how many times I added 0.000001,
>> would I have a count of how many distinct values can go in a float? And
>> would that be the same thing as FLT_MAX - FLT_MIN?

>
> As an additional comment to the other answers you've received, C99 adds
> a couple of functions nextafter() and nexttoward() that can be used to
> enumerate distinct floating values. This example would count the number of
> distinct finite float values:
>
> float f = -FLT_MAX;
> long long count = 1;
> do
> {
> f = nextafterf(f, FLT_MAX);
> count++;
> }
> while (f != FLT_MAX);
>
> Anyway, roughly speaking, an IEEE 32-bit single number can hold 2^32
> different values, as there are 2^32 different bit patterns for the number.
> But more specifically these break down as:

[details snipped]

It's also important to remember that C's type "float" isn't
necessarily an IEEE floating-point type. It almost certainly will be
on any modern machine, but there are still system's with other
floating-point formats (VAX, IBM 370, some older Cray systems, etc.).

In C99, an implementation will define the symbol __STDC_IEC_559__
if it conforms to the IEEE floating-point standard (C99 Annex F).

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Dec 8, 2004
6. ### Kevin D. QuittGuest

Memorize the following:

<ftp://ftp.quitt.net/Outgoing/goldbergFollowup.pdf>

--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list

Kevin D. Quitt, Dec 8, 2004
7. ### Dik T. WinterGuest

In article <> Kevin Bracey <> writes:
....
> In IEEE, all subnormal and normal numbers are distinct - it's an efficient
> representation in that respect. The only "duplicate" coding is +/-0, and that
> has its uses in certain infinitesimal convergence calculations - it preserves
> the information of which side a cut was approached.

Yes, I know that Kahan was a big proponent of it (as of the signed
infinities). But you get in problems when you try to implement
standard functions with complex arguments.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

Dik T. Winter, Dec 9, 2004
8. ### Kevin BraceyGuest

In message <>
Keith Thompson <> wrote:

> In C99, an implementation will define the symbol __STDC_IEC_559__
> if it conforms to the IEEE floating-point standard (C99 Annex F).

Although Annex F specifies a lot more than just IEEE 754 conformance. The
implementation I use has been IEEE 754 conformant for years - far more so
than most x86 compilers, but is still some way from meeting all the
requirements of Annex F regarding library functions and pragmas. There's lots
of intricacy there to do with when exceptions get raised etc. So
__STDC_IEC_559__ remains undefined.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/

Kevin Bracey, Dec 9, 2004

### Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.