[ 
https://issues.apache.org/jira/browse/MATH-475?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12982004#action_12982004
 ] 

Gilles commented on MATH-475:
-----------------------------

{quote}
assertTrue(MathUtils.equals(top, pen, 1.0)); // OK - implies the difference is 
<= 1.0
{quote}

The comment seems reasonable but I'm not sure that it must be correct when 
dealing with floating point numbers.

If we change the definition of {{equals(a, b, eps)}} to
{noformat}
  min(a, b) + abs(eps) >= max(a, b)
{noformat}
then
{noformat}
  equals(top, pen, 1.0)
{noformat}
will return {{true}}, because
{noformat}
 pen + 1.0 = top
{noformat}
although
{noformat}
 pen - top == 2.0
{noformat}

However,
{noformat}
  equals(top, pen, 0.98)
{noformat}
will return {{false}} because
{noformat}
  pen + 0.98 == pen
{noformat}

IIUC, the issue is: What is the meaning of {{equals(a, b, eps)}} when {{eps < 
ulp(max(a, b))}} ?
Because in that case, I think that the call is equivalent to the strict 
equality test: {{a == b}}, I'd say that the call to "equals" is also 
meaningless.

The current implementation short-circuits the problem by assuming that two 
floating point numbers {{a}} and {{b}} separated by 1 ulp are equally likely 
representations of any real number within the interval {{[a, b]}}.


> MathUtils.equals(double, double, double) does not agree with Javadoc
> --------------------------------------------------------------------
>
>                 Key: MATH-475
>                 URL: https://issues.apache.org/jira/browse/MATH-475
>             Project: Commons Math
>          Issue Type: Bug
>            Reporter: Sebb
>
> MathUtils.equals(double, double, double) does not agree with its Javadoc.
> The Javadoc says:
> "Returns true if both arguments are equal or within the range of allowed 
> error (inclusive)."
> However the following test fails:
> {code}
> double top=1.7976931348623184E16;
> double pen=1.7976931348623182E16;
> double diff=Math.abs(top-pen);
> assertTrue(MathUtils.equals(top, pen, 1.0)); // OK - implies the difference 
> is <= 1.0
> assertTrue("expected < 1.0, but was: "+diff,diff <= 1.0); // reports: 
> expected < 1.0, but was: 2.0
> {code}
> This discrepancy is because the equals(double, double, double) method also 
> checks to see if the numbers are within one ULP of each other.
> Either the Javadoc needs to be corrected, or the code needs to be corrected 
> to drop the ULP comparison.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to