Rappresenting infinite
avassalotti at gmail.com
avassalotti at gmail.com
Mon Jul 2 15:12:29 EDT 2007
On Jun 27, 6:41 am, andrea <kerny... at gmail.com> wrote:
> I would like to have a useful rappresentation of infinite, is there
> already something??
>
> I was thinking to something like this
>
> class Inf(int):
> """numero infinito"""
> def __init__(self,negative=False):
> self.negative = negative
> def __cmp__(self,y):
> """infinito maggiore di qualasiasi cosa"""
> if self.negative:
> return -1
> else:
> return 1
> def __repr__(self):
> """docstring for __repr__"""
> if self.negative:
> return '-inf'
> else:
> return 'inf'
> def __add__(self,y):
> """addizione con infinito"""
> return self
> def __mul__(self,y):
> """moltiplicazione"""
> import types
> if isinstance(y,types.IntType):
> if y > 0:
> return self
> if y == 0:
> return 'indefinito'
> else:
> return Inf(negative)
>
> I'd like to be able to compare between any value and infinite always
> getting the right result, and paying attention to the special cases
> like
> inf / inf => not determinate
float('inf') works well, no?
>>> inf = float('inf')
>>> inf / inf
nan
>>> -inf
-inf
>>> inf / 0
ZeroDivisionError: float division
>>> 1 / inf
0.0
>>> 0 * float('inf')
nan
More information about the Python-list
mailing list