Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70284

Re: Simple question

Date 2014-04-15 13:33 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Simple question
References <534D7807.6050408@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.9289.1397586829.18130.python-list@python.org> (permalink)

Show all headers | View raw


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




Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Simple question Tim Chase <python.list@tim.thechases.com> - 2014-04-15 13:33 -0500

csiph-web