mp.c, catacomb/__init__.py, pyke/: Fix mixed-mode arithmetic involving `float'.
[catacomb-python] / catacomb / __init__.py
index 94efb6f..4a98ced 100644 (file)
@@ -84,6 +84,9 @@ def _bin(s): return s
 def _iteritems(dict): return dict.iteritems()
 def _itervalues(dict): return dict.itervalues()
 
+## The built-in bignum type.
+_long = long
+
 ## How to fix a name back into the right identifier.  Alas, the rules are not
 ## consistent.
 def _fixname(name):
@@ -399,6 +402,10 @@ class BaseRat (object):
 
 class IntRat (BaseRat):
   RING = MP
+  def __new__(cls, a, b):
+    if isinstance(a, float) or isinstance(b, float): return a/b
+    return super(IntRat, cls).__new__(cls, a, b)
+  def __float__(me): return float(me._n)/float(me._d)
 
 class GFRat (BaseRat):
   RING = GF
@@ -412,8 +419,12 @@ class _tmp:
   def mont(x): return MPMont(x)
   def barrett(x): return MPBarrett(x)
   def reduce(x): return MPReduce(x)
-  def __truediv__(me, you): return IntRat(me, you)
-  def __rtruediv__(me, you): return IntRat(you, me)
+  def __truediv__(me, you):
+    if isinstance(you, float): return _long(me)/you
+    else: return IntRat(me, you)
+  def __rtruediv__(me, you):
+    if isinstance(you, float): return you/_long(me)
+    else: return IntRat(you, me)
   __div__ = __truediv__
   __rdiv__ = __rtruediv__
   _repr_pretty_ = _pp_str