On 14.06.20 17:52, David Mertz wrote:
On Sun, Jun 14, 2020, 10:22 AM Greg Ewing <[email protected] <mailto:[email protected]>> wrote:On 15/06/20 12:39 am, Sebastian M. Ernst wrote: > It's such a common problem when dealing with floating point numbers Is it really? I've done quite a lot of work with floating point numbers, and I've very rarely needed to compare two of them for almost-equality. When I do, I always want to be in control of the tolerance rather than have a default tolerance provided for me. I've had occasion to use math.isclose(), np.isclose(), and np.allclose() quite often. And most of the time, the default tolerances are good enough for my purpose. Note that NumPy and math use different algorithms to define closeness, moreover.
I never use `math.isclose` or `np.isclose` without specifying tolerances, even if they happen to be the defaults (which is rare). There is no such thing as "x and y are approximately equal"; the question is always within what bounds. And this question must be answered by the programmer and the answer should be stated explicitly. Obviously these tolerances are application dependent, be it measurement errors, limited precision of sensors, numerical errors, etc. What makes the default values so special anyway? If I were to design such a function, I wouldn't provide any defaults at all. Yes, I read PEP-485, but I'm not convinced. The paragraph [Relative Tolerance Default](https://www.python.org/dev/peps/pep-0485/#relative-tolerance-default) starts with: > The relative tolerance required for two values to be considered "close" is entirely use-case dependent. That doesn't call for a default value. [`np.isclose`](https://numpy.org/doc/stable/reference/generated/numpy.isclose.html) is even more extreme: They also specify (non-zero) defaults and because of that they need to display a *warning* at their docs which reads: > The default atol is not appropriate for comparing numbers that are much smaller than one (see Notes). Then in the notes there is: > atol should be carefully selected for the use case at hand. Sounds like it would've been more appropriate to not specify a default in the first place. Sure, some people might complain, who want a quick way to determine if two numbers are approximately equal, but as mentioned above, this question cannot be answered without specifying the bounds. All that the default tolerances do is prevent people from thinking about the appropriate values for their specific application. Since an operator doesn't allow to specify any tolerances, it's not a suitable replacement for the `isclose` functions.
_______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/PEDZUPKJYKI5ZNLMEGJKDT7YGBZO4B36/ Code of Conduct: http://python.org/psf/codeofconduct/
