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


Groups > comp.lang.python > #10300 > unrolled thread

Strange output from arange()

Started byChristopher Barrington-Leigh <christopherbl@gmail.com>
First post2011-07-25 12:20 -0700
Last post2011-07-25 14:45 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Strange output from arange() Christopher Barrington-Leigh <christopherbl@gmail.com> - 2011-07-25 12:20 -0700
    Re: Strange output from arange() Wanderer <wanderer@dialup4less.com> - 2011-07-25 12:42 -0700
    Re: Strange output from arange() Robert Kern <robert.kern@gmail.com> - 2011-07-25 14:45 -0500

#10300 — Strange output from arange()

FromChristopher Barrington-Leigh <christopherbl@gmail.com>
Date2011-07-25 12:20 -0700
SubjectStrange output from arange()
Message-ID<6fe5e5c8-de27-4d50-b797-c525968f50d1@h7g2000prf.googlegroups.com>
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?

[toc] | [next] | [standalone]


#10303

FromWanderer <wanderer@dialup4less.com>
Date2011-07-25 12:42 -0700
Message-ID<a2d54912-03ca-488d-95bb-bfe477ca4295@r18g2000vbs.googlegroups.com>
In reply to#10300
On Jul 25, 3:20 pm, Christopher Barrington-Leigh
<christophe...@gmail.com> 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?

It's rounding.

See http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html

      stop : number
      End of interval. The interval does not include this value,
except in some cases where step is not an integer and floating point
round-off affects the length of out.

The stops are

5 -- 1.2
6 -- 1.1666666666666666666666667
8 -- 1.125
10 -- 1.1

Only 6 has to be rounded up.

[toc] | [prev] | [next] | [standalone]


#10304

FromRobert Kern <robert.kern@gmail.com>
Date2011-07-25 14:45 -0500
Message-ID<mailman.1471.1311623115.1164.python-list@python.org>
In reply to#10300
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web