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


Groups > comp.lang.python > #30960

Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?

Date 2012-10-08 10:48 -0400
From Dave Angel <d@davea.name>
Subject Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
References <91ff9b33-c212-4be0-8ed0-1f6b16f56865@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1952.1349707766.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 10/08/2012 10:07 AM, iMath wrote:
> To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
> BTW ,Windows’s calculator get the accurate value ,anyone who knows how to implement it ?

Windows calculator is an application, not a programming language.  Like
all applications, it has to deal with the finite accuracy of the
underlying processor and language, and choose an algorithm that will
please its users.

The Pentium chip (and its equivalents from AMD), used by Windows
machines and most others, has about 18 digits of accuracy in its binary
floating point math.  However, being binary, the data has to be
converted from decimal to binary (when the user types it in) and binary
to decimal (when displaying it).  Either of those conversions may have
quantization errors, and it's up to the program to deal with those or
other inaccuracies.

If you subtract two values, either of which may have quantization
errors, and they are quite close, then the apparent error is
magnified.   Out of your 18 digits internal accuracy, you now have only
about 2.

Therefore many programs more concerned with apparent accuracy will
ignore the binary floating point, and do their work in decimal.  That
doesn't eliminate calculation errors, but only quantization errors. 
That makes the user think he is getting more accuracy than he really is.

Since that seems to be your goal, I suggest you look into the Decimal
class, locating in the stdlib decimal.

import decimal
a = decimal.Decimal(4.3)
print(a)

5.0999999999999996447286321199499070644378662109375

Note that you still seem to have some "error" since the value 4.3 is a binary float, and has already been quantized.  If you want to avoid the binary stuff entirely, try going directly from string to Decimal.

b = decimal.Decimal("5.1")
print(b)

5.1

Back to your original contrived example,

c = decimal.Decimal("1.0")
d = decimal.Decimal("0.999999999999999")
print(c-d)

1E-15

The Decimal class has the disadvantage that it's tons slower on any modern machine I know of, but the advantage that you can specify how much precision you need it to use.  It doesn't eliminate errors at all, just one class of them.

e = decimal.Decimal("3.0")
print(c/e)

0.3333333333333333333333333333

That of course is the wrong answer.  The "right" answer would never stop printing.  We still have a finite number of digits.

print(c/e*e)

0.9999999999999999999999999999

"Fixing" this is subject for another lesson, someday.

-- 

DaveA

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


Thread

To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ? iMath <redstone-cold@163.com> - 2012-10-08 07:07 -0700
  Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ? Dave Angel <d@davea.name> - 2012-10-08 10:48 -0400
  Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ? Chris Angelico <rosuav@gmail.com> - 2012-10-09 02:00 +1100
    Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm � Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-09 11:39 +0000
  Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ? Dave Angel <d@davea.name> - 2012-10-08 11:13 -0400
  Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-10-08 16:44 +0200
  Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ? Chris Angelico <rosuav@gmail.com> - 2012-10-09 02:19 +1100
  Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ? Terry Reedy <tjreedy@udel.edu> - 2012-10-08 21:45 -0400
  Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ? Dave Angel <d@davea.name> - 2012-10-08 22:20 -0400

csiph-web