Home > Software engineering >  I want to generate a for loop using decimals. I need the y value from the loop to create a list. Thi
I want to generate a for loop using decimals. I need the y value from the loop to create a list. Thi

Time:11-08

float object cannot be interpreted as an integer

I want to generate a for loop using decimals. I need the y value from the loop to create a list. This is a project and I cant use numpy.

Are there any alternatives on approaching this issue?

value1, value2 and interval are all float.

for i in range ( value1 , value2 , interval):

CodePudding user response:

This is a naive impementation of a range function for floats:

def float_range(start, stop, step):
    if step > 0:
        while start < stop:
            yield start
            start  = step
    else:
        while start > stop:
            yield start
            start  = step

Notre that all the caveats of floating point math apply.

CodePudding user response:

import itertools

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