[Python-Dev] PEP 754

Gustavo Niemeyer niemeyer at conectiva.com
Wed Apr 28 16:43:24 EDT 2004


> That isn't a good objection for this specific patch -- it's pure
> Python, and picks apart bits using the struct module.  IIRC, it wasn't
> correctly written for most 64-bit boxes, but that looked like a
> shallow flaw (I think it assumed that a struct "l" format code, in
> native mode, always yields a 32-bit thingie).

I was just wondering, now that C99 supports part of IEEE754, should
we really get into the job of implementing our own mechanism?

As an example, the attached patch exports part of the C99 interface to
Python.

If we agree that this is something interesting to have in Python, I'll
be willing to prepare a patch including support for this interface and
hacking some bits to introduce a better IEEE754 support (e.g. checking
nan comparisons, etc).

-- 
Gustavo Niemeyer
http://niemeyer.net
-------------- next part --------------

#include <Python.h>
#include <math.h>

#define CHECK_FLOAT(o) \
	if (!PyFloat_Check(o)) { \
		PyErr_SetString(PyExc_TypeError, "float argument required"); \
		return NULL; \
	}

static PyObject *
ieee754_isnan(PyObject *self, PyObject *o)
{
	CHECK_FLOAT(o);
	return PyBool_FromLong(isnan(PyFloat_AsDouble(o)));
}

static PyObject *
ieee754_isfinite(PyObject *self, PyObject *o)
{
	CHECK_FLOAT(o);
	return PyBool_FromLong(isfinite(PyFloat_AsDouble(o)));
}

static PyMethodDef ieee754_methods[] = {
	{"isnan", ieee754_isnan, METH_O, ""},
	{"isfinite", ieee754_isfinite, METH_O, ""},
	{NULL, NULL}
};

DL_EXPORT(void)
initieee754(void)
{
	PyObject *m = Py_InitModule3("ieee754", ieee754_methods, "");
	PyModule_AddObject(m, "nan", PyFloat_FromDouble(NAN));
	PyModule_AddObject(m, "inf", PyFloat_FromDouble(INFINITY));
}


More information about the Python-Dev mailing list