summaryrefslogtreecommitdiffstats
path: root/src/com/android/calculator2/BoundedRational.java
diff options
context:
space:
mode:
authorHans Boehm <hboehm@google.com>2015-07-22 20:23:29 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2015-07-22 20:23:29 +0000
commit51f5acc6069b0ed9b4ee15132cc09e15b0307161 (patch)
tree0f9d5c5c63d59053268733c726694902248db9f5 /src/com/android/calculator2/BoundedRational.java
parent9d75c1ac50abcd44ab5870a56e112196dc8c87c1 (diff)
parent82e5a2f60afd1cc330ccfa0b81b8824964e976c7 (diff)
downloadandroid_packages_apps_ExactCalculator-51f5acc6069b0ed9b4ee15132cc09e15b0307161.tar.gz
android_packages_apps_ExactCalculator-51f5acc6069b0ed9b4ee15132cc09e15b0307161.tar.bz2
android_packages_apps_ExactCalculator-51f5acc6069b0ed9b4ee15132cc09e15b0307161.zip
am 82e5a2f6: Avoid starting long uninterruptible computations
* commit '82e5a2f60afd1cc330ccfa0b81b8824964e976c7': Avoid starting long uninterruptible computations
Diffstat (limited to 'src/com/android/calculator2/BoundedRational.java')
-rw-r--r--src/com/android/calculator2/BoundedRational.java11
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);