digitalmars.D.bugs - [Issue 15763] New: std.math.approxEqual is not symmetric
- via Digitalmars-d-bugs (29/29) Mar 04 2016 https://issues.dlang.org/show_bug.cgi?id=15763
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 puremagic.com Reporter: r.97all 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? --
Mar 04 2016