# Really basic problem

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Oct 8 14:50:53 CEST 2007

```On Mon, 08 Oct 2007 12:09:28 +0200, Andreas Tawn wrote:

> The issue is that 0.1 etc don't have an exact representation as floating
> point.
>
> Interestingly:
>
>>>> 0.10000000000000001 == 0.1
> True
>>>> 0.30000000000000004 == 0.3
> False
>
> I guess this means that Python has some concept of "close enough", but
> I'll have to defer to someone more knowledgeable to explain that.

No, not really, except in the sense that any floating point calculation
will be necessarily imprecise in that sense.

The first example: when you type 0.1 as a float, it can't be stored as
exactly 1/10 in binary, so you actually get a tiny bit more than 1/10:

>>> 0.1
0.10000000000000001

That means that whether you type 0.1 or 0.10000000000000001 makes no
difference. Hence the following result:

>>> 0.10000000000000001 == 0.1
True

It isn't that Python looks at 0.10000000000000001 and 0.1 and says "well,
the difference is only 0.00000000000000001 which is too tiny to worry
about, so they're equal". Proof by example:

>>> 0.10000000000000001 == 0.10000000000000002
False

The second example: when you type 0.3, it too can't be represented
precisely in binary as 3/10, and so you get this:

>>> 0.3
0.29999999999999999
>>> 0.30000000000000001
0.29999999999999999

So typing 0.3 is the same as typing 0.29999999999999999 or
0.30000000000000001 as far as floating point binary values are concerned.

(Although note that these results are platform dependent. Your mileage
may vary.)

--
Steven

```