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


Groups > comp.lang.python > #32964

Re: accuracy problem in calculation

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '64-bit': 0.07; 'python': 0.09; 'accuracy.': 0.09; 'options.': 0.15; '3.3,': 0.16; 'accuracy,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'losing': 0.16; 'outputs': 0.16; 'skip:( 90': 0.16; 'skip:7 20': 0.16; 'tying': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'import': 0.21; 'received:209.85.214.174': 0.21; 'fraction': 0.22; 'subject:problem': 0.22; 'script': 0.24; 'header:In-Reply- To:1': 0.25; 'am,': 0.27; 'separate': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'run': 0.28; 'decimal': 0.29; 'probably': 0.29; 'fri,': 0.30; 'skip:( 40': 0.30; 'switch': 0.32; 'to:addr:python-list': 0.33; 'point.': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'acceptable': 0.35; 'nov': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'should': 0.36; 'too': 0.36; 'possible': 0.37; 'two': 0.37; 'ones': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'build': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'different': 0.63; 'more': 0.63; 'lose': 0.71; 'increase': 0.72
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=0WItBQm8g+KC/pqrn6oIH8BYsHks036Dr3/RPsidzkM=; b=TxJBeAzfiiIJUX5CBFRxFgNAJPe7+AyIOcEqA4UufGhW9AgF5dpoF2RsSo9c2/SnPe dJxVEHqg4jKcZZEpN1s3oovdwJFzYMx/8i1tnJTcMb36W55tFC8Y5HapEaFtMyzz+5mL v7u5ygqV8s/grNm0FQeHRm8r8hZp1RFawBpL073uHbJcdzbtiGoQ5OeLPps6mv+fbJ2e UCjimN+0LLpWd3t7/VuPzYkBH5jh+Z4uNxAvG4hpFmsy/eRX6GKsJDa9SbWmD1N1MgfF dVYTlXYvYkuq+IHOitYJvCET7XKbr/HSqVG2hkjd/pGz0kusQu/V2uAbKoZQNBfFV7Dh yLNQ==
MIME-Version 1.0
In-Reply-To <CA+b=61CdnBbFdxsO4yz5sK_w2GxOEXTxmBj387EEKwoQzu=W6w@mail.gmail.com>
References <CA+b=61CdnBbFdxsO4yz5sK_w2GxOEXTxmBj387EEKwoQzu=W6w@mail.gmail.com>
Date Fri, 9 Nov 2012 04:13:33 +1100
Subject Re: accuracy problem in calculation
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3452.1352394817.27098.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352394817 news.xs4all.nl 6917 [2001:888:2000:d::a6]:32925
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32964

Show key headers only | View raw


On Fri, Nov 9, 2012 at 4:05 AM, Debashish Saha <silideba@gmail.com> wrote:
> (1500000000+1.00067968)-(1500000000+1.00067961)
> Out[102]: 2.384185791015625e-07
>
> 1.00067968-(1.00067961)
> Out[103]: 7.000000001866624e-08
>
> above i am showing the two different results,though the two outputs
> should be same if we do it in copy(the lass one is acceptable value).
> so my question is how to increase the accuracy(windows7(32bit)
> ,python2.7.2)

Welcome to floating point. You're working with very large and very
small numbers, and you _will_ lose accuracy.

There are a few options. It's possible that a 64-bit build of Python
will give you more accuracy, but better would be to separate your huge
numbers from your tiny ones and work with them separately.
Alternatively, switch to the Decimal or Fraction types, but be aware
that your script will probably run a lot slower.

>>> from decimal import Decimal
>>> (Decimal("1500000000")+Decimal("1.00067968"))-(Decimal("1500000000")+Decimal("1.00067961"))
Decimal('7E-8')
>>> Decimal("1.00067968")-Decimal("1.00067961")
Decimal('7E-8')

Unless something's tying you to Python 2, consider moving to Python 3.
You may find that, on Python 3.3, you can switch to Decimal without
losing too much performance.

ChrisA

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


Thread

Re: accuracy problem in calculation Chris Angelico <rosuav@gmail.com> - 2012-11-09 04:13 +1100
  Re: accuracy problem in calculation Grant Edwards <invalid@invalid.invalid> - 2012-11-08 18:17 +0000

csiph-web