On Sep 24, 6:53 am, Steven D'Aprano <steve +comp.lang.pyt...@pearwood.info> wrote: > I'm trying to generate a sequence of equally-spaced numbers between a lower > and upper limit. Given arbitrary limits, what is the best way to generate a > list of equally spaced floats with the least rounding error for each point? > > For example, suppose I want to divide the range 0 to 2.1 into 7 equal > intervals, then the end-points of each interval are: > > (0.0)---(0.3)---(0.6)---(0.9)---(1.2)---(1.5)---(1.8)---(2.1) > > and I'd like to return the values in the brackets. Using Decimal or Fraction > is not an option, I must use floats. If the exact value isn't representable > as a float, I'm okay with returning the nearest possible float.
Use numpy. >>> from numpy import mgrid >>> seq = mgrid[0.0:2.1:8j] >>> seq array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1]) >>> for x in seq: print repr(x) 0.0 0.29999999999999999 0.59999999999999998 0.89999999999999991 1.2 1.5 1.7999999999999998 2.1000000000000001 -- http://mail.python.org/mailman/listinfo/python-list