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

Commit cd740596 authored by Hans Boehm's avatar Hans Boehm
Browse files

Fix digitsRequired() for negative denominators

Bug: 21789679

Also adds some tests for digitsRequired(), including one that fails
without this CL.

Change-Id: Ib007e753f90c019c37666d71c1cfd02301dcd360
parent b3f7278a
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -485,6 +485,7 @@ public class BoundedRational {
    }

    private static final BigInteger BIG_FIVE = BigInteger.valueOf(5);
    private static final BigInteger BIG_MINUS_ONE = BigInteger.valueOf(-1);

    // Return the number of decimal digits to the right of the
    // decimal point required to represent the argument exactly,
@@ -514,7 +515,9 @@ public class BoundedRational {
        // (Recall the fraction was in lowest terms to start with.)
        // Otherwise the powers of 10 we need to cancel the denominator
        // is the larger of powers_of_two and powers_of_five.
        if (!den.equals(BigInteger.ONE)) return Integer.MAX_VALUE;
        if (!den.equals(BigInteger.ONE) && !den.equals(BIG_MINUS_ONE)) {
            return Integer.MAX_VALUE;
        }
        return Math.max(powers_of_two, powers_of_five);
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -139,6 +139,12 @@ public class BRTest extends TestCase {
        check(BR_0.signum() == 0, "signum(0)");
        check(BR_M1.signum() == -1, "signum(-1)");
        check(BR_2.signum() == 1, "signum(2)");
        check(BoundedRational.digitsRequired(BoundedRational.ZERO) == 0, "digitsRequired(0)");
        check(BoundedRational.digitsRequired(BoundedRational.HALF) == 1, "digitsRequired(1/2)");
        check(BoundedRational.digitsRequired(BoundedRational.MINUS_HALF) == 1,
                "digitsRequired(-1/2)");
        check(BoundedRational.digitsRequired(new BoundedRational(1,-2)) == 1,
                "digitsRequired(1/-2)");
        // We check values that include all interesting degree values.
        BoundedRational r = BR_M390;
        while (!r.equals(BR_390)) {