Skip to content

numpy/arange-float-step

Rule arange-float-step
Pack NumPy
Severity warning
Fix none, edit by hand
Library versions every version

With a float step such as 0.1, the length of np.arange depends on rounding, and the stop value can appear in the result. Use np.linspace with the number of values and endpoint=False.

ticks = np.arange(0, 1, 0.1)
ticks = np.linspace(0, 0.9, 10)

The rule reports np.arange(0, 1, 0.1), whose length depends on rounding. For example, np.arange(1, 1.3, 0.1) ends with 1.3000000000000003. The NumPy docs for arange recommend np.linspace for a step that is not whole.

The rule leaves a step alone that is a multiple of 1/64, such as 0.5 or 0.25, because such a step is exact.

xpd has no automatic fix, so make the change by hand.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

With a float step such as 0.1, the length of $NP.arange depends on rounding, and the stop value can appear in the result. Write $NP.linspace(start, stop, num, endpoint=False) with the number of values, which gives the same grid and never the stop value.

Browse the other rules of the NumPy pack.