Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 953da2b1 authored by Timothy Knight's avatar Timothy Knight Committed by Android Git Automerger
Browse files

am 232c39ab: am 1b039b1d: Merge "Camera2: Updated Rational for denom=0." into klp-dev

* commit '232c39ab':
  Camera2: Updated Rational for denom=0.
parents 353f12d9 232c39ab
Loading
Loading
Loading
Loading
+61 −22
Original line number Diff line number Diff line
@@ -26,22 +26,17 @@ public final class Rational {
    /**
     * <p>Create a Rational with a given numerator and denominator.</p>
     *
     * <p>
     * The signs of the numerator and the denominator may be flipped such that the denominator
     * is always 0.
     * </p>
     * <p>The signs of the numerator and the denominator may be flipped such that the denominator
     * is always positive.</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 denominator the denominator of the rational
     *
     * @throws IllegalArgumentException if the denominator is 0
     */
    public Rational(int numerator, int denominator) {

        if (denominator == 0) {
            throw new IllegalArgumentException("Argument 'denominator' is 0");
        }

        if (denominator < 0) {
            numerator = -numerator;
            denominator = -denominator;
@@ -55,6 +50,9 @@ public final class Rational {
     * Gets the numerator of the rational.
     */
    public int getNumerator() {
        if (mDenominator == 0) {
            return 0;
        }
        return mNumerator;
    }

@@ -65,14 +63,30 @@ public final class Rational {
        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>A Rational object can only be equal to another Rational object (comparing against any other
     * type will return false).</p>
     *
     * <p>A Rational object is considered equal to another Rational object if and only if their
     * reduced forms have the same numerator and denominator.</p>
     * <p>A Rational object is considered equal to another Rational object if and only if one of
     * 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
     * denominator by their greatest common divisor.</p>
@@ -81,6 +95,9 @@ public final class Rational {
     *      (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(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>
     *
     * @param obj a reference to another object
@@ -91,13 +108,17 @@ public final class Rational {
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
        } else if (obj instanceof Rational) {
            Rational other = (Rational) obj;
            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;
                }
        if (obj instanceof Rational) {
            Rational other = (Rational) obj;
            if(mNumerator == other.mNumerator && mDenominator == other.mDenominator) {
            } else if (mNumerator == other.mNumerator && mDenominator == other.mDenominator) {
                return true;
            } else {
                int thisGcd = gcd();
@@ -117,8 +138,26 @@ public final class Rational {

    @Override
    public String toString() {
        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
    public int hashCode() {
+44 −7
Original line number Diff line number Diff line
@@ -50,12 +50,20 @@ public class RationalTest extends junit.framework.TestCase {
        assertEquals(1, r.getNumerator());
        assertEquals(2, r.getDenominator());

        // Dividing by zero is not allowed
        try {
        // Infinity.
        r = new Rational(1, 0);
            fail("Expected Rational constructor to throw an IllegalArgumentException");
        } catch(IllegalArgumentException e) {
        }
        assertEquals(0, r.getNumerator());
        assertEquals(0, r.getDenominator());

        // 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
@@ -110,5 +118,34 @@ public class RationalTest extends junit.framework.TestCase {
        assertEquals(moreComplicated, moreComplicated2);
        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));
    }
}