diff -r 4a55b98314cd Doc/library/stdtypes.rst --- a/Doc/library/stdtypes.rst Mon Jan 12 21:03:41 2015 +0100 +++ b/Doc/library/stdtypes.rst Mon Jan 12 15:16:34 2015 -0800 @@ -1332,6 +1332,30 @@ :attr:`~range.stop` and :attr:`~range.step` attributes, for example ``range(0) == range(2, 1, 3)`` or ``range(0, 3, 2) == range(0, 4, 2)``.) +Range objects are often inappropriate for non-integral types, +especially inexact types such as :class:`float`. In most cases, for +such types, it is better to specify a count of numbers or intervals +instead of a step size. Care must be taken not to accumulate rounding +errors, to avoid overflow and underflow, and to avoid using operations +that unnecessarily restrict the types (e.g., if you only multiply and +divide differences, the algorithm will work with +:class:`datetime.datetime`; if you multiply and divide values +themselves, it will not). A simple version (a closed range, a count +of numbers rather than intervals, potentially underflowing denormal +values, and potentially being off by 1 digit on the :data:`stop` +value) is: + + >>> start, stop, num = 0, 10, 11 + >>> step = (stop-start)/(num-1) + >>> [start + i*step for i in range(num)] + [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] + +.. seealso:: + + * `linspace recipe `_ + for an example lazy sequence built on this algorithm, and links + to alternatives. + .. versionchanged:: 3.2 Implement the Sequence ABC. Support slicing and negative indices.