[Issue 15763] New: std.math.approxEqual is not symmetric

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Mar 4 17:05:34 PST 2016


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

          Issue ID: 15763
           Summary: std.math.approxEqual is not symmetric
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: minor
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: r.97all at gmail.com

Current implementation of std.math.approxEqual is not symmetric.  The code
below gives an example.


import std.math : eq = approxEqual;
immutable real
    a = (2e-3)-(1e-5),
    b = (2e-3)+(1e-5);
assert ( a.eq(b)); // (error relative to b) = 1 / (100.5)
assert (!b.eq(a)); // (error relative to a) = 1 / ( 99.5)


IMO at least we need to document this.  It may be enough since the case this
asymmetricity do something harm is rare.

If we "fix" it, we must consider the backward compatibility, and/or
performance.

I would be pleased with an alternative for approxEqual like this:


return ((a - b).abs < abserr) || ((a - b).abs / (a.abs + b.abs) < relerr / 2);

Any opinion?

--


More information about the Digitalmars-d-bugs mailing list