Skip to content

Commit

Permalink
Merge 8202d13 into c3d173a
Browse files Browse the repository at this point in the history
  • Loading branch information
XenoAmess committed Aug 26, 2020
2 parents c3d173a + 8202d13 commit 60de44f
Showing 1 changed file with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ private Fraction(int num, int den) {
}
}

/**
* Private constructor: Instances are created using factory methods.
*
* <p>This constructor should only be invoked when the fraction is known
* to be already reduced, and non-ZERO;
* otherwise use {@link #Fraction(int, int)}, or {@link #ZERO}.
*
* @param num Numerator.
* @param den Denominator.
* @param ignore Ignored, do not really use this, just to make it can overload.
*/
private Fraction(int num, int den, boolean ignore) {
if (den == 0) {
throw new FractionException(FractionException.ERROR_ZERO_DENOMINATOR);
}
numerator = num;
denominator = den;
}

/**
* Private constructor: Instances are created using factory methods.
*
Expand Down Expand Up @@ -470,8 +489,8 @@ public Fraction abs() {
@Override
public Fraction negate() {
return numerator == Integer.MIN_VALUE ?
new Fraction(numerator, -denominator) :
new Fraction(-numerator, denominator);
new Fraction(numerator, -denominator, false) :
new Fraction(-numerator, denominator, false);
}

/**
Expand All @@ -483,7 +502,7 @@ public Fraction negate() {
*/
@Override
public Fraction reciprocal() {
return new Fraction(denominator, numerator);
return new Fraction(denominator, numerator, false);
}

/**
Expand Down Expand Up @@ -581,7 +600,7 @@ public Fraction subtract(final int value) {
if (isZero()) {
// Special case for min value
return value == Integer.MIN_VALUE ?
new Fraction(Integer.MIN_VALUE, -1) :
new Fraction(Integer.MIN_VALUE, -1, false) :
new Fraction(-value);
}
// Convert to numerator with same effective denominator
Expand Down Expand Up @@ -674,7 +693,7 @@ public Fraction multiply(final int value) {
// (see multiply(Fraction) using value / 1 as the argument).
final int d2 = ArithmeticUtils.gcd(value, denominator);
return new Fraction(Math.multiplyExact(numerator, value / d2),
denominator / d2);
denominator / d2, false);
}

/**
Expand Down Expand Up @@ -740,7 +759,7 @@ public Fraction divide(final int value) {
// (see multiply(Fraction) using 1 / value as the argument).
final int d1 = ArithmeticUtils.gcd(numerator, value);
return new Fraction(numerator / d1,
Math.multiplyExact(denominator, value / d1));
Math.multiplyExact(denominator, value / d1), false);
}

/**
Expand Down Expand Up @@ -789,18 +808,18 @@ public Fraction pow(final int exponent) {
}
if (exponent > 0) {
return new Fraction(ArithmeticUtils.pow(numerator, exponent),
ArithmeticUtils.pow(denominator, exponent));
ArithmeticUtils.pow(denominator, exponent), false);
}
if (exponent == -1) {
return this.reciprocal();
}
if (exponent == Integer.MIN_VALUE) {
// MIN_VALUE can't be negated
return new Fraction(ArithmeticUtils.pow(denominator, Integer.MAX_VALUE) * denominator,
ArithmeticUtils.pow(numerator, Integer.MAX_VALUE) * numerator);
ArithmeticUtils.pow(numerator, Integer.MAX_VALUE) * numerator, false);
}
return new Fraction(ArithmeticUtils.pow(denominator, -exponent),
ArithmeticUtils.pow(numerator, -exponent));
ArithmeticUtils.pow(numerator, -exponent), false);
}

/**
Expand Down

0 comments on commit 60de44f

Please sign in to comment.