Simple question

Tim Chase python.list at tim.thechases.com
Tue Apr 15 14:33:40 EDT 2014


On 2014-04-15 19:18, Phil Dobbin wrote:
> I'm confused as to this part:
> 
> '>>> 0.1 + 0.1 + 0.1 - 0.3  
> 5.55111.....'
>
> What I'm wondering is why the first calculation that arrives at
> '5.55111...' is so far out?

You omit one key detail in your "....", the "e-17" which means that
is 5.55111... times 10^-17 which is a REALLY small number.  To
better show that, have it formatted to place:

  >>> "%55.55f" % (0.1 + 0.1 + 0.1 - 0.3)
  '0.0000000000000000555111512312578270211815834045410156250'

For most engineering purposes, that's effectively zero.

It comes about because of how floating-point numbers are represented
internally.  You can read a lot more than anybody should want to
know at [1].  By using the Decimal module, you remove some of that
imprecision.

-tkc

[1] http://en.wikipedia.org/wiki/Ieee_float








More information about the Python-list mailing list