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


Groups > comp.lang.python > #73482

Re: python 3.44 float addition bug?

From Ned Deily <nad@acm.org>
Subject Re: python 3.44 float addition bug?
Date 2014-06-21 12:24 -0700
References <XnsA35313E634BA0fraserlonggmailcom34@216.196.109.145> <CAPTjJmrkPd5K__h9Qg12Q+AraFZVaN6eGUdTmEDGe2ccAqEmRw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.11179.1403378678.18130.python-list@python.org> (permalink)

Show all headers | View raw


In article 
<CAPTjJmrkPd5K__h9Qg12Q+AraFZVaN6eGUdTmEDGe2ccAqEmRw@mail.gmail.com>,
 Chris Angelico <rosuav@gmail.com> wrote:
> Also, when you're looking at how things print out, consider looking at
> two things: the str() and the repr(). Sometimes just "print(p)"
> doesn't give you all the info, so you might instead want to write your
> loop thus:
> 
> z = 0.01
> p = 0.0
> for i in range(19):
>     p += z
>     print(str(p) + " -- " + repr(p)) 
> Sometimes you can get extra clues that way, although in this instance
> I think you won't.

Actually, I think this is one case where you would get extra clues (or 
extra headscratching) if you run the code with various releases of 
Python.

$ python2.6 b.py
0.01 -- 0.01
0.02 -- 0.02
0.03 -- 0.029999999999999999
0.04 -- 0.040000000000000001
0.05 -- 0.050000000000000003
0.06 -- 0.060000000000000005
0.07 -- 0.070000000000000007
0.08 -- 0.080000000000000002
0.09 -- 0.089999999999999997
0.1 -- 0.099999999999999992
0.11 -- 0.10999999999999999
0.12 -- 0.11999999999999998
0.13 -- 0.12999999999999998
0.14 -- 0.13999999999999999
0.15 -- 0.14999999999999999
0.16 -- 0.16
0.17 -- 0.17000000000000001
0.18 -- 0.18000000000000002
0.19 -- 0.19000000000000003

$ python2.7 b.py
0.01 -- 0.01
0.02 -- 0.02
0.03 -- 0.03
0.04 -- 0.04
0.05 -- 0.05
0.06 -- 0.060000000000000005
0.07 -- 0.07
0.08 -- 0.08
0.09 -- 0.09
0.1 -- 0.09999999999999999
0.11 -- 0.10999999999999999
0.12 -- 0.11999999999999998
0.13 -- 0.12999999999999998
0.14 -- 0.13999999999999999
0.15 -- 0.15
0.16 -- 0.16
0.17 -- 0.17
0.18 -- 0.18000000000000002
0.19 -- 0.19000000000000003

$ python3.4 b.py
0.01 -- 0.01
0.02 -- 0.02
0.03 -- 0.03
0.04 -- 0.04
0.05 -- 0.05
0.060000000000000005 -- 0.060000000000000005
0.07 -- 0.07
0.08 -- 0.08
0.09 -- 0.09
0.09999999999999999 -- 0.09999999999999999
0.10999999999999999 -- 0.10999999999999999
0.11999999999999998 -- 0.11999999999999998
0.12999999999999998 -- 0.12999999999999998
0.13999999999999999 -- 0.13999999999999999
0.15 -- 0.15
0.16 -- 0.16
0.17 -- 0.17
0.18000000000000002 -- 0.18000000000000002
0.19000000000000003 -- 0.19000000000000003

What's going on here is that in Python 2.7 the repr() of floats was 
changed to use the minimum number of digits to accurately roundtrip the 
number under correct rounding.  For compatibility reasons, the str() 
representation was not changed for 2.7.  But in Python 3.2, str() was 
changed to be identical to repr() for floats.  It's important to keep in 
mind that the actual binary values stored in float objects are the same 
across all of these releases; only the representation of them as decimal 
characters varies.

https://docs.python.org/2.7/whatsnew/2.7.html#other-language-changes

http://bugs.python.org/issue9337

-- 
 Ned Deily,
 nad@acm.org

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


Thread

python 3.44 float addition bug? FraserL <fraser.long+usenet@NOSPAMgmail.com> - 2014-06-20 19:57 -0500
  Re: python 3.44 float addition bug? FraserL <fraser.long+usenet@gmail.com> - 2014-06-20 20:11 -0500
    Re: python 3.44 float addition bug? Gary Herron <gary.herron@islandtraining.com> - 2014-06-20 18:19 -0700
  Re: python 3.44 float addition bug? Gary Herron <gary.herron@islandtraining.com> - 2014-06-20 18:07 -0700
  Re: python 3.44 float addition bug? Chris Angelico <rosuav@gmail.com> - 2014-06-21 11:33 +1000
  Re: python 3.44 float addition bug? INADA Naoki <songofacandy@gmail.com> - 2014-06-21 10:06 +0900
  Re: python 3.44 float addition bug? Grant Edwards <invalid@invalid.invalid> - 2014-06-21 14:25 +0000
  Re: python 3.44 float addition bug? Ned Deily <nad@acm.org> - 2014-06-21 12:24 -0700
    Re: python 3.44 float addition bug? buck <workitharder@gmail.com> - 2014-06-23 17:55 -0700
      Re: python 3.44 float addition bug? Chris Angelico <rosuav@gmail.com> - 2014-06-24 13:26 +1000
      Re: python 3.44 float addition bug? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-06-24 17:30 +1200
      Re: python 3.44 float addition bug? Steven D'Aprano <steve@pearwood.info> - 2014-06-24 06:34 +0000
  Re: python 3.44 float addition bug? Maciej Dziardziel <fiedzia@gmail.com> - 2014-06-25 14:12 -0700
    Re: python 3.44 float addition bug? Steven D'Aprano <steve@pearwood.info> - 2014-06-26 02:56 +0000
      Re: python 3.44 float addition bug? Chris Angelico <rosuav@gmail.com> - 2014-06-26 13:13 +1000
        Re: python 3.44 float addition bug? Steven D'Aprano <steve@pearwood.info> - 2014-06-26 04:17 +0000
          Re: python 3.44 float addition bug? Chris Angelico <rosuav@gmail.com> - 2014-06-26 14:41 +1000
      Re: python 3.44 float addition bug? Ben Finney <ben@benfinney.id.au> - 2014-06-26 13:39 +1000
        Re: python 3.44 float addition bug? Steven D'Aprano <steve@pearwood.info> - 2014-06-26 09:15 +0000
          Re: python 3.44 float addition bug? Chris Angelico <rosuav@gmail.com> - 2014-06-26 19:38 +1000
            Re: python 3.44 float addition bug? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-27 02:51 +0000
              Re: python 3.44 float addition bug? Chris Angelico <rosuav@gmail.com> - 2014-06-27 13:24 +1000
              Re: python 3.44 float addition bug? Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-27 10:18 -0600
      Re: python 3.44 float addition bug? Stefan Behnel <stefan_ml@behnel.de> - 2014-06-26 07:53 +0200

csiph-web