Decimal vs float

Steven D'Aprano steve at
Sat Jan 21 05:08:40 CET 2006

On Sat, 21 Jan 2006 03:48:26 +0000, Steve Holden wrote:

> Steven D'Aprano wrote:
>> On Fri, 20 Jan 2006 04:25:01 +0000, Bengt Richter wrote:
>>>On Thu, 19 Jan 2006 12:16:22 +0100, =?ISO-8859-1?Q?Gerhard_H=E4ring?= <gh at> wrote:
>>>>floating points are always imprecise, so you wouldn't want them as an 
>>>Please, floating point is not "always imprecise." In a double there are
>>>64 bits, and most patterns represent exact rational values. Other than
>>>infinities and NaNs, you can't pick a bit pattern that doesn't have
>>>a precise, exact rational value. 
>> Of course every float has a precise rational value.
>> 0.1000000000000000000001 has a precise rational value:
>> 1000000000000000000001/10000000000000000000000
>> But that's hardly what people are referring to. The question isn't whether
>> every float is an (ugly) rational, but whether every (tidy) rational is a
>> float. And that is *not* the case, simple rationals like 1/10 cannot be
>> written precisely as floats no matter how many bits you use. 
>>>You can't represent all arbitarily chosen reals exactly as floats, that's true,
>>>but that's not the same as saying that "floating points are always imprecise."
>> "Always" is too strong, since (for example) 1/2 can be represented
>> precisely as a float. But in general, for any "random" rational value N/M,
>> the odds are that it cannot be represented precisely as a float. And
>> that's what people mean when they say floats are imprecise.
> And you thought Bengt didn't know that?

I didn't know what to think.

Given the question "I want 0.1, but my float has the value 0.100...01,
why does Python have a bug?" comes up all the time, does it really help
to point out that the float representing 0.100...01 is an exact rational
-- especially without mentioning that it happens to be the *wrong* exact

I won't even try to guess what Bengt does or doesn't know, but he seems to
be implying that while floats can't represent arbitrary reals (like
sqrt(3) or pi) exactly, exact rationals are no problem at all. (Certainly
every one of his examples are of rationals which floats do represent
exactly.) In any case, Bengt isn't the only person reading the thread.
Don't they deserve a clarification?


More information about the Python-list mailing list