[Python-Dev] Decimal <-> float comparisons in py3k.

Steven D'Aprano steve at pearwood.info
Wed Mar 17 11:45:51 CET 2010

On Wed, 17 Mar 2010 12:27:01 pm Raymond Hettinger wrote:
> On Mar 16, 2010, at 3:16 PM, Greg Ewing wrote:
> > Seems to me that this education would mostly consist of saying
> > "don't compare floats and decimals", which is why I think that
> > disallowing them in the first place would be better.
> That makes sense.
> I do worry that 2.x currently does make the comparison
> and gives the wrong answer.  We have the ability to
> make it a correct answer.  But, it seems like the mood
> here is that wrong-is-better-than-right for an action
> that someone shouldn't be doing in the first place.

I don't get this.

Why is it "wrong" to compare Decimals to floats, and why shouldn't I do 
so? What harm is there?

If the argument is that naive users who don't understand floats may be 
confused by the results, then the problem lies with floats, and if you 
really want to avoid confusing the float-naive, then we should prohibit 
all comparisons on floats:

>>> 1e20 + 1e4 < 1e20 + 2e4

I don't mean that as a serious suggestion -- it would be absurd to 
cripple floats for the sake of avoiding confusion of those who don't 
understand floats. Why are Decimals different?

I can't see why comparing Decimal(1) to float(1) is wrong in any sense. 
I can see that comparing Decimal("1.1") to float("1.1") may confuse the 
float-naive, but the float naive will be confused by this too:

>>> x = 1.0/3
>>> x + 1.0 - 1.0 == x

There's an awful lot about floats that is confusing to naive users, I 
don't see that the behaviour of Decimals will make it worse.

Steven D'Aprano

More information about the Python-Dev mailing list