Thread overview
[Issue 3232] New: std.math.approxEqual should consider maxAbsDiff when rhs==0 && lhs!=0
August 07, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3232

           Summary: std.math.approxEqual should consider maxAbsDiff when
                    rhs==0 && lhs!=0
           Product: D
           Version: 2.031
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bugzilla@kyllingen.net


Currently, approxEqual doesn't take the maximum absolute difference into account when rhs is zero while lhs is nonzero. An example:

  double epsrel = 0.01;  // ...or whatever, it doesn't matter
  double epsabs = 1e-5; // This matters when rhs or lhs is zero!

  assert (approxEqual(0.0, 1e-10, epsrel, epsabs));  // OK
  assert (approxEqual(1e-10, 0.0, epsrel, epsabs));  // Fails!

This is very unintuitive -- I think the order of the "operands" shouldn't matter here. The offending piece of code is at line 3087 of std.math (rev. 1233):

    if (rhs == 0) {
        return (lhs == 0 ? 0 : 1) <= maxRelDiff;
    }

This could be changed to:

    if (rhs == 0) {
        return (lhs == 0 ? 0 : 1) <= maxRelDiff
            || (maxAbsDiff != 0 && fabs(rhs-lhs) <= maxAbsDiff);
    }

Another option, if abs(lhs-rhs)/rhs and abs(lhs-rhs)/lhs could be considered
equally good definitions of the relative difference, would be this:

    if (rhs == 0) {
        if (lhs == 0) return true;
        // Switch lhs and rhs
        return approxEqual(rhs, lhs, maxRelDiff, maxAbsDiff);
    }

I actually prefer this one, because the name "approxEqual" doesn't in any way imply that the order of its arguments matter. It should simply return true if lhs and rhs are approximately equal, regardless of which is which.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 11, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3232





--- Comment #1 from Lars T. Kyllingstad <bugzilla@kyllingen.net>  2009-08-11 00:36:02 PDT ---
I just realised the two solutions I suggested are exactly equivalent. :)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 11, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3232


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 27, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3232


Lars T. Kyllingstad <bugzilla@kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


--- Comment #2 from Lars T. Kyllingstad <bugzilla@kyllingen.net> 2010-08-27 06:44:27 PDT ---
Fixed by Andrei a long time ago.

http://www.dsource.org/projects/phobos/changeset/1313

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------