commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aherb...@apache.org
Subject [commons-numbers] 04/09: Remove a branch from equals tests with NaN.
Date Sat, 04 Apr 2020 21:16:34 GMT
This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 08edae944e4abe7ec4b95a72d3791a9f39fb53c8
Author: Alex Herbert <aherbert@apache.org>
AuthorDate: Sat Apr 4 21:23:08 2020 +0100

    Remove a branch from equals tests with NaN.
    
    A bitwise OR of the booleans is true if either boolean is true. Since
    they are both already computed then the || operator short circuit (if
    first is true) cannot avoid having to compute the second boolean.
    
    This change converts 3 conditional evaluations to 2 conditional
    evaluations.
---
 .../main/java/org/apache/commons/numbers/core/Precision.java | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
index d60806d..4155b51 100644
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
+++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
@@ -177,7 +177,8 @@ public final class Precision {
     public static boolean equalsIncludingNaN(float x, float y) {
         final boolean xIsNan = Float.isNaN(x);
         final boolean yIsNan = Float.isNaN(y);
-        return xIsNan || yIsNan ?
+        // Combine the booleans with bitwise OR
+        return (xIsNan | yIsNan) ?
             !(xIsNan ^ yIsNan) :
             equals(x, y, 1);
     }
@@ -274,7 +275,8 @@ public final class Precision {
     public static boolean equalsIncludingNaN(float x, float y, int maxUlps) {
         final boolean xIsNan = Float.isNaN(x);
         final boolean yIsNan = Float.isNaN(y);
-        return xIsNan || yIsNan ?
+        // Combine the booleans with bitwise OR
+        return (xIsNan | yIsNan) ?
             !(xIsNan ^ yIsNan) :
             equals(x, y, maxUlps);
     }
@@ -302,7 +304,8 @@ public final class Precision {
     public static boolean equalsIncludingNaN(double x, double y) {
         final boolean xIsNan = Double.isNaN(x);
         final boolean yIsNan = Double.isNaN(y);
-        return xIsNan || yIsNan ?
+        // Combine the booleans with bitwise OR
+        return (xIsNan | yIsNan) ?
             !(xIsNan ^ yIsNan) :
             equals(x, y, 1);
     }
@@ -428,7 +431,8 @@ public final class Precision {
     public static boolean equalsIncludingNaN(double x, double y, int maxUlps) {
         final boolean xIsNan = Double.isNaN(x);
         final boolean yIsNan = Double.isNaN(y);
-        return xIsNan || yIsNan ?
+        // Combine the booleans with bitwise OR
+        return (xIsNan | yIsNan) ?
             !(xIsNan ^ yIsNan) :
             equals(x, y, maxUlps);
     }


Mime
View raw message