You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/04/07 12:43:40 UTC
[commons-numbers] 08/09: Avoid reassigning parameters.
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 071aa588ba37907241ae684a0e34c014789450ec
Author: aherbert <ah...@apache.org>
AuthorDate: Tue Apr 7 13:33:23 2020 +0100
Avoid reassigning parameters.
---
.../commons/numbers/fraction/BigFraction.java | 11 +++++------
.../apache/commons/numbers/fraction/Fraction.java | 22 ++++++++++++++--------
2 files changed, 19 insertions(+), 14 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 9cbc43d..60f2c06 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
@@ -71,13 +71,12 @@ public final class BigFraction
// reduce numerator and denominator by greatest common denominator
final BigInteger gcd = num.gcd(den);
if (BigInteger.ONE.compareTo(gcd) < 0) {
- num = num.divide(gcd);
- den = den.divide(gcd);
+ numerator = num.divide(gcd);
+ denominator = den.divide(gcd);
+ } else {
+ numerator = num;
+ denominator = den;
}
-
- // store the values in the final fields
- numerator = num;
- denominator = den;
}
/**
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 f8fa171..a2acc21 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
@@ -73,23 +73,29 @@ public final class Fraction
numerator = 1;
denominator = 1;
} else {
+ // Reduce numerator (p) and denominator (q) by greatest common divisor.
+ int p;
+ int q;
+
// If num and den are both 2^-31, or if one is 0 and the other is 2^-31,
// the calculation of the gcd below will fail. Ensure that this does not
// happen by dividing both by 2 in case both are even.
if (((num | den) & 1) == 0) {
- num >>= 1;
- den >>= 1;
+ p = num >> 1;
+ q = den >> 1;
+ } else {
+ p = num;
+ q = den;
}
- // Reduce numerator and denominator by greatest common divisor.
- final int d = ArithmeticUtils.gcd(num, den);
+ final int d = ArithmeticUtils.gcd(p, q);
if (d > 1) {
- num /= d;
- den /= d;
+ p /= d;
+ q /= d;
}
- numerator = num;
- denominator = den;
+ numerator = p;
+ denominator = q;
}
}