Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '0.6': 0.09; 'compute': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:()': 0.09; 'tends': 0.09; 'underlying': 0.09; 'api': 0.11; 'output': 0.11; 'wrote:': 0.15; '0.3': 0.16; 'enigma': 0.16; 'kern': 0.16; 'numpy': 0.16; 'pylab': 0.16; 'received:216.62': 0.16; 'received:216.62.213': 0.16; 'received:enthought.com': 0.16; 'tries': 0.16; 'pm,': 0.16; 'happening': 0.19; 'slightly': 0.19; 'header:In-Reply-To:1': 0.22; '0.1': 0.23; 'import': 0.29; 'interpret': 0.29; '"the': 0.29; 'arrays': 0.30; 'print': 0.32; 'to:addr:python-list': 0.34; 'header:X-Complaints-To:1': 0.34; 'header:User-Agent:1': 0.34; 'christopher': 0.35; 'using': 0.37; 'received:org': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'two': 0.38; 'mailing': 0.38; 'case': 0.39; 'should': 0.39; 'header:Mime- Version:1': 0.39; 'subject:from': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'our': 0.63; 'world': 0.65; 'believe': 0.66; 'skip:1 10': 0.66; 'here.': 0.66; 'ways,': 0.67; '11,': 0.68; 'exact': 0.69; '11.': 0.82; '0.8': 0.84; 'eco': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robert Kern Subject: Re: Strange output from arange() Date: Mon, 25 Jul 2011 14:45:00 -0500 Organization: The Church of Last Thursday References: <6fe5e5c8-de27-4d50-b797-c525968f50d1@h7g2000prf.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: outbound.enthought.com User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <6fe5e5c8-de27-4d50-b797-c525968f50d1@h7g2000prf.googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311623115 news.xs4all.nl 23949 [2001:888:2000:d::a6]:38978 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10304 On 7/25/11 2:20 PM, Christopher Barrington-Leigh wrote: > The following code: > > from pylab import arange > nSegments=5.0 > print arange(0,1.0+1.0/nSegments,1.0/nSegments) > nSegments=6.0 > print arange(0,1.0+1.0/nSegments,1.0/nSegments) > nSegments=8.0 > print arange(0,1.0+1.0/nSegments,1.0/nSegments) > nSegments=10.0 > print arange(0,1.0+1.0/nSegments,1.0/nSegments) > > gives an output of: > > [ 0. 0.2 0.4 0.6 0.8 1. ] > [ 0. 0.16666667 0.33333333 0.5 0.66666667 > 0.83333333 1. 1.16666667] > [ 0. 0.125 0.25 0.375 0.5 0.625 0.75 0.875 1. ] > [ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ] > > These arrays have lengths, 6, 8, 9, and 11, in stead of 6, 7, 9, and > 11. > What is going on for the case of n=6? Floating point computations are not always accurate, and when one tries to compute "the same thing" two different ways, one may get inconsistent results. This is what is happening with n=6. 1+1./6 happens to be slightly greater than 7*(1./6) while 1+1./5 happens to be slightly less than 6*(1./5), etc. The trick of using 1.0+1.0/nSegments/2 tends to work better. Nonetheless, if you want to get exactly nSegments segments with exact endpoints, you should use numpy.linspace(0.0, 1.0, nSegments+1). That's a much better API for what you want. Also, you will want to ask numpy questions on the numpy-discussion mailing list, not here. http://www.scipy.org/Mailing_Lists -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco