Velocity Reviews > How many distinct float values?

# How many distinct float values?

Johnathan Doe
Guest
Posts: n/a

 12-08-2004
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

Gordon Burditt
Guest
Posts: n/a

 12-08-2004
>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

CBFalconer
Guest
Posts: n/a

 12-08-2004
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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Kevin Bracey
Guest
Posts: n/a

 12-08-2004
In message <41b6a942\$0\$25784\$(E-Mail Removed)>
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?

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/

Keith Thompson
Guest
Posts: n/a

 12-08-2004
Kevin Bracey <(E-Mail Removed)> writes:
> In message
> <41b6a942\$0\$25784\$(E-Mail Removed)> 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?

>
> 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.velocityreviews.com/forums/(E-Mail Removed) <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.

Kevin D. Quitt
Guest
Posts: n/a

 12-08-2004
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

Dik T. Winter
Guest
Posts: n/a

 12-09-2004
In article <(E-Mail Removed)> Kevin Bracey <(E-Mail Removed)> 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/

Kevin Bracey
Guest
Posts: n/a

 12-09-2004
In message <(E-Mail Removed)>
Keith Thompson <(E-Mail Removed)> 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/

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Edward A. Falk C Programming 1 04-04-2013 08:07 PM Hicham Mouline C++ 1 04-11-2010 10:56 AM Carsten Fuchs C++ 45 10-08-2009 09:47 AM Andy Java 7 05-10-2004 09:26 PM bd C Programming 0 07-07-2003 12:09 AM