]> granicus.if.org Git - python/commitdiff
Change long/long true division to return as many good bits as it can;
authorTim Peters <tim.peters@gmail.com>
Tue, 4 Sep 2001 06:17:36 +0000 (06:17 +0000)
committerTim Peters <tim.peters@gmail.com>
Tue, 4 Sep 2001 06:17:36 +0000 (06:17 +0000)
e.g., (1L << 40000)/(1L << 40001) returns 0.5, not Inf or NaN or whatever.

Lib/test/test_long_future.py [new file with mode: 0644]
Objects/intobject.c
Objects/longobject.c

diff --git a/Lib/test/test_long_future.py b/Lib/test/test_long_future.py
new file mode 100644 (file)
index 0000000..4348bbe
--- /dev/null
@@ -0,0 +1,38 @@
+from __future__ import division
+# When true division is the default, get rid of this and add it to
+# test_long.py instead.  In the meantime, it's too obscure to try to
+# trick just part of test_long into using future division.
+
+from test_support import TestFailed, verify, verbose
+
+def test_true_division():
+    if verbose:
+        print "long true division"
+    huge = 1L << 40000
+    mhuge = -huge
+    verify(huge / huge == 1.0)
+    verify(mhuge / mhuge == 1.0)
+    verify(huge / mhuge == -1.0)
+    verify(mhuge / huge == -1.0)
+    verify(1 / huge == 0.0)
+    verify(1L / huge == 0.0)
+    verify(1 / mhuge == 0.0)
+    verify(1L / mhuge ==- 0.0)
+    verify((666 * huge + (huge >> 1)) / huge == 666.5)
+    verify((666 * mhuge + (mhuge >> 1)) / mhuge == 666.5)
+    verify((666 * huge + (huge >> 1)) / mhuge == -666.5)
+    verify((666 * mhuge + (mhuge >> 1)) / huge == -666.5)
+    verify(huge / (huge << 1) == 0.5)
+
+    namespace = {'huge': huge, 'mhuge': mhuge}
+    for overflow in ["float(huge)", "float(mhuge)",
+                     "huge / 1", "huge / 2L", "huge / -1", "huge / -2L",
+                     "mhuge / 100", "mhuge / 100L"]:
+        try:
+            eval(overflow, namespace)
+        except OverflowError:
+            pass
+        else:
+            raise TestFailed("expected OverflowError from %r" % overflow)
+
+test_true_division()
index 775213c2d3a6e68ae038e0eeed9fb2d041790c95..73d5e77e359312a411ec56f024a4abb5554b731e 100644 (file)
@@ -535,7 +535,14 @@ int_classic_div(PyIntObject *x, PyIntObject *y)
 static PyObject *
 int_true_divide(PyObject *v, PyObject *w)
 {
-       return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
+       /* If they aren't both ints, give someone else a chance.  In
+          particular, this lets int/long get handled by longs, which
+          underflows to 0 gracefully if the long is too big to convert
+          to float. */
+       if (PyInt_Check(v) && PyInt_Check(w))
+               return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
+       Py_INCREF(Py_NotImplemented);
+       return Py_NotImplemented;
 }
 
 static PyObject *
index 7aa60054b06565e7a553e5478b5083246a587a09..5da5113d2ef698ed9b9d8f08fcfff87ecb7e1573 100644 (file)
@@ -1584,7 +1584,38 @@ long_classic_div(PyObject *v, PyObject *w)
 static PyObject *
 long_true_divide(PyObject *v, PyObject *w)
 {
-       return PyFloat_Type.tp_as_number->nb_divide(v, w);
+       PyLongObject *a, *b;
+       double ad, bd;
+       int aexp, bexp;
+
+       CONVERT_BINOP(v, w, &a, &b);
+       ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
+       bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
+       if ((ad == -1.0 || bd == -1.0) && PyErr_Occurred())
+               return NULL;
+
+       if (bd == 0.0) {
+               PyErr_SetString(PyExc_ZeroDivisionError,
+                       "long division or modulo by zero");
+               return NULL;
+       }
+
+       /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
+       ad /= bd;       /* overflow/underflow impossible here */
+       aexp -= bexp;
+       if (aexp > INT_MAX / SHIFT)
+               goto overflow;
+       errno = 0;
+       ad = ldexp(ad, aexp * SHIFT);
+       if (ad != 0 && errno == ERANGE) /* ignore underflow to 0.0 */
+               goto overflow;
+       return PyFloat_FromDouble(ad);
+
+overflow:
+       PyErr_SetString(PyExc_OverflowError,
+               "long/long too large for a float");
+       return NULL;
+       
 }
 
 static PyObject *