Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: accuracy problem in calculation

 
 
Dave Angel
Guest
Posts: n/a
 
      11-08-2012
On 11/08/2012 12:05 PM, 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)


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

 
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 Chris Angelico Python 1 11-08-2012 06:17 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