Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NUMBERS-152] add a private constructor for already known reduced num and den, to avoid unneeded gcd. #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ 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);
}
if (num == den) {
numerator = 1;
denominator = 1;
} else {
numerator = num;
denominator = den;
}
}

/**
* Private constructor: Instances are created using factory methods.
*
Expand Down Expand Up @@ -470,8 +494,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 +507,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 +605,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 +698,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 +764,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 +813,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