[Numpy-discussion] Re: numpy, overflow, inf, ieee, and rich , comparison

jay.krell at cornell.edu jay.krell at cornell.edu
Thu Oct 26 16:27:52 EDT 2000


Crap my mistake. The Python source is confusing. Long != PyLong. Long == C
long. Still, I guess all those extra checks in int_divmod that lead to all
the add and subtract..

 - Jay

-----Original Message-----
From: jay.krell at cornell.edu <jay.krell at cornell.edu>
To: Fredrik Lundh <effbot at telia.com>
Cc: python-list at python.org <python-list at python.org>
Date: Thursday, October 26, 2000 1:25 PM
Subject: Re: [Numpy-discussion] Re: numpy, overflow, inf, ieee, and rich ,
comparison


>1.5.2 source:
>
>Are those trailing Py.._From... doing heap allocations? I believe so. Yuck.
>That probably dominates everything, in general, in Python. Besides,
"PyLong"
>is more expensive than "PyDouble", right? Well, it looks like one level of
>its heap allocation is made very fast, just pluck the head of the free
list.
>Why does integer division return a long? I can understand integer
>multiplication..
>
>static PyObject *
>float_div(v, w)
> PyFloatObject *v;
> PyFloatObject *w;
>{
> double result;
> if (w->ob_fval == 0) {
>  PyErr_SetString(PyExc_ZeroDivisionError, "float division");
>  return NULL;
> }
> PyFPE_START_PROTECT("divide", return 0)
> result = v->ob_fval / w->ob_fval;
> PyFPE_END_PROTECT(result)
> return PyFloat_FromDouble(result);
>}
>
>static PyObject *
>int_div(x, y)
> PyIntObject *x;
> PyIntObject *y;
>{
> long d, m;
> if (i_divmod(x, y, &d, &m) < 0)
>  return NULL;
> return PyInt_FromLong(d);
>}
>
>static int
>i_divmod(x, y, p_xdivy, p_xmody)
> register PyIntObject *x, *y;
> long *p_xdivy, *p_xmody;
>{
> long xi = x->ob_ival;
> long yi = y->ob_ival;
> long xdivy, xmody;
>
> if (yi == 0) {
>  PyErr_SetString(PyExc_ZeroDivisionError,
>    "integer division or modulo");
>  return -1;
> }
> if (yi < 0) {
>  if (xi < 0)
>   xdivy = -xi / -yi;
>  else
>   xdivy = - (xi / -yi);
> }
> else {
>  if (xi < 0)
>   xdivy = - (-xi / yi);
>  else
>   xdivy = xi / yi;
> }
> xmody = xi - xdivy*yi;
> if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
>  xmody += yi;
>  xdivy -= 1;
> }
> *p_xdivy = xdivy;
> *p_xmody = xmody;
> return 0;
>}
>
>
>-----Original Message-----
>From: Fredrik Lundh <effbot at telia.com>
>To: jay.krell at cornell.edu <jay.krell at cornell.edu>
>Cc: python-list at python.org <python-list at python.org>
>Date: Thursday, October 26, 2000 12:25 PM
>Subject: Re: [Numpy-discussion] Re: numpy, overflow, inf, ieee, and rich ,
>comparison
>
>
>>jay wrote:
>>> But is integer division actually much faster than floating point
>division? I
>>> don't believe there is a fast algorithm for integer division.
>>
>>On my PII, floating point division under Python is *faster* than
>>integer division (hint: look at the float_div and int_div functions,
>>and you'll understand why...)
>>
>>(btw, my "wow" meant "wow, I've seen many strange arguments
>>in this thread, but not so many in one single paragraph...")
>>
>></F>
>>
>>
>>--
>>http://www.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list