# [Numpy-discussion] linspace upper bound not met?

Bill Baxter wbaxter at gmail.com
Mon Aug 21 11:27:20 CDT 2006

```Out of curiosity I checked on what matlab does.
It does explicity set the last value to 'stop' to avoid the roundoff issue.
In numpy terms, it does something like
y = r_[start+r_[0:num-1]*(stop-start)/(num-1.0),  stop]

But for numpy it's probably more efficient to just do the 'y[-1] = stop'
like you say.

--bb

On 8/22/06, Alan G Isaac <aisaac at american.edu> wrote:
>
> The definition of linspace is:
> def linspace(start, stop, num=50, endpoint=True, retstep=False):
>     """Return evenly spaced numbers.
>
>     Return 'num' evenly spaced samples from 'start' to 'stop'.  If
>     'endpoint' is True, the last sample is 'stop'. If 'retstep' is
>     True then return the step value used.
>     """
>     num = int(num)
>     if num <= 0:
>         return array([], float)
>     if endpoint:
>         if num == 1:
>             return array([float(start)])
>         step = (stop-start)/float((num-1))
>     else:
>         step = (stop-start)/float(num)
>     y = _nx.arange(0, num) * step + start
>     if retstep:
>         return y, step
>     else:
>         return y
>
> The simplest way to achieve this goal is to add right after
> the assignment to y two new lines:
>    if endpoint:
>        y[-1] = float(stop)
>
> Cheers,
> Alan Isaac
>
> PS I'll take this opportunity to state again my opinion that
> in the denerate case num=1 that if endpoint=True then
> linspace should return stop rather than start.  (Otherwise
> endpoint is ignored. But I do not expect anyone to agree.)
>
>
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job
> easier