[Python-Dev] Re: Re: Decimal type question [Prothon]

Michael Chermside mcherm at mcherm.com
Wed Aug 11 22:53:31 CEST 2004


> the only reason programmers write loops as integers is
> because accumulating floats "doesn't work".  With Decimal, that
> is no longer the case.

That is completely untrue. Binary floating point does just as
good a job of handling integers as decimal floating point does.
For instance:

    >>> accumulator = 0.0
    >>> for i in range(25000):
    ...     accumulator += 1.0
    ...
    >>> print accumulator
    25000.0

Notice, perfect accuracy. Yet people don't tend to use floating
point for loops... why not?

I'd argue there are at LEAST two other good reasons. Performance
(floating point arithmatic is fast, but the equivalent integer
arithmatic is normally faster), and clarity of intent (using an
int type makes it clear that you intend only to use integers.

But lots of languages make other choices. The number of languages
that have a single numeric type is pretty large... perl comes to
mind, but I bet I could list 10 others without too much trouble.

-- Michael Chermside



More information about the Python-Dev mailing list