[Tutor] re: range function

D-Man dsh8290@rit.edu
Wed, 23 May 2001 16:29:59 -0400


On Wed, May 23, 2001 at 03:06:11PM -0400, Lance E Sloan wrote:
| 
<doc string from range>
| Integers only.  So I tried my hand at making a float version:
...
| The only problem is the inaccurate way floating-point numbers are
| represented on most computers:
...
| Maybe somebody else knows of a Python module that will help us do more
| accurate float math.

Floating point is inaccurate even on paper -- try writing 1/3 in
decimal.

The solution is to rework your algorithm to use ints instead of
floats or create a rational type that uses ints as its basis.  There
is a PEP to add rationals as builtin types.  I think there is also
some sample code on the web somewhere for a Rational class.

The key is to determine what you need the floats for, why you need
them and what kind of precission you need.  Then you can create a
solution that works for those situations, but a general solution is
not possible on hardware that uses base-2 "scientific notation"
floating point.

-D