FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-132908: Add math.isnormal/issubnormal() functions by skirpichev · Pull Request #132935 · python/cpython · GitHub

Repository navigation

20 changes: 20 additions & 0 deletions Doc/library/math.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ noted otherwise, all return values are floats.
:func:`frexp(x) <frexp>` Mantissa and exponent of *x*
:func:`isclose(a, b, rel_tol, abs_tol) <isclose>` Check if the values *a* and *b* are close to each other
:func:`isfinite(x) <isfinite>` Check if *x* is neither an infinity nor a NaN
:func:`isnormal(x) <isnormal>` Check if *x* is a normal number
:func:`issubnormal(x) <issubnormal>` Check if *x* is a subnormal number
:func:`isinf(x) <isinf>` Check if *x* is a positive or negative infinity
:func:`isnan(x) <isnan>` Check if *x* is a NaN (not a number)
:func:`ldexp(x, i) <ldexp>` ``x * (2**i)``, inverse of function :func:`frexp`
Expand Down Expand Up @@ -373,6 +375,24 @@ Floating point manipulation functions
.. versionadded:: 3.2


.. function:: isnormal(x)

Return ``True`` if *x* is a normal number, that is a finite
nonzero number that is not a subnormal (see :func:`issubnormal`).
Return ``False`` otherwise.

.. versionadded:: next


.. function:: issubnormal(x)

Return ``True`` if *x* is a subnormal number, that is a finite
nonzero number with a magnitude smaller than the smallest positive normal
number, see :data:`sys.float_info.min`. Return ``False`` otherwise.
Comment thread
skirpichev marked this conversation as resolved.

.. versionadded:: next


.. function:: isinf(x)

Return ``True`` if *x* is a positive or negative infinity, and
Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.14.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ math
----

* Added more detailed error messages for domain errors in the module.
(Contributed by by Charlie Zhao and Sergey B Kirpichev in :gh:`101410`.)
(Contributed by Charlie Zhao and Sergey B Kirpichev in :gh:`101410`.)


mimetypes
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.15.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ difflib
(Contributed by Jiahao Li in :gh:`134580`.)


math
----

* Add :func:`math.isnormal` and :func:`math.issubnormal` functions.
(Contributed by Sergey B Kirpichev in :gh:`132908`.)


shelve
------

Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_math.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,28 @@ def testIsfinite(self):
self.assertFalse(math.isfinite(float("inf")))
self.assertFalse(math.isfinite(float("-inf")))

def testIsnormal(self):
self.assertTrue(math.isnormal(1.25))
self.assertTrue(math.isnormal(-1.0))
self.assertFalse(math.isnormal(0.0))
self.assertFalse(math.isnormal(-0.0))
self.assertFalse(math.isnormal(INF))
self.assertFalse(math.isnormal(NINF))
self.assertFalse(math.isnormal(NAN))
self.assertFalse(math.isnormal(FLOAT_MIN/2))
self.assertFalse(math.isnormal(-FLOAT_MIN/2))

def testIssubnormal(self):
self.assertFalse(math.issubnormal(1.25))
self.assertFalse(math.issubnormal(-1.0))
self.assertFalse(math.issubnormal(0.0))
self.assertFalse(math.issubnormal(-0.0))
self.assertFalse(math.issubnormal(INF))
self.assertFalse(math.issubnormal(NINF))
self.assertFalse(math.issubnormal(NAN))
self.assertTrue(math.issubnormal(FLOAT_MIN/2))
self.assertTrue(math.issubnormal(-FLOAT_MIN/2))

def testIsnan(self):
self.assertTrue(math.isnan(float("nan")))
self.assertTrue(math.isnan(float("-nan")))
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`math.isnormal` and :func:`math.issubnormal` functions. Patch by
Sergey B Kirpichev.
70 changes: 69 additions & 1 deletion Modules/clinic/mathmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions Modules/mathmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3118,6 +3118,44 @@ math_isfinite_impl(PyObject *module, double x)
}


/*[clinic input]
Comment thread
skirpichev marked this conversation as resolved.
math.isnormal

x: double
/

Return True if x is normal, and False otherwise.
[clinic start generated code]*/

static PyObject *
math_isnormal_impl(PyObject *module, double x)
/*[clinic end generated code: output=c7b302b5b89c3541 input=fdaa00c58aa7bc17]*/
{
return PyBool_FromLong(isnormal(x));
}


/*[clinic input]
math.issubnormal

x: double
/

Return True if x is subnormal, and False otherwise.
[clinic start generated code]*/

static PyObject *
math_issubnormal_impl(PyObject *module, double x)
/*[clinic end generated code: output=4e76ac98ddcae761 input=9a20aba7107d0d95]*/
{
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
return PyBool_FromLong(issubnormal(x));
#else
return PyBool_FromLong(isfinite(x) && x && !isnormal(x));
#endif
}


/*[clinic input]
math.isnan

Expand Down Expand Up @@ -4145,6 +4183,8 @@ static PyMethodDef math_methods[] = {
MATH_HYPOT_METHODDEF
MATH_ISCLOSE_METHODDEF
MATH_ISFINITE_METHODDEF
MATH_ISNORMAL_METHODDEF
MATH_ISSUBNORMAL_METHODDEF
MATH_ISINF_METHODDEF
MATH_ISNAN_METHODDEF
MATH_ISQRT_METHODDEF
Expand Down

Back | FazBrowse Home | New Git URL