am 1b039b1d: Merge "Camera2: Updated Rational for denom=0." into klp-dev
* commit '1b039b1d2e93cef528da3dcf01b328d6286e9d98': Camera2: Updated Rational for denom=0.
This commit is contained in:
@@ -26,22 +26,17 @@ public final class Rational {
|
|||||||
/**
|
/**
|
||||||
* <p>Create a Rational with a given numerator and denominator.</p>
|
* <p>Create a Rational with a given numerator and denominator.</p>
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>The signs of the numerator and the denominator may be flipped such that the denominator
|
||||||
* The signs of the numerator and the denominator may be flipped such that the denominator
|
* is always positive.</p>
|
||||||
* is always 0.
|
*
|
||||||
* </p>
|
* <p>A rational value with a 0-denominator may be constructed, but will have similar semantics
|
||||||
|
* as float NaN and INF values. The int getter functions return 0 in this case.</p>
|
||||||
*
|
*
|
||||||
* @param numerator the numerator of the rational
|
* @param numerator the numerator of the rational
|
||||||
* @param denominator the denominator of the rational
|
* @param denominator the denominator of the rational
|
||||||
*
|
|
||||||
* @throws IllegalArgumentException if the denominator is 0
|
|
||||||
*/
|
*/
|
||||||
public Rational(int numerator, int denominator) {
|
public Rational(int numerator, int denominator) {
|
||||||
|
|
||||||
if (denominator == 0) {
|
|
||||||
throw new IllegalArgumentException("Argument 'denominator' is 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (denominator < 0) {
|
if (denominator < 0) {
|
||||||
numerator = -numerator;
|
numerator = -numerator;
|
||||||
denominator = -denominator;
|
denominator = -denominator;
|
||||||
@@ -55,6 +50,9 @@ public final class Rational {
|
|||||||
* Gets the numerator of the rational.
|
* Gets the numerator of the rational.
|
||||||
*/
|
*/
|
||||||
public int getNumerator() {
|
public int getNumerator() {
|
||||||
|
if (mDenominator == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return mNumerator;
|
return mNumerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,22 +63,41 @@ public final class Rational {
|
|||||||
return mDenominator;
|
return mDenominator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isNaN() {
|
||||||
|
return mDenominator == 0 && mNumerator == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isInf() {
|
||||||
|
return mDenominator == 0 && mNumerator > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isNegInf() {
|
||||||
|
return mDenominator == 0 && mNumerator < 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Compare this Rational to another object and see if they are equal.</p>
|
* <p>Compare this Rational to another object and see if they are equal.</p>
|
||||||
*
|
*
|
||||||
* <p>A Rational object can only be equal to another Rational object (comparing against any other
|
* <p>A Rational object can only be equal to another Rational object (comparing against any other
|
||||||
* type will return false).</p>
|
* type will return false).</p>
|
||||||
*
|
*
|
||||||
* <p>A Rational object is considered equal to another Rational object if and only if their
|
* <p>A Rational object is considered equal to another Rational object if and only if one of
|
||||||
* reduced forms have the same numerator and denominator.</p>
|
* the following holds</p>:
|
||||||
|
* <ul><li>Both are NaN</li>
|
||||||
|
* <li>Both are infinities of the same sign</li>
|
||||||
|
* <li>Both have the same numerator and denominator in their reduced form</li>
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* <p>A reduced form of a Rational is calculated by dividing both the numerator and the
|
* <p>A reduced form of a Rational is calculated by dividing both the numerator and the
|
||||||
* denominator by their greatest common divisor.</p>
|
* denominator by their greatest common divisor.</p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* (new Rational(1, 2)).equals(new Rational(1, 2)) == true // trivially true
|
* (new Rational(1, 2)).equals(new Rational(1, 2)) == true // trivially true
|
||||||
* (new Rational(2, 3)).equals(new Rational(1, 2)) == false // trivially false
|
* (new Rational(2, 3)).equals(new Rational(1, 2)) == false // trivially false
|
||||||
* (new Rational(1, 2)).equals(new Rational(2, 4)) == true // true after reduction
|
* (new Rational(1, 2)).equals(new Rational(2, 4)) == true // true after reduction
|
||||||
|
* (new Rational(0, 0)).equals(new Rational(0, 0)) == true // NaN.equals(NaN)
|
||||||
|
* (new Rational(1, 0)).equals(new Rational(5, 0)) == true // both are +infinity
|
||||||
|
* (new Rational(1, 0)).equals(new Rational(-1, 0)) == false // +infinity != -infinity
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param obj a reference to another object
|
* @param obj a reference to another object
|
||||||
@@ -91,13 +108,17 @@ public final class Rational {
|
|||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
} else if (obj instanceof Rational) {
|
||||||
if (this == obj) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (obj instanceof Rational) {
|
|
||||||
Rational other = (Rational) obj;
|
Rational other = (Rational) obj;
|
||||||
if(mNumerator == other.mNumerator && mDenominator == other.mDenominator) {
|
if (mDenominator == 0 || other.mDenominator == 0) {
|
||||||
|
if (isNaN() && other.isNaN()) {
|
||||||
|
return true;
|
||||||
|
} else if (isInf() && other.isInf() || isNegInf() && other.isNegInf()) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if (mNumerator == other.mNumerator && mDenominator == other.mDenominator) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
int thisGcd = gcd();
|
int thisGcd = gcd();
|
||||||
@@ -117,7 +138,25 @@ public final class Rational {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return mNumerator + "/" + mDenominator;
|
if (isNaN()) {
|
||||||
|
return "NaN";
|
||||||
|
} else if (isInf()) {
|
||||||
|
return "Infinity";
|
||||||
|
} else if (isNegInf()) {
|
||||||
|
return "-Infinity";
|
||||||
|
} else {
|
||||||
|
return mNumerator + "/" + mDenominator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Convert to a floating point representation.</p>
|
||||||
|
*
|
||||||
|
* @return The floating point representation of this rational number.
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
public float toFloat() {
|
||||||
|
return (float) mNumerator / (float) mDenominator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -50,12 +50,20 @@ public class RationalTest extends junit.framework.TestCase {
|
|||||||
assertEquals(1, r.getNumerator());
|
assertEquals(1, r.getNumerator());
|
||||||
assertEquals(2, r.getDenominator());
|
assertEquals(2, r.getDenominator());
|
||||||
|
|
||||||
// Dividing by zero is not allowed
|
// Infinity.
|
||||||
try {
|
r = new Rational(1, 0);
|
||||||
r = new Rational(1, 0);
|
assertEquals(0, r.getNumerator());
|
||||||
fail("Expected Rational constructor to throw an IllegalArgumentException");
|
assertEquals(0, r.getDenominator());
|
||||||
} catch(IllegalArgumentException e) {
|
|
||||||
}
|
// Negative infinity.
|
||||||
|
r = new Rational(-1, 0);
|
||||||
|
assertEquals(0, r.getNumerator());
|
||||||
|
assertEquals(0, r.getDenominator());
|
||||||
|
|
||||||
|
// NaN.
|
||||||
|
r = new Rational(0, 0);
|
||||||
|
assertEquals(0, r.getNumerator());
|
||||||
|
assertEquals(0, r.getDenominator());
|
||||||
}
|
}
|
||||||
|
|
||||||
@SmallTest
|
@SmallTest
|
||||||
@@ -110,5 +118,34 @@ public class RationalTest extends junit.framework.TestCase {
|
|||||||
assertEquals(moreComplicated, moreComplicated2);
|
assertEquals(moreComplicated, moreComplicated2);
|
||||||
assertEquals(moreComplicated2, moreComplicated);
|
assertEquals(moreComplicated2, moreComplicated);
|
||||||
|
|
||||||
|
Rational nan = new Rational(0, 0);
|
||||||
|
Rational nan2 = new Rational(0, 0);
|
||||||
|
assertTrue(nan.equals(nan));
|
||||||
|
assertTrue(nan.equals(nan2));
|
||||||
|
assertTrue(nan2.equals(nan));
|
||||||
|
assertFalse(nan.equals(r));
|
||||||
|
assertFalse(r.equals(nan));
|
||||||
|
|
||||||
|
// Infinities of the same sign are equal.
|
||||||
|
Rational posInf = new Rational(1, 0);
|
||||||
|
Rational posInf2 = new Rational(2, 0);
|
||||||
|
Rational negInf = new Rational(-1, 0);
|
||||||
|
Rational negInf2 = new Rational(-2, 0);
|
||||||
|
assertEquals(posInf, posInf);
|
||||||
|
assertEquals(negInf, negInf);
|
||||||
|
assertEquals(posInf, posInf2);
|
||||||
|
assertEquals(negInf, negInf2);
|
||||||
|
|
||||||
|
// Infinities aren't equal to anything else.
|
||||||
|
assertFalse(posInf.equals(negInf));
|
||||||
|
assertFalse(negInf.equals(posInf));
|
||||||
|
assertFalse(negInf.equals(r));
|
||||||
|
assertFalse(posInf.equals(r));
|
||||||
|
assertFalse(r.equals(negInf));
|
||||||
|
assertFalse(r.equals(posInf));
|
||||||
|
assertFalse(posInf.equals(nan));
|
||||||
|
assertFalse(negInf.equals(nan));
|
||||||
|
assertFalse(nan.equals(posInf));
|
||||||
|
assertFalse(nan.equals(negInf));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user