diff options
author | Christian Heimes <christian@cheimes.de> | 2008-01-03 20:23:15 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2008-01-03 20:23:15 (GMT) |
commit | e2ca4245c90a4e41e45d943d4df2eacf7ef62962 (patch) | |
tree | f02bef3c72086738783aeaa3fff96d365255f2a7 /Modules/mathmodule.c | |
parent | ae04c3356ed2aec0e9e2c39096a3ccd05722575a (diff) | |
download | cpython-e2ca4245c90a4e41e45d943d4df2eacf7ef62962.zip cpython-e2ca4245c90a4e41e45d943d4df2eacf7ef62962.tar.gz cpython-e2ca4245c90a4e41e45d943d4df2eacf7ef62962.tar.bz2 |
Added math.isinf() and math.isnan()
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r-- | Modules/mathmodule.c | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 431590c..7540b5d 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -315,9 +315,8 @@ math_log10(PyObject *self, PyObject *arg) PyDoc_STRVAR(math_log10_doc, "log10(x) -> the base 10 logarithm of x."); -/* XXX(nnorwitz): Should we use the platform M_PI or something more accurate - like: 3.14159265358979323846264338327950288 */ -static const double degToRad = 3.141592653589793238462643383 / 180.0; +static const double degToRad = Py_MATH_PI / 180.0; +static const double radToDeg = 180.0 / Py_MATH_PI; static PyObject * math_degrees(PyObject *self, PyObject *arg) @@ -325,7 +324,7 @@ math_degrees(PyObject *self, PyObject *arg) double x = PyFloat_AsDouble(arg); if (x == -1.0 && PyErr_Occurred()) return NULL; - return PyFloat_FromDouble(x / degToRad); + return PyFloat_FromDouble(x * radToDeg); } PyDoc_STRVAR(math_degrees_doc, @@ -343,6 +342,33 @@ math_radians(PyObject *self, PyObject *arg) PyDoc_STRVAR(math_radians_doc, "radians(x) -> converts angle x from degrees to radians"); +static PyObject * +math_isnan(PyObject *self, PyObject *arg) +{ + double x = PyFloat_AsDouble(arg); + if (x == -1.0 && PyErr_Occurred()) + return NULL; + return PyBool_FromLong((long)Py_IS_NAN(x)); +} + +PyDoc_STRVAR(math_isnan_doc, +"isnan(x) -> bool\n\ +Checks if float x is not a number (NaN)"); + +static PyObject * +math_isinf(PyObject *self, PyObject *arg) +{ + double x = PyFloat_AsDouble(arg); + if (x == -1.0 && PyErr_Occurred()) + return NULL; + return PyBool_FromLong((long)Py_IS_INFINITY(x)); +} + +PyDoc_STRVAR(math_isinf_doc, +"isinf(x) -> bool\n\ +Checks if float x is infinite (positive or negative)"); + + static PyMethodDef math_methods[] = { {"acos", math_acos, METH_O, math_acos_doc}, {"asin", math_asin, METH_O, math_asin_doc}, @@ -358,6 +384,8 @@ static PyMethodDef math_methods[] = { {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, {"frexp", math_frexp, METH_O, math_frexp_doc}, {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, + {"isinf", math_isinf, METH_O, math_isinf_doc}, + {"isnan", math_isnan, METH_O, math_isnan_doc}, {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, {"log", math_log, METH_VARARGS, math_log_doc}, {"log10", math_log10, METH_O, math_log10_doc}, @@ -389,13 +417,13 @@ initmath(void) if (d == NULL) goto finally; - if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0))) + if (!(v = PyFloat_FromDouble(Py_MATH_PI))) goto finally; if (PyDict_SetItemString(d, "pi", v) < 0) goto finally; Py_DECREF(v); - if (!(v = PyFloat_FromDouble(exp(1.0)))) + if (!(v = PyFloat_FromDouble(Py_MATH_E))) goto finally; if (PyDict_SetItemString(d, "e", v) < 0) goto finally; |