Write a FloatRange iterable (start, stop, step) that works with decimal steps, can be looped over more than once, and avoids accumulating floating-point error.
class FloatRange:
def __init__(self, start, stop, step):
self.start, self.stop, self.step = start, stop, step
def __iter__(self): # a fresh generator each time: reusable
i = 0
while (value := self.start + i * self.step) < self.stop:
yield round(value, 10) # multiply instead of adding repeatedly
i += 1
r = FloatRange(0, 1, 0.25)
print(list(r))
print(sum(r), len(list(FloatRange(1, 2, 0.1))))
[0.0, 0.25, 0.5, 0.75] 1.5 10
Computing start + i × step for each value (rather than adding step again and again) stops rounding errors from building up; because __iter__ is a generator method, the object can be iterated repeatedly.