Re: Math errors in python

From: Gary Herron (gherron_at_islandtraining.com)
Date: 09/19/04


To: "Chris S." <chrisks@NOSPAM.udel.edu>, python-list@python.org
Date: Sun, 19 Sep 2004 02:34:00 -0700

On Sunday 19 September 2004 01:00 am, Chris S. wrote:
> Gary Herron wrote:
> > That's called rational arithmetic, and I'm sure you can find a package
> > that implements it for you. However what would you propose for
> > irrational numbers like sqrt(2) and transcendental numbers like PI?
>
> Sqrt is a fair criticism, but Pi equals 22/7,

What? WHAT? Are you nuts? Pi and 22/7 are most certainly not equal.
They don't even share three digits beyond the decimal point. (Can you
really be that ignorant about numbers and expect to contribute
intelligently to a discussion about numbers. Pi is a non-repeating
and non-ending number in base 10 or any other base.)

> exactly the form this
> arithmetic is meant for. Any decimal can be represented by a fraction,
> yet not all fractions can be represented by decimals. My point is that
> such simple accuracy should be supported out of the box.
>
> > While I'd love to compute with all those numbers in infinite
> > precision, we're all stuck with FINITE sized computers, and hence with
> > the inaccuracies of finite representations of numbers.
>
> So are our brains, yet we somehow manage to compute 12.10 + 8.30
> correctly using nothing more than simple skills developed in
> grade-school. You could theoretically compute an infinitely long
> equation by simply operating on single digits, yet Python, with all of
> its resources, can't overcome this hurtle?
>
> However, I understand Python's limitation in this regard. This
> inaccuracy stems from the traditional C mindset, which typically
> dismisses any approach not directly supported in hardware. As the FAQ
> states, this problem is due to the "underlying C platform". I just find
> it funny how a $20 calculator can be more accurate than Python running
> on a $1000 Intel machine.

If you are happy doing calculations with decimal numbers like 12.10 +
8.30, then the Decimal package may be what you want, but that fails as
soon as you want 1/3. But then you could use a rational arithmetic
package and get 1/3, but that would fail as soon as you needed sqrt(2)
or Pi. But then you could try ... what? Can you see the pattern
here? Any representation of the infinity of numbers on a finite
computer *must* necessarily be unable to represent some (actually
infinity many) of those numbers. The inaccuracies stem from that
fact.

Hardware designers have settled on a binary representation of floating
point numbers, and both C and Python use the underlying hardware
implementation. (Try your calculation in C -- you'll get the same
result if you choose to print out enough digits.)

And BTW, your calculator is not, in general, more accurate than the
modern IEEE binary hardware representation of numbers used on most of
today's computers. It is more accurate on only a select subset of all
numbers, and it does a good job of fooling you in those cases where it
loses accuracy, by doing calculations on more digits then it displays,
and rounding off to the on-screen digits.

So while a calculator will fool you into believing it is accurate when
it is not, it is Python's design decision to not cater to fools.

Dr Gary Herron



Relevant Pages

  • Re: Time Accuracy
    ... seconds is no less "reliable" than the representation of 0.1 seconds, ... theoretically be handled by the available decimals. ... the format of the time component is just 5 decimal fractions. ... numeric format to 15 significant digits is 39890.5031526620. ...
    (microsoft.public.excel.misc)
  • Re: Time Accuracy
    ... That means the 864,000 tenths of a second in one day can theoretically be handled by the available decimals. ... Perhaps I should have said unreliable digits rather than unreliable numbers. ... oddities of IEEE floating point representation. ... raised issues about the resolution of the Excel NOWfunction. ...
    (microsoft.public.excel.misc)
  • Re: ((.99999.... == 1) == 1) = 1
    ... >Using Powertoys Calculator on Windows XP, I can disply up to 140 decimal ... as many digits as you want. ... 140 come no where near an infinite number of decimals. ...
    (sci.math)
  • Re: What is magnitude or size of a number?
    ... reals is depicted as points upon a numberline. ... label them using strings of digits may depend on the base, ... no base for which the digit representation terminates. ... Don't find it by going one place at a time, using decimals. ...
    (sci.math)
  • Re: Improve Excel accuracy over 15 digits
    ... We regain all the lost productivity (because of not understanding how Excel calculates) in no time. ... but my calculator can do it!" ... That last group is often confused because binary representation and ... With that many digits I can get>> the ...
    (microsoft.public.excel.misc)