[Python-ideas] Approximately equal operator
Antoine Pitrou
solipsis at pitrou.net
Fri Jun 15 13:53:41 EDT 2018
On Fri, 15 Jun 2018 14:38:22 -0300
Andre Roberge <andre.roberge at gmail.com>
wrote:
>
> 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.30000000000000004
> ~~> 0.1 + 0.2 == 0.3
> False
> ~~> from __experimental__ import approx
> ~~> 0.1 + 0.2 ~= 0.3 # use approximately equal operator
> True
> ~~> 0.1 + 0.2 <~= 0.3
> True
> ~~> 0.1 + 0.2 >~= 0.3
> True
> ~~> 2 ** 0.5
> 1.4142135623730951
> ~~> 2**0.5 ~= 1.414
> False
> ~~> set_tols(0.001, 0.001)
> ~~> 2**0.5 ~= 1.414
> True
On the one hand, this matches the corresponding math notation quite
pleasantly. On the other hand, it doesn't seem so useful that it
deserves to be a builtin operator. I'm also not sure we want to
encourage its use for anything other than experimenting at the prompt
and writing unit tests.
Being able to set a global tolerance setting is an anti-pattern IMHO,
regardless of the operator proposal.
Regards
Antoine.
More information about the Python-ideas
mailing list