[Slightly OT]: More on ints and floats

Steven Taschuk staschuk at telusplanet.net
Mon Apr 7 23:09:03 EDT 2003


Quoth Tim Daneliuk:
  [...]
> As I understand it, integers and floats are distinct mathematical
> entities. A colleague of mine claims, that insfar as we use them in
> computing, ints are merely a proper subset of floats. [...]

Not if both use the same amount of storage.

For example, with four decimal digits to work with, the usual
unsigned int representation can represent 9999.  But with a
floating point representation in which one digit is dedicated to
the exponent, 9999 cannot be represented; it is between the two
representable numbers 9.99e3 and 1.00e4.

> [...] He furthermore
> asserts that (again as regards to computing) the distinction between
> them was made as a purely as a practical matter because floating point
> arithmetic was historically computationally expensive. He argues that
> any place one can use an int, these days (with cheap FP hardware), one
> could use a float 0 extended to the precision of the machine and get
> equivalent computational results. [...]

This may be true if you assume that overflow and underflow never
occur, and that error never accumulates to significant proportions.

But it is easy to construct counterexamples.  Working again with
my notional four-decimal-digit computer, unsigned ints can
correctly compute
	9980 + 0017 = 9997
while the floating point representation has rounding error for the
same computation:
	9.98e3 + 1.70e1 = 9.99e3
(or 1.00e4, depending on rounding mode).

Perhaps your friend's notion of a "practical programming problem"
is one in which the precision needed is much less than the
precision available.  (Lots of problems do indeed have this
property; but lots do not.)

  [...]
> As a matter of 'pure' mathematics, I argued that ints and floats are
> very different critters. My argument (which is no doubt formally very
> weak) is that the integer 3 and the float 3.0000 are different because
> of the precision problem.  For instance, the integer 3 exists at a single
> invariant point on the number line, but 3.0000 represents all numbers
> from 2.99995 through 3.00004.
> 
> Could one of the genius mathematicians here bring some clarity to this
> discussion, please?

I'm hardly a genius mathematician, but here's a few comments.

First, I disagree that the int 3 represents exactly the integer 3.
If int 3 is the result of wrapping int addition, it can be said to
represent some integer congruent to 3 modulo 2^w (where w is the
word size in bits).  If the int 3 is the result of a floor
division (that is, round-to-negative-infinity division of ints)
then it can be said to represent some number in [3,4).  (This is
exactly analogous to rounding error with floats.)

There are at least two ways to approach these situations formally.

One way is to describe computer addition, etc., as operators
directly on real numbers.  For example, you could define the int
addition operator (+) by
	x (+) y := (floor(x) + floor(y)) mod 10^4
In this way of thinking, the int 3 and the float 3.0 are not
distinct; the difference is actually between the operators 'int
addition' and 'float addition'.

Another way is to describe the computer values as entities
distinct from real numbers, and to define computer addition, etc.,
as operators on these entities, with a separate function which
maps from these entities to real numbers.  In this way of
thinking, ints and floats may be different kinds of entities.
(The operators are also distinct in this view.)

In any case, the important practical and theoretical question is
this: if you do a computer calculation and get a value x, what can
you infer about the number you would have gotten if you'd done
real arithmetic?  For such questions, whether you use fixed-point
or floating-point representations, what precision the
representations offer, and so forth, are vitally important points.

-- 
Steven Taschuk                          staschuk at telusplanet.net
"Its force is immeasurable.  Even Computer cannot determine it."
                           -- _Space: 1999_ episode "Black Sun"





More information about the Python-list mailing list