[Issue 15881] approxEqual Ignores maxAbsDiff

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Sep 7 08:02:46 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=15881

Berni <dlang at croco-puzzle.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dlang at croco-puzzle.com

--- Comment #4 from Berni <dlang at croco-puzzle.com> ---
Christopher Barker has collected a lot of thoughts on this theme in a proposal
for a similar function in python (called isclose there) [1]. The test
implementation with several versions for comparison can be found on github [2].

According to Barker the or-ing of the two tests is not the problem, but the
large default value for maxRelDiff (1e-2). Barker argues to use 1e-9 for python
floats (which seem to be D's doubles).

Additionally the function should be symmetric, i.e. the order of the first two
arguments should not matter. At present approxEqual(0.99001,1) !=
approxEqual(1,0.99001). This is mainly for not confusing users.

Barker suggests to use 0.0 for the default of maxAbsDiff, because it is
completely dependend on the usecase, and other defaults would be missleading.
People should think about what limit they need and not rely on a given default,
which may or may not be suitable.

Implementing this in Phobos will lead to lots of unittests failing, because
they rely on the actual implementation. Anyway, I think, we should follow
Barkers thoughts. I my oppinion they have their merits.

PS: It would be nice to have different default values for maxRelDiff, depending
on which floatingpoint numbers are used. But I don't know, if this is easy to
implement.

[1] https://www.python.org/dev/peps/pep-0485/
[2] https://github.com/PythonCHB/close_pep/blob/master/is_close.py

--


More information about the Digitalmars-d-bugs mailing list