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


Groups > comp.lang.python > #32967

Re: accuracy problem in calculation

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!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 <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'encoded': 0.05; 'float': 0.05; 'bytes.': 0.07; 'postpone': 0.07; 'python': 0.09; 'oh,': 0.09; 'part,': 0.09; 'stating': 0.09; 'subtract': 0.09; 'yeah,': 0.09; 'cc:addr:python-list': 0.10; 'library': 0.15; '3.3,': 0.16; 'accuracy,': 0.16; 'canceled': 0.16; 'force.': 0.16; 'hits': 0.16; 'hypothetical': 0.16; 'outputs': 0.16; 'parts.': 0.16; 'permits': 0.16; 'precision,': 0.16; 'precision.': 0.16; 'skip:7 20': 0.16; 'takes,': 0.16; 'unnecessary.': 0.16; 'wrote:': 0.17; 'intel': 0.17; 'items.': 0.17; 'processor': 0.17; 'specify': 0.17; 'subject:problem': 0.22; 'cc:2**0': 0.23; 'programming': 0.23; 'nearly': 0.23; 'needed.': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; 'values': 0.26; 'coding': 0.27; 'question': 0.27; 'accuracy': 0.27; 'factor': 0.29; 'measure': 0.29; 'knows': 0.30; 'usually': 0.30; 'skip:( 40': 0.30; 'function': 0.30; 'error': 0.30; 'problem.': 0.32; 'url:python': 0.32; 'could': 0.32; 'problem': 0.33; 'themselves': 0.33; 'acceptable': 0.35; 'so,': 0.35; 'pm,': 0.35; 'table': 0.35; 'similar': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.36; 'level': 0.37; 'two': 0.37; 'being': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'url:docs': 0.38; 'instead': 0.39; 'received:192': 0.39; 'space': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'your': 0.60; 'side': 0.61; "you'll": 0.62; 'distance': 0.62; 'is.': 0.62; 'needing': 0.62; 'different': 0.63; 'more': 0.63; 'decided': 0.65; 'sum': 0.66; 'header:Reply-To:1': 0.68; 'lose': 0.71; 'received:74.208': 0.71; 'increase': 0.72; 'reply-to:no real name:2**0': 0.72; 'different.': 0.84; 'earth.': 0.84; 'fortunately': 0.84; 'trig': 0.84; 'approach.': 0.91; 'hundred': 0.95; 'picture': 0.96
Date Thu, 08 Nov 2012 12:37:52 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To Debashish Saha <silideba@gmail.com>
Subject Re: accuracy problem in calculation
References <CA+b=61CdnBbFdxsO4yz5sK_w2GxOEXTxmBj387EEKwoQzu=W6w@mail.gmail.com>
In-Reply-To <CA+b=61CdnBbFdxsO4yz5sK_w2GxOEXTxmBj387EEKwoQzu=W6w@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:4YvuE1YqHg87TClg/BCrDIsEoEE+gj0d5+VdMJADMnm e5MUl8zTyd+Z0cglLi7JgXTTY2E+FAmhAUNpLwVmm/XQE9a9wO +HqMfU5i2sX52lVthznZDnUSg6BWfCKajzPS0W69qvj5vUr1dV jQ/w6E7VYuugovY2RsE2X2DBL5Vj5gVdC5f9kAXF8W3UDKd2Ce FZz3roplUP9Gr1pWNQ8sHu10zvXn+rRp470RzgWVcANpQ+6B9z J8qA3RcQstbPFqbAMsPXoet+6jetjwjlMUXydAXEQvxP/x1y2V bao9VU/scF6wSAI+avWrBI/wZQYwiAbPJtUO9SubLahZMn/1A= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
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.3456.1352396303.27098.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352396303 news.xs4all.nl 6879 [2001:888:2000:d::a6]:43761
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32967

Show key headers only | View raw


On 11/08/2012 12:05 PM, Debashish Saha 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)

To improve accuracy, do some algebra before coding brute force.  If you
know you'll be subtracting two values that are nearly equal, see if you
can factor out the large part, and only subtract the small parts.

If you have a processor with 16 digits of float accuracy, and you
calculate an 18 digit sum, you're going to lose 3 digts at a minimum. 
Each sum has the same problem.  So now you have lopped off all the
digits that are different.  So subtracting them is meaningless.

Picture needing to know how thick a leaf is.  So measure the distance
from one side of it to the sun.  Then measure the distance from the
other side to the sun.  Now just subtract the answers.  Silly, isn't it?

You have  (a+b) - (a+c), where a is much larger than either b or c.  So
simplify it to b-c before programming it.  Oh, yeah, that's your second
approach.

More generally, if you have a sum of 3 or more values (which you can get
by just stating the problem as a + b + -a + -c), you can usually
minimize error by reordering the arithmetic, based on the sizes of the
items.  Fortunately Python has a library function that knows how to do that:

http://docs.python.org/2/library/math.html#math.fsum

If you want to postpone the problem so instead of hitting at 15 digits
or so, it hits at a few hundred digits, you could use decimal.Decimal,
which is a standard library in Python.  It permits you to specify your
own precision, instead of being what Intel (professor Kahn) decided on
in about 1985.  They constrained themselves to what could be encoded in
8 bytes.  Naturally, if you ask for much higher precision with Decimal,
you'll pay for it with space and/or time.  But apparently in 3.3, they
did a very good job minimizing the time it takes, for reasonable precision.

Interestingly, I got a similar question in about 1977, concerning a trig
package I had microcoded.  A man was measuring the flatness of a
hypothetical level table (which must be curved, of course).  And he did
it with trig, and effectively by subtracting two measurements to the
center of the earth.  I was able to simplify his problem using some
geometry (similar triangles) to get all the accuracy he needed.  And
strangely enough, the trig canceled out and was unnecessary.


-- 

DaveA

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


Thread

Re: accuracy problem in calculation Dave Angel <d@davea.name> - 2012-11-08 12:37 -0500

csiph-web