2

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):
Roxy
  • 1,015
  • 7
  • 20
  • 1
    What efforts have you put into the project? Project solutions aren't to be provided herein SO. You could instead try ´range(len(value))´ – Roxy Nov 08 '21 at 11:28

2 Answers2

0

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.

user2390182
  • 72,016
  • 6
  • 67
  • 89
  • could it be possible to use `round` to limit the floating point math inaccuracy? so for example round each result to only two (or user given) digits after the decimal point and use that rounded value for the next calculation and so on, since the floating point inaccuracy is so small this should to an extent reduce it – Matiiss Nov 08 '21 at 11:47
  • If you want that, you should probably use `deimal.Decimal` instead of floats – user2390182 Nov 08 '21 at 11:56
  • 1
    Computing a running partial sum throughout the loop will accumulate rounding errors, causing the sum to deviate increasingly (on average) from the ideal sum as the loop progresses. It may also cause the termination condition not to trigger at the desired point. The idiomatic way to iterate a floating-point counter is to maintain an integer counter and compute a floating-point value from it in each iteration. – Eric Postpischil Nov 08 '21 at 14:07
0
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)
minattosama
  • 263
  • 1
  • 9
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '21 at 11:34
  • `sample_count = int(abs(end - start) / step)` will not always produce the desired sample count, due to rounding in floating-point arithmetic. – Eric Postpischil Nov 08 '21 at 14:05
  • 1
    From [itertools.count](https://docs.python.org/3.10/library/itertools.html#itertools.count): _"When counting with floating point numbers, better accuracy can sometimes be achieved by substituting multiplicative code such as: `(start + step * i for i in count())`"_ – Timus Nov 08 '21 at 14:34
  • Don't hesitate to modify my code. Contribute please – minattosama Nov 09 '21 at 17:01