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


Groups > comp.lang.python > #20914

Re: Python math is off by .000000000000045

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Python math is off by .000000000000045
Date 2012-02-26 20:30 -0500
References <mailman.49.1329934400.3037.python-list@python.org> <99543cae-b494-409e-9ac1-073639dcf224@p7g2000yqk.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.185.1330306213.3037.python-list@python.org> (permalink)

Show all headers | View raw


On 2/26/2012 7:24 PM, John Ladasky wrote:

 > I always found it helpful to ask someone who is confused by this issue
 > to imagine what the binary representation of the number 1/3 would be.
 >
 > 0.011 to three binary digits of precision:
 > 0.0101 to four:
 > 0.01011 to five:
 > 0.010101 to six:
 > 0.0101011 to seven:
 > 0.01010101 to eight:
 >
 > And so on, forever.  So, what if you want to do some calculator-style
 > math with the number 1/3, that will not require an INFINITE amount of
 > time?  You have to round.  Rounding introduces errors.  The more
 > binary digits you use for your numbers, the smaller those errors will
 > be.  But those errors can NEVER reach zero in finite computational
 > time.

Ditto for 1/3 in decimal.
...
0.33333333 to eitht

> If ALL the numbers you are using in your computations are rational
> numbers, you can use Python's rational and/or decimal modules to get
> error-free results.

Decimal floats are about as error prone as binary floats. One can only 
exact represent a subset of rationals of the form n / (2**j * 5**k). For 
a fixed number of bits of storage, they are 'lumpier'. For any fixed 
precision, the arithmetic issues are the same.

The decimal module decimals have three advantages (sometimes) over floats.

1. Variable precision - but there are multiple-precision floats also 
available outside the stdlib.

2. They better imitate calculators - but that is irrelevant or a minus 
for scientific calculation.

3. They better follow accounting rules for financial calculation, 
including a multiplicity of rounding rules. Some of these are laws that 
*must* be followed to avoid nasty consequences. This is the main reason 
for being in the stdlib.

 > Learning to use them is a bit of a specialty.

Definitely true.

-- 
Terry Jan Reedy

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


Thread

Python math is off by .000000000000045 Alec Taylor <alec.taylor6@gmail.com> - 2012-02-23 05:13 +1100
  Re: Python math is off by .000000000000045 nn <pruebauno@latinmail.com> - 2012-02-22 10:29 -0800
  Re: Python math is off by .000000000000045 Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-02-22 20:44 +0200
  Re: Python math is off by .000000000000045 Grant Edwards <invalid@invalid.invalid> - 2012-02-22 20:48 +0000
    Re: Python math is off by .000000000000045 Tobiah <toby@tobiah.org> - 2012-02-25 09:56 -0800
      Re: Python math is off by .000000000000045 Tim Wintle <tim.wintle@teamrubber.com> - 2012-02-25 19:08 +0000
      Re: Python math is off by .000000000000045 Terry Reedy <tjreedy@udel.edu> - 2012-02-25 16:05 -0500
        Re: Python math is off by .000000000000045 jmfauth <wxjmfauth@gmail.com> - 2012-02-25 13:25 -0800
          Re: Python math is off by .000000000000045 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-25 22:51 +0000
            Re: Python math is off by .000000000000045 jmfauth <wxjmfauth@gmail.com> - 2012-02-26 01:59 -0800
              Re: Python math is off by .000000000000045 Ethan Furman <ethan@stoneleaf.us> - 2012-02-27 09:28 -0800
              Re: Python math is off by .000000000000045 Michael Torrie <torriem@gmail.com> - 2012-02-27 17:53 -0700
              Re: Python math is off by .000000000000045 Ethan Furman <ethan@stoneleaf.us> - 2012-02-28 09:56 -0800
      Re: Python math is off by .000000000000045 Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-02-25 21:49 -0500
      Re: Python math is off by .000000000000045 Terry Reedy <tjreedy@udel.edu> - 2012-02-26 00:44 -0500
  Re: Python math is off by .000000000000045 John Ladasky <ladasky@my-deja.com> - 2012-02-26 16:24 -0800
    Re: Python math is off by .000000000000045 Terry Reedy <tjreedy@udel.edu> - 2012-02-26 20:30 -0500
    Re: Python math is off by .000000000000045 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-27 03:28 +0000
      Re: Python math is off by .000000000000045 Grant Edwards <invalid@invalid.invalid> - 2012-02-27 15:02 +0000
        Re: Python math is off by .000000000000045 Michael Torrie <torriem@gmail.com> - 2012-02-27 08:34 -0700

csiph-web