Thanks. I’ve been trying to express the “infinity” concept.
If you’re after the concept of infinite, a for-loop isn’t going to be
much help. When we want a loop while runs forver we generally use a
while True:
loop. Regardless of the flavour of loop, anything which
tries to “count to infinity” will never complete, and any intermediate
value in that counter eg in the unbounded sequence 1,2,3,4,… will not
be infinity.
Usually infinity (and things like the various types of infinite sets)
are treated symbolicly in maths, and thus also in programmes which deal
with in.
You might look at the sympy module, which facilitates doing symbolic
math in Python:
https://scipy-lectures.org/packages/sympy.html
https://docs.sympy.org/latest/install.html
It does come with an infinity symbol which is an object which compares
to be greater than other objects, etc.
Since the float("+-inf"
) type is a tuple() and an int()
.
This isn’t the case. It is a Python float
, which a special IEEE754
floating point value used to indicate value in floating point
arithmetic.
Maybe you can provide some more detail about what you’re trying to do:
- reason mathematically about infinity, which tends to be a symbolic
operation
- make loops which run forever, typically via
while True:
- make a sequence which is unbounded such as an unbounded iterator
For this last, you might imagine writing a loop like this:
for n in cardinals():
print(n)
which prints values for n
starting at 1
and not stopping.
In this situation cardinals()
returns an iterator which yields these
values as required. A Python for-loop does count, it just iterates over
whatever argument you give it. So:
for i in [1,2,3]:
iterates over the elements in that 3 element list.
Here’s an unbounded cardinals()
generator function:
def cardinals():
i = 1
while True:
yield i
i += 1
This function, when you call it (as in the for-loop example above)
returns a generator which yields values when the for-loop iterator on
it, getting 1
then 2
etc. The generator is running the function
above, stalling when it yield
s a value. next time the for-loop
“iterates” by asking for the next value, the function resumes running
until it executes its next yield
statement.
Cheers,
Cameron Simpson cs@cskk.id.au