[Python-ideas] Approximately equal operator

Andre Roberge andre.roberge at gmail.com
Fri Jun 15 13:38:22 EDT 2018

I have a suggestion to make inspired by the current discussion about
trigonometric functions in degrees, and the desire to have them show
"exact" values in some special cases.

I suggest that it would be useful to have operators for performing
**approximate** comparisons. I believe that such operators would be useful
both for students learning using Python as well as experts doing numerical

For discussion purpose, I will use ~= as representing an operator testing
for approximate equality. (I will show some sample usage below).

When teaching students, the availability of both == and ~= would give the
opportunity to discuss the fact that numerical computations using floats
are approximate, while having the possibility to write code that is
readable using the approximate equality operator instead of the strict
equality operator when needed.

Before I started writing this email, I had noticed that numpy includes at
least two functions (isclose and allclose) whose purpose is to perform such
approximate comparisons. [1]  I had completely missed the fact that Python
added the function isclose() in the math module in version 3.5, as
described in PEP 485 [0].  I would suggest that the possibility of using
operators instead of explicit function calls could make programs easier to
write and read, both for beginners and experts alike.  I note that PEP 485
makes no mention of introducing operators as a possibility.

In addition to an approximate equality operator, it would be natural to
include two additional operators, greater than or approximately equal, and
lesser than or approximately equal.  These could be written respectively as
>~= and <~=.  I did consider using some relevant utf-8 symbol instead of
combination of ascii characters, but I think that it would be easier to
write programs if one does not require characters nor found on any normal

Some time ago, I created a toy module [2] to enable easy experiments with
some syntactic additions to Python.  Using this module, I created a very
poor and limited implementation that shows what using these proposed
o[erators might look like [3] ...  My current implementation is slightly
different from either Numpy or Python's math.isclose() function [This may
no longer be the case soon as I plan to change it to use Python's version
instead.] .  As is the case for isclose(), there are two paramaters to be
set to determine if the values are close enough to be considered
approximately equal: an absolute tolerance and a relative one.  Given that
one cannot pass parameters to an operator, my implementation includes a
function which can change the values of these parameters for a given
session. If these new operators were to be added to Python, such a function
would either have to be added as a builtin or as a special function in the
math module.

Here's a sample session for demonstration purpose...

$ python -m experimental
experimental console version 0.9.5. [Python version: 3.6.1]

~~> 0.1 + 0.2
~~> 0.1 + 0.2 == 0.3
~~> from __experimental__ import approx
~~> 0.1 + 0.2 ~= 0.3    # use approximately equal operator
~~> 0.1 + 0.2 <~= 0.3
~~> 0.1 + 0.2 >~= 0.3
~~> 2 ** 0.5
~~> 2**0.5 ~= 1.414
~~> set_tols(0.001, 0.001)
~~> 2**0.5 ~= 1.414

André Roberge

[0] https://www.python.org/dev/peps/pep-0485/

[1] See for example

[2] https://github.com/aroberge/experimental

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180615/9124a47f/attachment.html>

More information about the Python-ideas mailing list