Smart way to iterate over the entire interval

I repeat the decimal point interval using the code in this solution: stack overflow

def seq(start, end, step):
    assert(step != 0)
    sample_count = abs(end - start) / step
    return itertools.islice(itertools.count(start, step), sample_count)

But I have a problem. My step size will be approximately 0.001, while my interval is 70.390829 to 70.855549. How can I assure that I am actually sorting as much as possible? Do I have to round to three decimal places to make sure I get as much area as possible? By this, I mean that I need to start as close to the beginning as possible and end as close to the end as possible. Will this help? Any other smart ideas?

+4
source share
2 answers

, , , float, decimal, , :

>>> from decimal import Decimal as D
>>> compare = lambda a, b: (a > b) - (a < b)
>>> def drange(start, stop, step):
        relation = compare(stop, start)
        if not relation:
            raise ValueError('start and stop may not have the same value')
        if compare(relation, 0) != compare(step, 0):
            raise ValueError('step will not allow the sequence to finish')
        while True:
            yield start
            start += step
            if compare(stop, start) != relation:
                break

>>> sequence = tuple(drange(D('70.390829'), D('70.855549'), D('0.001')))
>>> len(sequence)
465

, drange , :

>>> sequence = tuple(drange(70.390829, 70.855549, 0.001))
>>> len(sequence)
465
+3

numpy.linspace, , , , :

numpy.linspace(70.390829, 70.855549, 464)

464 0,001, linspace, , .

+3

Source: https://habr.com/ru/post/1599173/


All Articles