diff options
author | Hans Boehm <hboehm@google.com> | 2015-07-24 03:02:42 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-07-24 03:02:42 +0000 |
commit | 2a3b06bd233e082af77d5eaa2009d30eac0beb60 (patch) | |
tree | 6ebbe3e4cca28409254172988d7fa180b08ee90d /src/com/android/calculator2/BoundedRational.java | |
parent | 70f49ecd24447f735e78143358abd716482f783e (diff) | |
parent | 51f5acc6069b0ed9b4ee15132cc09e15b0307161 (diff) | |
download | android_packages_apps_ExactCalculator-2a3b06bd233e082af77d5eaa2009d30eac0beb60.tar.gz android_packages_apps_ExactCalculator-2a3b06bd233e082af77d5eaa2009d30eac0beb60.tar.bz2 android_packages_apps_ExactCalculator-2a3b06bd233e082af77d5eaa2009d30eac0beb60.zip |
am 51f5acc6: am 82e5a2f6: Avoid starting long uninterruptible computations
* commit '51f5acc6069b0ed9b4ee15132cc09e15b0307161':
Avoid starting long uninterruptible computations
Diffstat (limited to 'src/com/android/calculator2/BoundedRational.java')
-rw-r--r-- | src/com/android/calculator2/BoundedRational.java | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/com/android/calculator2/BoundedRational.java b/src/com/android/calculator2/BoundedRational.java index 70c21af..2b3d1ed 100644 --- a/src/com/android/calculator2/BoundedRational.java +++ b/src/com/android/calculator2/BoundedRational.java @@ -87,6 +87,11 @@ public class BoundedRational { return CR.valueOf(mNum).divide(CR.valueOf(mDen)); } + // Approximate number of bits to left of binary point. + public int wholeNumberBits() { + return mNum.bitLength() - mDen.bitLength(); + } + private boolean tooBig() { if (mDen.equals(BigInteger.ONE)) return false; return (mNum.bitLength() + mDen.bitLength() > MAX_SIZE); @@ -418,6 +423,9 @@ public class BoundedRational { // This algorithm is very naive, but we doubt it matters. long count = 0; while (n.mod(BigInteger.TEN).equals(BigInteger.ZERO)) { + if (Thread.interrupted()) { + throw new CR.AbortedException(); + } n = n.divide(BigInteger.TEN); ++count; } @@ -502,6 +510,9 @@ public class BoundedRational { if (r.mDen.equals(BigInteger.ONE)) return 0; r = r.reduce(); BigInteger den = r.mDen; + if (den.bitLength() > MAX_SIZE) { + return Integer.MAX_VALUE; + } while (!den.testBit(0)) { ++powers_of_two; den = den.shiftRight(1); |