MATH-1248
Unnecessary allocations in "BigFraction". Thanks to Chris Popp.
This commit is contained in:
parent
0448161808
commit
ae5d8f8a92
3
pom.xml
3
pom.xml
|
@ -350,6 +350,9 @@
|
|||
<contributor>
|
||||
<name>Xiaogang Zhang</name>
|
||||
</contributor>
|
||||
<contributor>
|
||||
<name>Chris Popp</name>
|
||||
</contributor>
|
||||
</contributors>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -119,10 +119,10 @@ public class BigFraction
|
|||
public BigFraction(BigInteger num, BigInteger den) {
|
||||
MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR);
|
||||
MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR);
|
||||
if (BigInteger.ZERO.equals(den)) {
|
||||
if (den.signum() == 0) {
|
||||
throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
|
||||
}
|
||||
if (BigInteger.ZERO.equals(num)) {
|
||||
if (num.signum() == 0) {
|
||||
numerator = BigInteger.ZERO;
|
||||
denominator = BigInteger.ONE;
|
||||
} else {
|
||||
|
@ -135,7 +135,7 @@ public class BigFraction
|
|||
}
|
||||
|
||||
// move sign to numerator
|
||||
if (BigInteger.ZERO.compareTo(den) > 0) {
|
||||
if (den.signum() == -1) {
|
||||
num = num.negate();
|
||||
den = den.negate();
|
||||
}
|
||||
|
@ -450,7 +450,7 @@ public class BigFraction
|
|||
* @return the absolute value as a {@link BigFraction}.
|
||||
*/
|
||||
public BigFraction abs() {
|
||||
return (BigInteger.ZERO.compareTo(numerator) <= 0) ? this : negate();
|
||||
return (numerator.signum() == 1) ? this : negate();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -467,6 +467,14 @@ public class BigFraction
|
|||
*/
|
||||
public BigFraction add(final BigInteger bg) throws NullArgumentException {
|
||||
MathUtils.checkNotNull(bg);
|
||||
|
||||
if (numerator.signum() == 0) {
|
||||
return new BigFraction(bg);
|
||||
}
|
||||
if (bg.signum() == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
return new BigFraction(numerator.add(denominator.multiply(bg)), denominator);
|
||||
}
|
||||
|
||||
|
@ -509,13 +517,17 @@ public class BigFraction
|
|||
* @return a {@link BigFraction} instance with the resulting values.
|
||||
* @throws NullArgumentException if the {@link BigFraction} is {@code null}.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction add(final BigFraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
}
|
||||
if (ZERO.equals(fraction)) {
|
||||
if (fraction.numerator.signum() == 0) {
|
||||
return this;
|
||||
}
|
||||
if (numerator.signum() == 0) {
|
||||
return fraction;
|
||||
}
|
||||
|
||||
BigInteger num = null;
|
||||
BigInteger den = null;
|
||||
|
@ -527,6 +539,11 @@ public class BigFraction
|
|||
num = (numerator.multiply(fraction.denominator)).add((fraction.numerator).multiply(denominator));
|
||||
den = denominator.multiply(fraction.denominator);
|
||||
}
|
||||
|
||||
if (num.signum() == 0) {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
return new BigFraction(num, den);
|
||||
|
||||
}
|
||||
|
@ -596,7 +613,18 @@ public class BigFraction
|
|||
* than {@code object}, 0 if they are equal.
|
||||
* @see java.lang.Comparable#compareTo(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(final BigFraction object) {
|
||||
int lhsSigNum = numerator.signum();
|
||||
int rhsSigNum = object.numerator.signum();
|
||||
|
||||
if (lhsSigNum != rhsSigNum) {
|
||||
return (lhsSigNum > rhsSigNum) ? 1 : -1;
|
||||
}
|
||||
if (lhsSigNum == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
BigInteger nOd = numerator.multiply(object.denominator);
|
||||
BigInteger dOn = denominator.multiply(object.numerator);
|
||||
return nOd.compareTo(dOn);
|
||||
|
@ -617,9 +645,12 @@ public class BigFraction
|
|||
if (bg == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
}
|
||||
if (BigInteger.ZERO.equals(bg)) {
|
||||
if (bg.signum() == 0) {
|
||||
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
|
||||
}
|
||||
if (numerator.signum() == 0) {
|
||||
return ZERO;
|
||||
}
|
||||
return new BigFraction(numerator, denominator.multiply(bg));
|
||||
}
|
||||
|
||||
|
@ -662,13 +693,17 @@ public class BigFraction
|
|||
* @throws NullArgumentException if the {@code fraction} is {@code null}.
|
||||
* @throws MathArithmeticException if the fraction to divide by is zero
|
||||
*/
|
||||
@Override
|
||||
public BigFraction divide(final BigFraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
}
|
||||
if (BigInteger.ZERO.equals(fraction.numerator)) {
|
||||
if (fraction.numerator.signum() == 0) {
|
||||
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
|
||||
}
|
||||
if (numerator.signum() == 0) {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
return multiply(fraction.reciprocal());
|
||||
}
|
||||
|
@ -870,6 +905,9 @@ public class BigFraction
|
|||
if (bg == null) {
|
||||
throw new NullArgumentException();
|
||||
}
|
||||
if (numerator.signum() == 0 || bg.signum() == 0) {
|
||||
return ZERO;
|
||||
}
|
||||
return new BigFraction(bg.multiply(numerator), denominator);
|
||||
}
|
||||
|
||||
|
@ -883,7 +921,12 @@ public class BigFraction
|
|||
* the {@code int} to multiply by.
|
||||
* @return a {@link BigFraction} instance with the resulting values.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction multiply(final int i) {
|
||||
if (i == 0 || numerator.signum() == 0) {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
return multiply(BigInteger.valueOf(i));
|
||||
}
|
||||
|
||||
|
@ -898,6 +941,10 @@ public class BigFraction
|
|||
* @return a {@link BigFraction} instance with the resulting values.
|
||||
*/
|
||||
public BigFraction multiply(final long l) {
|
||||
if (l == 0 || numerator.signum() == 0) {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
return multiply(BigInteger.valueOf(l));
|
||||
}
|
||||
|
||||
|
@ -911,12 +958,13 @@ public class BigFraction
|
|||
* @return a {@link BigFraction} instance with the resulting values.
|
||||
* @throws NullArgumentException if {@code fraction} is {@code null}.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction multiply(final BigFraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
}
|
||||
if (numerator.equals(BigInteger.ZERO) ||
|
||||
fraction.numerator.equals(BigInteger.ZERO)) {
|
||||
if (numerator.signum() == 0 ||
|
||||
fraction.numerator.signum() == 0) {
|
||||
return ZERO;
|
||||
}
|
||||
return new BigFraction(numerator.multiply(fraction.numerator),
|
||||
|
@ -931,6 +979,7 @@ public class BigFraction
|
|||
*
|
||||
* @return the negation of this fraction.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction negate() {
|
||||
return new BigFraction(numerator.negate(), denominator);
|
||||
}
|
||||
|
@ -959,6 +1008,13 @@ public class BigFraction
|
|||
* @return <tt>this<sup>exponent</sup></tt>.
|
||||
*/
|
||||
public BigFraction pow(final int exponent) {
|
||||
if (exponent == 0) {
|
||||
return ONE;
|
||||
}
|
||||
if (numerator.signum() == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (exponent < 0) {
|
||||
return new BigFraction(denominator.pow(-exponent), numerator.pow(-exponent));
|
||||
}
|
||||
|
@ -976,6 +1032,13 @@ public class BigFraction
|
|||
* @return <tt>this<sup>exponent</sup></tt> as a <code>BigFraction</code>.
|
||||
*/
|
||||
public BigFraction pow(final long exponent) {
|
||||
if (exponent == 0) {
|
||||
return ONE;
|
||||
}
|
||||
if (numerator.signum() == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (exponent < 0) {
|
||||
return new BigFraction(ArithmeticUtils.pow(denominator, -exponent),
|
||||
ArithmeticUtils.pow(numerator, -exponent));
|
||||
|
@ -995,7 +1058,14 @@ public class BigFraction
|
|||
* @return <tt>this<sup>exponent</sup></tt> as a <code>BigFraction</code>.
|
||||
*/
|
||||
public BigFraction pow(final BigInteger exponent) {
|
||||
if (exponent.compareTo(BigInteger.ZERO) < 0) {
|
||||
if (exponent.signum() == 0) {
|
||||
return ONE;
|
||||
}
|
||||
if (numerator.signum() == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (exponent.signum() == -1) {
|
||||
final BigInteger eNeg = exponent.negate();
|
||||
return new BigFraction(ArithmeticUtils.pow(denominator, eNeg),
|
||||
ArithmeticUtils.pow(numerator, eNeg));
|
||||
|
@ -1026,6 +1096,7 @@ public class BigFraction
|
|||
*
|
||||
* @return the reciprocal fraction.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction reciprocal() {
|
||||
return new BigFraction(denominator, numerator);
|
||||
}
|
||||
|
@ -1040,7 +1111,12 @@ public class BigFraction
|
|||
*/
|
||||
public BigFraction reduce() {
|
||||
final BigInteger gcd = numerator.gcd(denominator);
|
||||
return new BigFraction(numerator.divide(gcd), denominator.divide(gcd));
|
||||
|
||||
if (BigInteger.ONE.compareTo(gcd) < 0) {
|
||||
return new BigFraction(numerator.divide(gcd), denominator.divide(gcd));
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1057,6 +1133,13 @@ public class BigFraction
|
|||
if (bg == null) {
|
||||
throw new NullArgumentException();
|
||||
}
|
||||
if (bg.signum() == 0) {
|
||||
return this;
|
||||
}
|
||||
if (numerator.signum() == 0) {
|
||||
return new BigFraction(bg.negate());
|
||||
}
|
||||
|
||||
return new BigFraction(numerator.subtract(denominator.multiply(bg)), denominator);
|
||||
}
|
||||
|
||||
|
@ -1096,13 +1179,17 @@ public class BigFraction
|
|||
* @return a {@link BigFraction} instance with the resulting values
|
||||
* @throws NullArgumentException if the {@code fraction} is {@code null}.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction subtract(final BigFraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
}
|
||||
if (ZERO.equals(fraction)) {
|
||||
if (fraction.numerator.signum() == 0) {
|
||||
return this;
|
||||
}
|
||||
if (numerator.signum() == 0) {
|
||||
return fraction.negate();
|
||||
}
|
||||
|
||||
BigInteger num = null;
|
||||
BigInteger den = null;
|
||||
|
@ -1140,6 +1227,7 @@ public class BigFraction
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public BigFractionField getField() {
|
||||
return BigFractionField.getInstance();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue