[Python-Dev] New math functions

Christian Heimes lists at cheimes.de
Fri Feb 15 11:01:33 CET 2008


Raymond Hettinger wrote:
> Was some thought given to possibly adding these as
> float methods instead of as separate functions?
> 
>      float.isinf()
>      float.isnan()
> 
> Also, it would be useful to have a new method, float.is_integer().  This would be better than the current approach where we make the 
> test:  if x == floor(x).

No, I haven't thought about it. I could add isinf (or better is_inf?) to
the float type but the methods in the math and cmath module should stay
as well. They can also deal with complex and integer numbers.

+1 for is_integer

static PyObject *
float_is_integer(PyObject *v)
{
	double x = PyFloat_AsDouble(v);
	PyObject *o;
	
	if (x == -1.0 && PyErr_Occurred())
		return NULL;
	if (!Py_IS_FINITE(x))
		Py_RETURN_FALSE;
	PyFPE_START_PROTECT("is_integer", return 0)
	o = (floor(x) == x) ? Py_True : Py_False;
	PyFPE_END_PROTECT(x)
	if (errno != 0) {
		PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
						     PyExc_ValueError);
		return NULL;
	}
	Py_INCREF(o);
	return o;
}


Christian


More information about the Python-Dev mailing list