inf class (was: gmpy floating point exception)

Martin Manns mmanns at gmx.de
Thu Mar 29 11:11:47 EDT 2007


On Thu, 29 Mar 2007 00:57:03 -0700
aleax at mac.com (Alex Martelli) wrote:

> Martin Manns <mmanns at gmx.net> wrote:
> > 2) Is there any inf type around with
> > a + inf == inf
> > inf > a (as long as a != inf)
> > etc.
> > that works with any other type?
> 
> You mean something like:
> 
> class inf(object):
>     def __radd__(self, other): return self
>     def __gt__(self, other): return self is not other
>     # etc
> inf = inf()

Actually, I meant something a bit more sophisticated:
a=inf()
b=inf()
c=-inf()
d=1e99999
e=numpy.float64(1e99999)
f=gmpy.mpq(1,2)

a > b		OverflowError (AmbiguousValueError?)
a == b		OverflowError (AmbiguousValueError?)
a + b		inf (New inf instance)
a + c		OverflowError (AmbiguousValueError?)
a + d		a
max(a,b)	inf (New inf instance)
max(a,c)	a
max(a,d)	a
max(a,b) == a	OverflowError (AmbiguousValueError?)
max(a,b) == b	OverflowError (AmbiguousValueError?)
max(a,b) > a	OverflowError (AmbiguousValueError?)
max(a,b) >= a	True (if somehow possible)
max(a,b) < a	False (if somehow possible)
a is b		False
a > c		True
a == -a		False
a == -c		OverflowError (AmbiguousValueError?)
a > d		True
a > e		True
c > f		False

The idea is a class that permits non-ambiguous comparisons but throws
exceptions if ambiguous comparisons are made. Since I think that
setting up such a class involves quite some effort (especially
considering things such as a + a) I just wanted to know if something
similar is already around. I am aware of the PEP 326 but I feel that it
does not really address the problem.

Martin



More information about the Python-list mailing list