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


Groups > comp.lang.python > #20960

Re: Python math is off by .000000000000045

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <torriem@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'subject:Python': 0.05; 'interpreter.': 0.07; 'say.': 0.07; 'python': 0.08; '>>>>': 0.09; 'am,': 0.12; 'binary': 0.13; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'hex': 0.16; 'sequence,': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'trying': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'sat,': 0.25; "i'm": 0.28; 'subject: .': 0.29; 'ball': 0.30; 'finite': 0.30; 'message- id:@gmail.com': 0.31; 'quite': 0.31; 'idea': 0.32; 'there': 0.33; 'header:User-Agent:1': 0.33; 'direction': 0.34; 'showing': 0.34; 'steven': 0.34; 'numbers.': 0.34; 'to:addr:python-list': 0.35; 'things': 0.35; 'post': 0.36; 'question': 0.36; 'received:org': 0.36; "he's": 0.37; 'sequence': 0.37; 'enough': 0.38; 'received:192': 0.38; 'doing': 0.38; 'should': 0.38; 'clearly': 0.39; 'that.': 0.39; 'to:addr:python.org': 0.40; 'back': 0.60; 'simple': 0.61; 'your': 0.61; 'afraid': 0.63; 'exact': 0.68; '10:28': 0.84; 'readers.': 0.84
X-Virus-Scanned amavisd-new at torriefamily.org
Date Mon, 27 Feb 2012 17:53:41 -0700
From Michael Torrie <torriem@gmail.com>
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.24) Gecko/20111108 Fedora/3.1.16-1.fc14 Lightning/1.0b3pre Thunderbird/3.1.16
MIME-Version 1.0
To python-list@python.org
Subject Re: Python math is off by .000000000000045
References <mailman.49.1329934400.3037.python-list@python.org> <ji3kb2$hk3$1@reader1.panix.com> <wh92r.13653$1I2.11412@newsfe08.iad> <mailman.162.1330203944.3037.python-list@python.org> <aa5ca147-b5ed-4f8c-935d-48b2c2cfac87@gr6g2000vbb.googlegroups.com> <4f4965f5$0$29989$c3e8da3$5496439d@news.astraweb.com> <cbd3219a-d379-4499-94ea-c4d0178a498f@db5g2000vbb.googlegroups.com> <4F4BBD4C.1050603@stoneleaf.us>
In-Reply-To <4F4BBD4C.1050603@stoneleaf.us>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.210.1330390434.3037.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330390434 news.xs4all.nl 6947 [2001:888:2000:d::a6]:34434
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20960

Show key headers only | View raw


On 02/27/2012 10:28 AM, Ethan Furman wrote:
> jmfauth wrote:
>> On 25 fév, 23:51, Steven D'Aprano <steve
>> +comp.lang.pyt...@pearwood.info> wrote:
>>> On Sat, 25 Feb 2012 13:25:37 -0800, jmfauth wrote:
>>>>>>> (2.0).hex()
>>>> '0x1.0000000000000p+1'
>>>>>>> (4.0).hex()
>>>> '0x1.0000000000000p+2'
>>>>>>> (1.5).hex()
>>>> '0x1.8000000000000p+0'
>>>>>>> (1.1).hex()
>>>> '0x1.199999999999ap+0'
>>>> jmf
>>> What's your point? I'm afraid my crystal ball is out of order and I have
>>> no idea whether you have a question or are just demonstrating your
>>> mastery of copy and paste from the Python interactive interpreter.
>>
>> It should be enough to indicate the right direction
>> for casual interested readers.
> 
> I'm a casual interested reader and I have no idea what your post is 
> trying to say.

He's simply showing you the hex (binary) representation of the
floating-point number's binary representation.  As you can clearly see
in the case of 1.1, there is no finite sequence that can store that.
You end up with repeating numbers.  Just like 1/3, when represented in
base 10 fractions (x1/10 + x2/100, x3/1000, etc), is a repeating
sequence, the number base 10 numbers 1.1 or 0.2, or many others that are
represented by exact base 10 fractions, end up as repeating sequences in
base 2 fractions.  This should help you understand why you get errors
doing simple things like x/y*y doesn't quite get you back to x.

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