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

Commit 82d19f31 authored by hoffc's avatar hoffc Committed by Linux Build Service Account
Browse files

ExactCalculator: Fix StackOverflowError issue

Calculate e^1209! will trigger the StackOverflowError, which results in
ExactCalculator exit unexpectedly.

Change-Id: Iab080ee98824c213183d5798073365f7de712c63
CRs-Fixed: 1022502
parent 2d20bbc2
Loading
Loading
Loading
Loading
+15 −8
Original line number Diff line number Diff line
@@ -873,19 +873,25 @@ class CalculatorExpr {
     * Unlike the "general" case using logarithms, this handles a negative base.
     */
    private static CR pow(CR base, BigInteger exp) {
        if (exp.compareTo(BigInteger.ZERO) < 0) {
            return pow(base, exp.negate()).inverse();
        BigInteger bigInteger = new BigInteger(exp.toString());

        if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
            return pow(base, bigInteger.negate()).inverse();
        }
        if (exp.equals(BigInteger.ONE)) {

        if (bigInteger.equals(BigInteger.ONE)) {
            return base;
        }
        if (exp.and(BigInteger.ONE).intValue() == 1) {
            return pow(base, exp.subtract(BigInteger.ONE)).multiply(base);

        if (bigInteger.and(BigInteger.ONE).intValue() == 1) {
            return pow(base, bigInteger.subtract(BigInteger.ONE)).multiply(base);
        }
        if (exp.equals(BigInteger.ZERO)) {

        if (bigInteger.equals(BigInteger.ZERO)) {
            return CR.valueOf(1);
        }
        CR tmp = pow(base, exp.shiftRight(1));

        CR tmp = pow(base, bigInteger.shiftRight(1));
        return tmp.multiply(tmp);
    }

@@ -961,7 +967,8 @@ class CalculatorExpr {
            // values.  That wouldn't work reliably with floating point either.
            BigInteger int_exp = BoundedRational.asBigInteger(exp.ratVal);
            if (int_exp != null) {
                crVal = pow(crVal, int_exp);
                BigInteger bigInteger = new BigInteger(int_exp.toString());
                crVal = pow(crVal, bigInteger);
            } else {
                crVal = crVal.ln().multiply(exp.val).exp();
            }