From a8006a7706d15710ad879bb59cf3e4a4294b5778 Mon Sep 17 00:00:00 2001 From: CharlieZhao Date: Mon, 28 Mar 2022 21:00:08 +0800 Subject: [PATCH 1/4] Improve documentation for `math.nan` --- Doc/library/math.rst | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index bcbcdef51d3fcb..3254fda1200b9e 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -646,8 +646,27 @@ Constants .. data:: nan - A floating-point "not a number" (NaN) value. Equivalent to the output of - ``float('nan')``. + A floating-point "not a number" (NaN) value. Equivalent to the output of + ``float('nan')``. Specifically, due to the requirements of the `IEEE-754 standard + `_, ``math.nan`` and ``float('nan')`` are + not considered to equal to any other numeric value, including themselves. To avoid errors + when checking whether a number is a NaN, use the :func:`isnan` function to test + for NaNs instead of ``is`` or ``==``. + Example:: + + >>> import math + >>> math.nan == math.nan + False + >>> float('nan') == float('nan') + False + >>> math.nan is math.nan + True + >>> float('nan') is float('nan') + False + >>> math.isnan(math.nan) + True + >>> math.isnan(float('nan')) + True .. versionchanged:: 3.11 It is now always available. From 98275bd104c25856aec56c679f50202d387cf708 Mon Sep 17 00:00:00 2001 From: Charlie Zhao Date: Tue, 29 Mar 2022 11:30:55 +0800 Subject: [PATCH 2/4] Update Doc/library/math.rst Co-authored-by: Jelle Zijlstra --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 3254fda1200b9e..fbaa915829703b 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -647,7 +647,7 @@ Constants .. data:: nan A floating-point "not a number" (NaN) value. Equivalent to the output of - ``float('nan')``. Specifically, due to the requirements of the `IEEE-754 standard + ``float('nan')``. Due to the requirements of the `IEEE-754 standard `_, ``math.nan`` and ``float('nan')`` are not considered to equal to any other numeric value, including themselves. To avoid errors when checking whether a number is a NaN, use the :func:`isnan` function to test From cd3af79ee9b818b0de3e23563bfaf96e650f05b0 Mon Sep 17 00:00:00 2001 From: Charlie Zhao Date: Tue, 29 Mar 2022 11:31:14 +0800 Subject: [PATCH 3/4] Update Doc/library/math.rst Co-authored-by: Jelle Zijlstra --- Doc/library/math.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index fbaa915829703b..afb2f4ec0ba410 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -649,8 +649,8 @@ Constants A floating-point "not a number" (NaN) value. Equivalent to the output of ``float('nan')``. Due to the requirements of the `IEEE-754 standard `_, ``math.nan`` and ``float('nan')`` are - not considered to equal to any other numeric value, including themselves. To avoid errors - when checking whether a number is a NaN, use the :func:`isnan` function to test + not considered to equal to any other numeric value, including themselves. To check + whether a number is a NaN, use the :func:`isnan` function to test for NaNs instead of ``is`` or ``==``. Example:: From cb51d82503fe89b910b54383b37e383b261355d2 Mon Sep 17 00:00:00 2001 From: CharlieZhao Date: Tue, 29 Mar 2022 14:24:45 +0800 Subject: [PATCH 4/4] remove examples related to `is` --- Doc/library/math.rst | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index afb2f4ec0ba410..c0f96142319122 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -659,10 +659,6 @@ Constants False >>> float('nan') == float('nan') False - >>> math.nan is math.nan - True - >>> float('nan') is float('nan') - False >>> math.isnan(math.nan) True >>> math.isnan(float('nan'))