osmium wrote:

> "Vladimir Oka" writes:

>

>> I think (before looking into the Standard) that all small integers are

>> guaranteed to be. Beware numbers like 1000000000000042.0000, though.

>

> No. For example, the number 1 can not be represented exactly.
You've happened to pick a number that's exactly representable in all

floating-point systems: 1.0 = 1 * b^0 for any valid base b.

> Select as many terms as you wish from the following series:

> 1/2, 1/4, 1/8, ....1/2^googolplex

>

> No way are you going to get a 1 out of that. People who write I/O libraries

> are aware of this and create the *illusion* that you can.

>
This illusion is called rounding, and it's not really I/O specific. It

depends on the number of digits you ask for, and are able to get.

Had you said 0.1, you might have had a point, as this number is not exactly

representable in binary floating point. On the other hand, implementations

need not use base 2; 0.1 *is* exactly representable in base 10.

You may wish to verify that "all small integers" are indeed exactly

representable in every floating-point system. All integers from -(b^p)

through b^p, with b the base and p the precision, are exactly representable.

For b = 2 and p = 24 (an IEEE 754 single-precision float), all integers

from -16777216 through 16777216 are exactly representable. For double

precision, all integers from -(2^53) through 2^53 are exactly representable.

At this point I'm obliged to refer to the classic: "What Every Computer

Scientist Should Know About Floating-Point Arithmetic", available at, for

example,

http://www.physics.ohio-state.edu/~d...point_math.pdf
S.