summaryrefslogtreecommitdiffstats
path: root/Objects/longobject.c
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-09-04 06:17:36 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-09-04 06:17:36 (GMT)
commite2a600099d3b61327aba5be94d30d40773faa2c9 (patch)
tree7f9ed79ab6f997c2afbeae21bb097d045c344e66 /Objects/longobject.c
parent9c1d7fd5f2173dc72ecdb1df7816c7e432437e02 (diff)
downloadcpython-e2a600099d3b61327aba5be94d30d40773faa2c9.zip
cpython-e2a600099d3b61327aba5be94d30d40773faa2c9.tar.gz
cpython-e2a600099d3b61327aba5be94d30d40773faa2c9.tar.bz2
Change long/long true division to return as many good bits as it can;
e.g., (1L << 40000)/(1L << 40001) returns 0.5, not Inf or NaN or whatever.
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r--Objects/longobject.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 7aa6005..5da5113 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -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 *