www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3232] New: std.math.approxEqual should consider maxAbsDiff when rhs==0 && lhs!=0

reply d-bugmail puremagic.com writes:
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: -------
Aug 07 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3232






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: -------
Aug 11 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Oct 11 2009
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Aug 27 2010