Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: accuracy problem in calculation

Reply
Thread Tools

Re: accuracy problem in calculation

 
 
Chris Angelico
Guest
Posts: n/a
 
      11-08-2012
On Fri, Nov 9, 2012 at 4:05 AM, Debashish Saha <> wrote:
> (1500000000+1.0006796-(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
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      11-08-2012
On 2012-11-08, Chris Angelico <> wrote:
> On Fri, Nov 9, 2012 at 4:05 AM, Debashish Saha <> wrote:


>> (1500000000+1.0006796-(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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: accuracy problem in calculation Dave Angel Python 0 11-08-2012 05:37 PM
accuracy problem in calculation Debashish Saha Python 0 11-08-2012 05:05 PM
Problem with accuracy in DEV C++ Hatzigiannakis Nikos C Programming 10 08-06-2008 10:59 AM
Accuracy and CSS =?Utf-8?B?Q2hhcmxlc0E=?= ASP .Net 5 01-17-2006 12:47 PM
accuracy problem Eric Wilhelm Perl Misc 12 11-17-2003 06:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57