Use Double.isNaN rather than x != x in FastMath.

Thanks to Benedikt Ritter.

Github: closes #5.
JIRA: MATH-1222
This commit is contained in:
Luc Maisonobe 2015-05-09 22:03:14 +02:00
parent 35ad940908
commit 903f280595
3 changed files with 18 additions and 12 deletions

View File

@ -296,6 +296,9 @@
<contributor>
<name>S&#233;bastien Riou</name>
</contributor>
<contributor>
<name>Benedikt Ritter</name>
</contributor>
<contributor>
<name>Bill Rossi</name>
</contributor>

View File

@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</release>
<release version="4.0" date="XXXX-XX-XX" description="">
<action dev="luc" type="fix" issue="MATH-1222" due-to="Benedikt Ritter">
Use Double.isNaN rather than x != x in FastMath.
</action>
<action dev="tn" type="fix"> <!-- backported to 3.6 -->
Fix potential branching errors in "FastMath#pow(double, double)" when
passing special values, i.e. infinity, due to erroneous JIT optimization.

View File

@ -392,7 +392,7 @@ public class FastMath {
* @return hyperbolic cosine of x
*/
public static double cosh(double x) {
if (x != x) {
if (Double.isNaN(x)) {
return x;
}
@ -462,7 +462,7 @@ public class FastMath {
*/
public static double sinh(double x) {
boolean negate = false;
if (x != x) {
if (Double.isNaN(x)) {
return x;
}
@ -588,7 +588,7 @@ public class FastMath {
public static double tanh(double x) {
boolean negate = false;
if (x != x) {
if (Double.isNaN(x)) {
return x;
}
@ -991,7 +991,7 @@ public class FastMath {
* @return exp(x) - 1
*/
private static double expm1(double x, double hiPrecOut[]) {
if (x != x || x == 0.0) { // NaN or zero
if (Double.isNaN(x) || x == 0.0) { // NaN or zero
return x;
}
@ -1155,7 +1155,7 @@ public class FastMath {
long bits = Double.doubleToRawLongBits(x);
/* Handle special cases of negative input, and NaN */
if (((bits & 0x8000000000000000L) != 0 || x != x) && x != 0.0) {
if (((bits & 0x8000000000000000L) != 0 || Double.isNaN(x)) && x != 0.0) {
if (hiPrec != null) {
hiPrec[0] = Double.NaN;
}
@ -1462,9 +1462,9 @@ public class FastMath {
if (y == 0.0) {
return 1.0;
} else if (x != x) { // X is NaN
} else if (Double.isNaN(x)) {
return x;
} else if (y != y) { // y is NaN
} else if (Double.isNaN(y)) {
return y;
} else if (x == 0) {
long bits = Double.doubleToRawLongBits(x);
@ -2587,7 +2587,7 @@ public class FastMath {
* @return phase angle of point (x,y) between {@code -PI} and {@code PI}
*/
public static double atan2(double y, double x) {
if (x != x || y != y) {
if (Double.isNaN(x) || Double.isNaN(y)) {
return Double.NaN;
}
@ -2708,7 +2708,7 @@ public class FastMath {
* @return arc sine of x
*/
public static double asin(double x) {
if (x != x) {
if (Double.isNaN(x)) {
return Double.NaN;
}
@ -2784,7 +2784,7 @@ public class FastMath {
* @return arc cosine of x
*/
public static double acos(double x) {
if (x != x) {
if (Double.isNaN(x)) {
return Double.NaN;
}
@ -3344,7 +3344,7 @@ public class FastMath {
public static double floor(double x) {
long y;
if (x != x) { // NaN
if (Double.isNaN(x)) {
return x;
}
@ -3371,7 +3371,7 @@ public class FastMath {
public static double ceil(double x) {
double y;
if (x != x) { // NaN
if (Double.isNaN(x)) {
return x;
}