commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aherb...@apache.org
Subject [commons-numbers] 03/04: Updated Fraction/BigFraction signum()
Date Wed, 08 Apr 2020 12:14:46 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 fbcde793e465e265ba3ee2ee88cc3d4fcf59cb89
Author: aherbert <aherbert@apache.org>
AuthorDate: Wed Apr 8 12:28:31 2020 +0100

    Updated Fraction/BigFraction signum()
    
    Use the method from BigInteger.divide(BigInteger).
    
    The sign of the result is equal to a.signum() * b.signum() for a/b.
    
    This exploits the fact that BigInteger explicitly stores the signum as a
    field thus removing the need for signed comparators in the
    BigFraction.signum() method.
    
    For Fraction the use of Integer.signum(int) for numerator and
    denominator is the equivalent operation. This is also branchless due to
    use of bit manipulation in Integer.signum to compute the sign.
---
 .../org/apache/commons/numbers/fraction/BigFraction.java     | 12 +-----------
 .../java/org/apache/commons/numbers/fraction/Fraction.java   |  9 +--------
 2 files changed, 2 insertions(+), 19 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index 60f2c06..cee2a6e 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -488,17 +488,7 @@ public final class BigFraction
      * positive, 0 if it is 0.
      */
     public int signum() {
-        final int numS = numerator.signum();
-        final int denS = denominator.signum();
-
-        if ((numS > 0 && denS > 0) ||
-            (numS < 0 && denS < 0)) {
-            return 1;
-        } else if (numS == 0) {
-            return 0;
-        } else {
-            return -1;
-        }
+        return numerator.signum() * denominator.signum();
     }
 
     /**
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index a2acc21..31e7cde 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -373,14 +373,7 @@ public final class Fraction
      * positive, 0 if it is 0.
      */
     public int signum() {
-        if ((numerator > 0 && denominator > 0) ||
-            (numerator < 0 && denominator < 0)) {
-            return 1;
-        } else if (isZero()) {
-            return 0;
-        } else {
-            return -1;
-        }
+        return Integer.signum(numerator) * Integer.signum(denominator);
     }
 
     /**


Mime
View raw message