[Numpy-discussion] step paramter for linspace

Sebastian Berg sebastian@sipsolutions....
Fri Mar 1 06:25:20 CST 2013


Hi,

there has been a request on the issue tracker for a step parameter to
linspace. This is of course tricky with the imprecision of floating
point numbers.
As a trade off, I was thinking of a step parameter that is used to
calculate the integer number of steps. However to be certain that it
never misbehaves, doing this strict up to the numerical precision of the
(float) numbers. Effectively this means:

In [9]: np.linspace(0, 1.2, step=0.3)
Out[9]: array([ 0. ,  0.3,  0.6,  0.9,  1.2])

In [10]: np.linspace(0, 1.2+5-5, step=0.3)
Out[10]: array([ 0. ,  0.3,  0.6,  0.9,  1.2])

In [11]: np.linspace(0, 1.2+500-500, step=0.3)
ValueError: could not determine exact number of samples for given step

I.e. the last fails, because 1.2 + 500 - 500 == 1.1999999999999886,
which is an error that is larger then the imprecision of floating point
numbers.

Is this considered useful, or as it can easily fail for calculated
numbers, and is thus only a convenience, it is not?

Regards,

Sebastian




More information about the NumPy-Discussion mailing list