By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,696 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,696 IT Pros & Developers. It's quick & easy.

gmpy floating point exception

P: n/a
Hi,

I am experiencing some trouble with gmpy v1.01.
Multiplying an mpq with inf results in a floating point exception that
exits python. Has this already been fixed in newer gmpy versions?

~ $ python
Python 2.4.3 (#1, Mar 26 2007, 00:32:54)
[GCC 4.1.1 (Gentoo 4.1.1-r3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>from gmpy import mpq as Rational
a=Rational(2,3)
b=1.0e99999
b
inf
>>int(b)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OverflowError: cannot convert float infinity to long
>>a+b
Floating point exception
~ $
BTW.
1) What is the best way to test for inf regardless of type?
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?

Thanks in advance

Martin
Mar 29 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Martin Manns <mm****@gmx.netwrote:
Hi,

I am experiencing some trouble with gmpy v1.01.
Multiplying an mpq with inf results in a floating point exception that
exits python. Has this already been fixed in newer gmpy versions?
No, I can reproduce the problem (on a Mac with an Intel CPU) with the
current version, 1.02. I will have no time to work for a fix until (I
hope) next week, though (spending a long weekend hitch-hiking).
BTW.
1) What is the best way to test for inf regardless of type?
No, 'inf' is in fact not portable among different builds of Python for
different CPUs, in general.
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()

....?
Alex
Mar 29 '07 #2

P: n/a
On Thu, 29 Mar 2007 00:57:03 -0700
al***@mac.com (Alex Martelli) wrote:
Martin Manns <mm****@gmx.netwrote:
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
Mar 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.