Skip to content

Commit 613c76a

Browse files
[3.15] gh-162604: clarify NaN comparisons for Decimal's (GH-162639) (#162706)
gh-162604: clarify NaN comparisons for Decimal's (GH-162639) (cherry picked from commit 5dbe2ac) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
1 parent 5abbc18 commit 613c76a

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎Doc/library/decimal.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,8 @@ condition.
17291729
0 ** 0
17301730
x ** (non-integer)
17311731
x ** Infinity
1732+
NaN >= 0
1733+
NaN < 0
17321734

17331735

17341736
.. class:: Overflow
@@ -1858,6 +1860,8 @@ expanding the precision sufficiently to avoid loss of significance:
18581860
Decimal('0.0060000')
18591861

18601862

1863+
.. _decimal-notes-specials:
1864+
18611865
Special values
18621866
^^^^^^^^^^^^^^
18631867

‎Doc/reference/expressions.rst‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,12 +2129,15 @@ built-in types.
21292129
involved, they compare mathematically (algorithmically) correct without loss
21302130
of precision.
21312131

2132-
The not-a-number values ``float('NaN')`` and ``decimal.Decimal('NaN')`` are
2132+
The not-a-number floating-point numbers, like ``float('NaN')``, are
21332133
special. Any ordered comparison of a number to a not-a-number value is false.
21342134
A counter-intuitive implication is that not-a-number values are not equal to
21352135
themselves. For example, if ``x = float('NaN')``, ``3 < x``, ``x < 3`` and
21362136
``x == x`` are all false, while ``x != x`` is true. This behavior is
2137-
compliant with IEEE 754.
2137+
compliant with IEEE 754. Similar behavior holds for
2138+
:class:`decimal.Decimal`'s not-a-number values, like ``Decimal('NaN')``.
2139+
Refer to the :ref:`Special values <decimal-notes-specials>` section
2140+
of the :mod:`decimal` documentation.
21382141

21392142
* ``None`` and :data:`NotImplemented` are singletons. :PEP:`8` advises that
21402143
comparisons for singletons should always be done with ``is`` or ``is not``,

0 commit comments

Comments
 (0)