Decimal vs float

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Jan 20 22:28:20 EST 2006


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 ghaering.de> 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.



-- 
Steven.




More information about the Python-list mailing list