Made numerator and denominator final in Fraction and
deprecated protected real and imaginary parts fields in Complex, making Fraction immutable and preparing Complex to become fully immutable in 2.0. JIRA: MATH-188 Reported and patched by Sebastian Bazley git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@620373 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ce9150e867
commit
f967b359e7
|
@ -56,10 +56,16 @@ public class Complex implements Serializable {
|
|||
/** A complex number representing "0.0 + 0.0i" */
|
||||
public static final Complex ZERO = new Complex(0.0, 0.0);
|
||||
|
||||
/** The imaginary part */
|
||||
/**
|
||||
* The imaginary part
|
||||
* @deprecated to be made final and private in 2.0
|
||||
*/
|
||||
protected double imaginary;
|
||||
|
||||
/** The real part */
|
||||
/**
|
||||
* The real part
|
||||
* @deprecated to be made final and private in 2.0
|
||||
*/
|
||||
protected double real;
|
||||
|
||||
/**
|
||||
|
|
|
@ -37,10 +37,10 @@ public class Fraction extends Number implements Comparable {
|
|||
private static final long serialVersionUID = -8958519416450949235L;
|
||||
|
||||
/** The denominator. */
|
||||
private int denominator;
|
||||
private final int denominator;
|
||||
|
||||
/** The numerator. */
|
||||
private int numerator;
|
||||
private final int numerator;
|
||||
|
||||
/**
|
||||
* Create a fraction given the double value.
|
||||
|
@ -210,9 +210,20 @@ public class Fraction extends Number implements Comparable {
|
|||
num = -num;
|
||||
den = -den;
|
||||
}
|
||||
// reduce numerator and denominator by greatest common denominator.
|
||||
int d = MathUtils.gcd(num, den);
|
||||
if (d > 1) {
|
||||
num /= d;
|
||||
den /= d;
|
||||
}
|
||||
|
||||
// move sign to numerator.
|
||||
if (den < 0) {
|
||||
num *= -1;
|
||||
den *= -1;
|
||||
}
|
||||
this.numerator = num;
|
||||
this.denominator = den;
|
||||
reduce();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -531,23 +542,4 @@ public class Fraction extends Number implements Comparable {
|
|||
denominator /= gcd;
|
||||
return new Fraction(numerator, denominator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduce this fraction to lowest terms. This is accomplished by dividing
|
||||
* both numerator and denominator by their greatest common divisor.
|
||||
*/
|
||||
private void reduce() {
|
||||
// reduce numerator and denominator by greatest common denominator.
|
||||
int d = MathUtils.gcd(numerator, denominator);
|
||||
if (d > 1) {
|
||||
numerator /= d;
|
||||
denominator /= d;
|
||||
}
|
||||
|
||||
// move sign to numerator.
|
||||
if (denominator < 0) {
|
||||
numerator *= -1;
|
||||
denominator *= -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -172,6 +172,12 @@ Commons Math Release Notes</title>
|
|||
Fixed AbstractIntegerDistribution cumulativeProbablility(-,-)
|
||||
to correctly handle double arguments.
|
||||
</action>
|
||||
<action dev="psteitz" type="update" issue="MATH-188" due-to="Sebastian Bazley">
|
||||
Made numerator and denominator final in Fraction and
|
||||
deprecated protected real and imaginary parts fields in Complex,
|
||||
making Fraction immutable and preparing Complex to become fully
|
||||
immutable in 2.0.
|
||||
</action>
|
||||
</release>
|
||||
<release version="1.1" date="2005-12-17"
|
||||
description="This is a maintenance release containing bug fixes and enhancements.
|
||||
|
|
Loading…
Reference in New Issue