# commons-issues mailing list archives

##### Site index · List index
Message view
Top
From "Gilles (JIRA)" <j...@apache.org>
Subject [jira] Commented: (MATH-370) NaN in "equals" methods
Date Tue, 11 May 2010 19:43:43 GMT
```
[ https://issues.apache.org/jira/browse/MATH-370?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12866296#action_12866296
]

Gilles commented on MATH-370:
-----------------------------

{code}
public static boolean equals(double x, double y) {
return (Double.isNaN(x) && Double.isNaN(y)) || x == y;
}
{code}

{code}
public static boolean equals(double x, double y, double eps) {
return equals(x, y) || (Math.abs(y - x) <= eps);
}
{code}

{code}
public static boolean equals(double[] x, double[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; ++i) {
if (!equals(x[i], y[i])) {
return false;
}
}
return true;
}
{code}

{code}
public static boolean equals(double x, double y, int maxUlps) {
// Check that "maxUlps" is non-negative and small enough so that the
// default NAN won't compare as equal to anything.
assert maxUlps > 0 && maxUlps < NAN_GAP;

long xInt = Double.doubleToLongBits(x);
long yInt = Double.doubleToLongBits(y);

// Make lexicographically ordered as a two's-complement integer.
if (xInt < 0) {
}
if (yInt < 0) {
}

return Math.abs(xInt - yInt) <= maxUlps;
}
{code}

The first assumes that NaN == NaN; the next two use the first; the last one also returns {{true}}
when both arguments are NaN.

I propose that these "equals" will return {{false}} when one or the other argument is NaN
(or contains a NaN for the array variant), and to create for each one an "equalsIncludingNaN"
variant that will behave as the current code of "equals".
All occurrences of "equals" currently in CM will be replaced by "equalsIncludingNaN" so that
no semantics change will happen.
CM will stay consistent and be compliant.

> NaN in "equals" methods
> -----------------------
>
>                 Key: MATH-370
>                 URL: https://issues.apache.org/jira/browse/MATH-370
>             Project: Commons Math
>          Issue Type: Bug
>            Reporter: Gilles
>            Priority: Minor
>
> In "MathUtils", some "equals" methods will return true if both argument are NaN.
> Unless I'm mistaken, this contradicts the IEEE standard.
> If nobody objects, I'm going to make the changes.

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

```
Mime
View raw message