#๐ range() with float
35 messages ยท Page 1 of 1 (latest)
@prime lotus
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
You can set step to 0.1
Like range(0, 1, 0.1) for example,
this will increase by 0.1
etc
That could produce unpredictable results.
How?
>>> range(0, 1, 0.1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
oh right it's also a TypeError
!e use integer and divide
for i in [x/10 for x in range(10)]:
print(i)
@tawdry escarp :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0.0
002 | 0.1
003 | 0.2
004 | 0.3
005 | 0.4
006 | 0.5
007 | 0.6
008 | 0.7
009 | 0.8
010 | 0.9
@prime lotus it's probably best to just iterate over a range of integers then do the math yourself
I think that's fine unless it's a lot of iterations.
Yeah, it's just to give an idea
idk, maybe something like that already exists, but u could also make ur own generator:
>>> def iter_float(start: float, stop: float, step: float):
... i = start
... while i < stop:
... yield i
... i += step
...
>>> for i in iter_float(0, 5, 0.25):
... print(i)
...
0
0.25
0.5
0.75
1.0
...
@prime lotus The question is: what is it you're trying to do?
And I think numpy has a float range
Didn't know that. Oops. Thank you for that ๐.
yield is cool ๐
numpy.arrange could work
similar to range()
it accepts floats
I think
!e
import numpy as np
for i in np.arrange(0, 1, 0.1):
print(i)
!e
import numpy as np
for i in np.arange(0, 1, 0.1):
print(i)
@late fog :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0.0
002 | 0.1
003 | 0.2
004 | 0.30000000000000004
005 | 0.4
006 | 0.5
007 | 0.6000000000000001
008 | 0.7000000000000001
009 | 0.8
010 | 0.9
0.30000000000000004 ๐ฃ๏ธ
Yeah, float can not express 0.3 exactly.
>>> for i in iter_float(0, 1, 0.1):
... print(i)
...
0
0.1
0.2
0.30000000000000004
yeah ik
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.