Skip to content

bpo-47031: Improve documentation for math.nan #32170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://en.wikipedia.org/wiki/IEEE_754>`_, ``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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too happy about this example because I could see us change this behavior in the future to return the same NaN object.

Copy link
Contributor Author

@CharlieZhao95 CharlieZhao95 Mar 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too happy about this example because I could see us change this behavior in the future to return the same NaN object.

I feel the same way, this example is confusing. This phenomenon is simply due to the differences in function implementations, not related to any standard.

Maybe we should omit the example about is and just keep docs to remind users not use is to check if a number is a NaN.

False
>>> math.isnan(math.nan)
True
>>> math.isnan(float('nan'))
True

.. versionchanged:: 3.11
It is now always available.
Expand Down