diff options
author | Tal Einat <taleinat@gmail.com> | 2015-05-31 19:05:00 (GMT) |
---|---|---|
committer | Tal Einat <taleinat@gmail.com> | 2015-05-31 19:05:00 (GMT) |
commit | d5519ed7f4889060363673ec802177250299920e (patch) | |
tree | 90bf7cc72a340c9512bcf7b4d0837ac845347c6a /Doc | |
parent | 439c5fe3ae62741f01da7e78a9c198375e837857 (diff) | |
download | cpython-d5519ed7f4889060363673ec802177250299920e.zip cpython-d5519ed7f4889060363673ec802177250299920e.tar.gz cpython-d5519ed7f4889060363673ec802177250299920e.tar.bz2 |
Issue #19543: Implementation of isclose as per PEP 485
For details, see:
PEP 0485 -- A Function for testing approximate equality
Functions added: math.isclose() and cmath.isclose().
Original code by Chris Barker. Patch by Tal Einat.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/cmath.rst | 32 | ||||
-rw-r--r-- | Doc/library/math.rst | 32 | ||||
-rw-r--r-- | Doc/whatsnew/3.5.rst | 21 |
3 files changed, 85 insertions, 0 deletions
diff --git a/Doc/library/cmath.rst b/Doc/library/cmath.rst index a981d94..ab619a0 100644 --- a/Doc/library/cmath.rst +++ b/Doc/library/cmath.rst @@ -207,6 +207,38 @@ Classification functions and ``False`` otherwise. +.. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) + + Return ``True`` if the values *a* and *b* are close to each other and + ``False`` otherwise. + + Whether or not two values are considered close is determined according to + given absolute and relative tolerances. + + *rel_tol* is the relative tolerance -- it is the maximum allowed difference + between *a* and *b*, relative to the larger absolute value of *a* or *b*. + For example, to set a tolerance of 5%, pass ``rel_tol=0.05``. The default + tolerance is ``1e-09``, which assures that the two values are the same + within about 9 decimal digits. *rel_tol* must be greater than zero. + + *abs_tol* is the minimum absolute tolerance -- useful for comparisons near + zero. *abs_tol* must be at least zero. + + If no errors occur, the result will be: + ``abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)``. + + The IEEE 754 special values of ``NaN``, ``inf``, and ``-inf`` will be + handled according to IEEE rules. Specifically, ``NaN`` is not considered + close to any other value, including ``NaN``. ``inf`` and ``-inf`` are only + considered close to themselves. + + .. versionadded:: 3.5 + + .. seealso:: + + :pep:`485` -- A function for testing approximate equality + + Constants --------- diff --git a/Doc/library/math.rst b/Doc/library/math.rst index a88d1ac..244663e 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -110,6 +110,38 @@ Number-theoretic and representation functions .. versionadded:: 3.5 +.. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) + + Return ``True`` if the values *a* and *b* are close to each other and + ``False`` otherwise. + + Whether or not two values are considered close is determined according to + given absolute and relative tolerances. + + *rel_tol* is the relative tolerance -- it is the maximum allowed difference + between *a* and *b*, relative to the larger absolute value of *a* or *b*. + For example, to set a tolerance of 5%, pass ``rel_tol=0.05``. The default + tolerance is ``1e-09``, which assures that the two values are the same + within about 9 decimal digits. *rel_tol* must be greater than zero. + + *abs_tol* is the minimum absolute tolerance -- useful for comparisons near + zero. *abs_tol* must be at least zero. + + If no errors occur, the result will be: + ``abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)``. + + The IEEE 754 special values of ``NaN``, ``inf``, and ``-inf`` will be + handled according to IEEE rules. Specifically, ``NaN`` is not considered + close to any other value, including ``NaN``. ``inf`` and ``-inf`` are only + considered close to themselves. + + .. versionadded:: 3.5 + + .. seealso:: + + :pep:`485` -- A function for testing approximate equality + + .. function:: isfinite(x) Return ``True`` if *x* is neither an infinity nor a NaN, and diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst index ee0e5d1..085ade7 100644 --- a/Doc/whatsnew/3.5.rst +++ b/Doc/whatsnew/3.5.rst @@ -285,6 +285,18 @@ rather than being restricted to ASCII. :pep:`488` -- Multi-phase extension module initialization +PEP 485: A function for testing approximate equality +---------------------------------------------------- + +:pep:`485` adds the :func:`math.isclose` and :func:`cmath.isclose` +functions which tell whether two values are approximately equal or +"close" to each other. Whether or not two values are considered +close is determined according to given absolute and relative tolerances. + +.. seealso:: + + :pep:`485` -- A function for testing approximate equality + Other Language Changes ====================== @@ -346,6 +358,13 @@ cgi * :class:`~cgi.FieldStorage` now supports the context management protocol. (Contributed by Berker Peksag in :issue:`20289`.) +cmath +----- + +* :func:`cmath.isclose` function added. + (Contributed by Chris Barker and Tal Einat in :issue:`24270`.) + + code ---- @@ -578,6 +597,8 @@ math * :data:`math.inf` and :data:`math.nan` constants added. (Contributed by Mark Dickinson in :issue:`23185`.) +* :func:`math.isclose` function added. + (Contributed by Chris Barker and Tal Einat in :issue:`24270`.) shutil ------ |