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


Groups > comp.lang.python > #32964 > unrolled thread

Re: accuracy problem in calculation

Started byChris Angelico <rosuav@gmail.com>
First post2012-11-09 04:13 +1100
Last post2012-11-08 18:17 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#32964 — Re: accuracy problem in calculation

FromChris Angelico <rosuav@gmail.com>
Date2012-11-09 04:13 +1100
SubjectRe: accuracy problem in calculation
Message-ID<mailman.3452.1352394817.27098.python-list@python.org>
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

[toc] | [next] | [standalone]


#32969

FromGrant Edwards <invalid@invalid.invalid>
Date2012-11-08 18:17 +0000
Message-ID<k7gt0c$l48$1@reader1.panix.com>
In reply to#32964
On 2012-11-08, Chris Angelico <rosuav@gmail.com> wrote:
> 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).

Then do it the way you did the last one.

Seriously, that's the answer they teach you in numerical analysis
classes.

>> 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,

Pretty doubtful.  64-bit and 32-bit builds on all common OSes and
hardware are both going to use 64-bit IEEE floating point.

> 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.

Or admit to yourself that the measurements that produce your input
data just aren't that accurate anyway and forget about it.  :)

-- 
Grant Edwards               grant.b.edwards        Yow! Bo Derek ruined
                                  at               my life!
                              gmail.com            

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web