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


Groups > comp.lang.python > #71766

Re: Bug in Decimal??

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'causing': 0.04; 'string.': 0.05; 'intermediate': 0.07; 'see:': 0.07; 'back.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'term,': 0.09; 'terms,': 0.09; 'python': 0.11; 'alpha,': 0.16; 'compute': 0.16; 'garbage': 0.16; 'incorrect': 0.16; 'message-id:@post.gmane.org': 0.16; 'propagation': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'rounding': 0.16; 'surprising': 0.16; 'truncate': 0.16; 'truncating': 0.16; '>>>': 0.22; 'platforms': 0.22; 'example': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; '(by': 0.24; 'simpler': 0.24; "i've": 0.25; 'compare': 0.26; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'testing': 0.29; 'expansion': 0.30; 'along': 0.30; "i'm": 0.30; 'getting': 0.31; 'that.': 0.31; 'decimal': 0.31; 'end,': 0.31; 'occurs': 0.31; 'writes:': 0.31; 'compatible': 0.32; "we're": 0.32; 'another': 0.32; 'but': 0.35; 'leads': 0.36; 'sequence': 0.36; 'charset:us-ascii': 0.36; 'example,': 0.37; 'wrong': 0.37; 'too': 0.37; 'two': 0.37; 'apple': 0.38; 'confirmed': 0.38; 'received:211': 0.38; 'to:addr :python-list': 0.38; 'expect': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'how': 0.40; 'middle': 0.60; "you're": 0.61; 'further': 0.61; 'first': 0.61; 'here:': 0.62; "you'll": 0.62; 'more': 0.64; 'taking': 0.65; 'afraid': 0.65; 'series': 0.66; 'here': 0.66; 'between': 0.67; 'mar': 0.68; 'power': 0.76; '2014,': 0.84; '4.2.1': 0.84; '4/3': 0.84; 'hand,': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Dickinson <mdickinson@enthought.com>
Subject Re: Bug in Decimal??
Date Mon, 19 May 2014 16:10:26 +0000 (UTC)
References <973d2677-03c6-464a-8c88-f07282806468@googlegroups.com> <mailman.9672.1399200803.18130.python-list@python.org> <b8d4de13-70b1-40b3-8216-af721000f667@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host sea.gmane.org
User-Agent Loom/3.14 (http://gmane.org/)
X-Loom-IP 213.1.240.226 (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10138.1400515847.18130.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1400515847 news.xs4all.nl 2952 [2001:888:2000:d::a6]:33649
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:71766

Show key headers only | View raw


<pleasedontspam <at> isp.com> writes:

> I've tested on all platforms I know of and confirmed it. The wrong digit
> occurs in the middle of the number.  Propagation error would have a bad digit
> near the end, and garbage after that. Here there's a perfect sequence of
> numbers, but with one single digit changed in the middle of the number.  No
> error propagation in a series expansion can do that.

I can see how it might be surprising if you don't think about it too hard, but
I'm afraid that you're wrong here: error propagation is *exactly* what's
causing the effects you're seeing.

Here's another way of looking at it: if you truncate the Taylor series about 0
for (1 + x) / (1 - x) to k (>= 1) terms, you get the polynomial (1 + x - 2x^k)
/ (1 - x).  For example, taking k to be 3, we're getting (1 + x - 2x^3) / (1 -
x).  Given that the particular value of x you're testing with has the form
10**<negative>, rounding your intermediate result to the working precision has
exactly the effect of truncating the series at some k.

Now you can compute and compare (by hand, via Wolfram alpha, or however you
like) the Taylor series expansions for log((1 + x) / (1 - x)) and log((1 + x -
2x^3) / (1 - x)).  For the first you'll see:

  2x + 2/3 x^3  + 2/5 x^5 - 2/7 x^7 + 2/9 x^9 - ...

and for the second you'll get:

  2x - 4/3 x^3 + 2 x^4 - 8/5 x^5 + 16/7 x^7 - ...

The difference between the two series is:

  -2x^3 + 2x^4 - 2x^5 + 2x^7 - 4x^8 + ...

So again with x a small power of 10, you're going to see a single-digit error
from the -2x^3 term, and another single-digit error further along from
the 2x^3 term, and so on.

Here's a simpler example of the same phenomenon.  Note how the error propagation
leads to a single incorrect digit in the *middle* of the digit string.

Python 3.4.0 (default, Mar 25 2014, 11:07:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> x = Decimal('1e-15')
>>> y = (1 - 2 * x) / (1 - x)
>>> 2 * x + (y - 1) * (1 - x)  # Mathematically, expect to get 'x' back.
Decimal('1.000000000000001000000000000E-15')
>>> x
Decimal('1E-15')

-- 
Mark

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


Thread

Bug in Decimal?? pleasedontspam@isp.com - 2014-04-29 19:37 -0700
  Re: Bug in Decimal?? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-30 03:39 +0000
    Re: Bug in Decimal?? pleasedontspam@isp.com - 2014-04-30 05:01 -0700
    Re: Bug in Decimal?? pleasedontspam@isp.com - 2014-04-30 05:13 -0700
  Re: Bug in Decimal?? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-30 19:59 +1200
  Re: Bug in Decimal?? Mark Dickinson <mdickinson@enthought.com> - 2014-05-04 10:53 +0000
    Re: Bug in Decimal?? pleasedontspam@isp.com - 2014-05-15 13:45 -0700
      Re: Bug in Decimal?? Mark H Harris <harrismh777@gmail.com> - 2014-05-15 16:26 -0500
      Re: Bug in Decimal?? Mark Dickinson <mdickinson@enthought.com> - 2014-05-19 16:10 +0000

csiph-web