diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2021-09-02 17:01:46 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-02 17:01:46 -0600 |
commit | 630501438a1ce0ac13ad7342dc8614e3f0440392 (patch) | |
tree | 1fe1d6519331362df417b454a06a530f80600d04 /numpy/core/src | |
parent | 95d254027f2fb526fe4c745f330e6fa448a7e4db (diff) | |
parent | bb3e4077a32078deb31d7c31034303303fc268f4 (diff) | |
download | numpy-630501438a1ce0ac13ad7342dc8614e3f0440392.tar.gz |
Merge pull request #19803 from BvB93/is_integer
ENH: Add `is_integer` to `np.floating` & `np.integer`
Diffstat (limited to 'numpy/core/src')
-rw-r--r-- | numpy/core/src/multiarray/scalartypes.c.src | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src index 40f736125..740ec8cc2 100644 --- a/numpy/core/src/multiarray/scalartypes.c.src +++ b/numpy/core/src/multiarray/scalartypes.c.src @@ -1908,6 +1908,39 @@ error: } /**end repeat**/ +/**begin repeat + * #name = half, float, double, longdouble# + * #Name = Half, Float, Double, LongDouble# + * #is_half = 1,0,0,0# + * #c = f, f, , l# + */ +static PyObject * +@name@_is_integer(PyObject *self) +{ +#if @is_half@ + npy_double val = npy_half_to_double(PyArrayScalar_VAL(self, @Name@)); +#else + npy_@name@ val = PyArrayScalar_VAL(self, @Name@); +#endif + PyObject *ret; + + if (npy_isnan(val)) { + Py_RETURN_FALSE; + } + if (!npy_isfinite(val)) { + Py_RETURN_FALSE; + } + + ret = (npy_floor@c@(val) == val) ? Py_True : Py_False; + Py_INCREF(ret); + return ret; +} +/**end repeat**/ + +static PyObject * +integer_is_integer(PyObject *self) { + Py_RETURN_TRUE; +} /* * need to fill in doc-strings for these methods on import -- copy from @@ -2167,7 +2200,7 @@ static PyMethodDef @name@type_methods[] = { /**end repeat**/ /**begin repeat - * #name = integer,floating, complexfloating# + * #name = floating, complexfloating# */ static PyMethodDef @name@type_methods[] = { /* Hook for the round() builtin */ @@ -2178,6 +2211,17 @@ static PyMethodDef @name@type_methods[] = { }; /**end repeat**/ +static PyMethodDef integertype_methods[] = { + /* Hook for the round() builtin */ + {"__round__", + (PyCFunction)integertype_dunder_round, + METH_VARARGS | METH_KEYWORDS, NULL}, + {"is_integer", + (PyCFunction)integer_is_integer, + METH_NOARGS, NULL}, + {NULL, NULL, 0, NULL} /* sentinel */ +}; + /**begin repeat * #name = half,float,double,longdouble# */ @@ -2185,6 +2229,9 @@ static PyMethodDef @name@type_methods[] = { {"as_integer_ratio", (PyCFunction)@name@_as_integer_ratio, METH_NOARGS, NULL}, + {"is_integer", + (PyCFunction)@name@_is_integer, + METH_NOARGS, NULL}, {NULL, NULL, 0, NULL} }; /**end repeat**/ |