History log of /packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
Revision Date Author Comments (<<< Hide modified files) (Show modified files >>>)
56bcbf121b29a035fba68a87b0c77ffa9d12a845 24-Sep-2016 Annie Chin <afchin@google.com> Accommodate unit tests.

Bug: 30416650

-Make some methods public for test access
-Add handling for KEYCODE_CLEAR
-Remove old tests

Change-Id: Icc180990b48e86d5a77d4b2ae6705927c540d10e
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
2ca183cd5d5b1b4d7ef8b76ea8b9f965dab7e1ff 24-Aug-2016 Hans Boehm <hboehm@google.com> Fix BoundedRational normalization

Bug: 31035142

Change maybeReduce to a static function, so that we can deal correctly
with results that might already be null.

Fix maybeReduce so that it actually returns the reduced result, not
the original. This was an undetected bug that I think only has
performance implications, and that only under conditions that are
unlikely for the calculator. Nonetheless the original code was
pretty silly.

Add tests for the original bug and other conceivable issues in that
vicinity.

Change-Id: I982dbbaff22f6b05f71a101fee38b25d527ed967
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
24c91edb89b3bfecb5167d3ba4f76cf0203cb547 01-Jul-2016 Hans Boehm <hboehm@google.com> Add digit grouping to display

Bug: 27461988

Add digit grouping separators to formula and result.

The result display piece of this is an annoyingly complex change,
since it interacts with our already subtle calculations of the
number of digits to display, our initial evaluation precision,
etc.

We display grouping separators in the result only when the entire
whole part of the number, with grouping separators, fits into
the display. If it fits into the display without, but not with,
grouping separators, we force scientific notation. This may require
the result to be scrollable when it otherwise would not be, and
leads to an interesting set of corner cases, which we claim to
handle reasonably.

Some cleanups were applied to make this easier, more useful, and
more debuggable. These included:

More accurate bookkeeping about different character widths. Otherwise
scrolling with grouping separators was not smooth.

Ignore grouping separators and spaces on input, as we should have
been doing all along.

Only redisplay the result if the character (as opposed to pixel)
position changed. This makes up for some extra computation and
facilitates debugging.

Introduce StringUtils.java to hold digit string operations that really
don't fit anywhere else. Move the duplicated repeat() function there.

Change-Id: I00502b9906b184671cd3379cd68b0447939b2394
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
995e5eb3b58a5cbef2686feb34f53493c1825866 08-Feb-2016 Hans Boehm <hboehm@google.com> Refactor arithmetic code. More symbolic result tracking.

Bug:21957368

Add UnifiedReal class, which represents the product of a CR and a
BoundedRational. Preferring well-known constants in the CR part
allows us to perform some symbolic-like symplifications, such as
keeping track of rational multiples of pi. But the real motivation
for this change is to factor out the combined rational/CR logic,
so that we can reuse it for other applications.

Our limited symbolic manipulations have several immediate effects:
- Evaluator.java is cleaner. The actual arithmetic is isaolated
to UnifiedReal.java, BoundedRational.java, and the external CR.java.
- We can display exact symbolic representations for some irrationals.
- We are guaranteed to produce correctly truncated results for
those values. Switches from zeroes to 9s can no longer occur
for those.
- We are more likely to be able to detect division by zero
up front instead of diverging. 1/(pi - pi) is detectable.
- Factorial argument validity checks are more likely to be exact.
- We can commonly determine the leading digit of a number near zero,
and hence quickly produce appropriate scientific notation more
often.
- Radian trig evaluations, like sin(pi), now produce exact rational
results.

We also remove the separate degree-based trig function implementations,
since they are no longer needed. Degrees transalate to exact multiples
of pi, which are now tracked correctly.

The logic for producing exact results from trig functions at special
points moved from BoundedRational to UnifiedReal, and was strengthened.

BoundedRational now uses a random normalization strategy, to allow
MAX_SIZE to be increased sufficiently to get more benefits
from some of the other changes.

Break BRTest.java into UnifiedRealTest.java and BoundedRationalTest.java.
The tests are still useful, but some now only apply to UnifiedReal,
since the transcendental functions no longer exist in BoundedRationsl.

Change-Id: Ic9d5d70252d54e17043c7328f69d93ab9225223f
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
65a99a423182145394f96d1430e1196ae3db1663 04-Feb-2016 Hans Boehm <hboehm@google.com> Copy and truncate rational results exactly

Bug: 26966394
Bug: 26175989

Provide a separate BoundedRational toString() function to handle
generation of decimal strings from rational results. This ensures
that rational result are always displayed as correctly truncated.
Use this facility to generate an exact representation when copying
a rational result, and when doing so is appropriate.

Includes some minor related changes to the same code:

- Refuse to copy a result that is currently being evaluated.
- Increase the bound on rational size again, to make all of this
more effective.
- Remove one line of dead code, and fix some comments, etc.

Change-Id: I5f72d5e47849ceeb2f1b6be870eb2a64edd5d508
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
997783ba5966a1a9fc5572007e314879ca70e1aa 02-Oct-2015 Hans Boehm <hboehm@google.com> Fix factorial(0)

Bug: 24575798

Make factorial(0) return 1, as intended.
Add a few factorial tests.
Fix targetPackage spec so test is actually runnable again.

Change-Id: Ibb8d827f0325999c6b5ed49a939e1532ddf55639
(cherry picked from commit 6951591806f46405564a6cb868a88d0daf6764d2)
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
f599db7639d61b030cde1189e3392be1f8c35a29 11-Aug-2015 Hans Boehm <hboehm@google.com> BoundedRational.java cleanup

Bug: 21469726
Bug: 23080519

Reformat to 100 columns. Reformat simple if-statements and rename
variables for better consistency with coding conventions.

Switch many comments to javadoc style.

Consistently use signum() to compare to zero. It's more concise and
probably slightly more efficient.

Change the asBigInteger implementation to use a single
divideAndRemainder call instead of relying on gcd. Add tests.

Eliminate redundant test in maybeReduce().

Change-Id: I4c275494e076612d09a05bc317c9972008619cda
(cherry picked from commit a4511f349124ca9cd2a7f9cb742be02b832a0206)
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
82e5a2f60afd1cc330ccfa0b81b8824964e976c7 21-Jul-2015 Hans Boehm <hboehm@google.com> Avoid starting long uninterruptible computations

Bug: 21957088

Shorten timeouts.

We usually do short computations first, so this helps to avoid
starting absurdly big ones.

Check size of result before converting to decimal. If the decimal
conversion is likely to time out anyway, just pretent we timed out.
This turns out to avoid a lot of problems, since BigInteger decimal
conversion is the clear bottleneck for something like 100000!
And its uninterruptible.

Remove the timeout in one case in which we had previously forgotten.

Check for interrupts in a couple of more places in BoundedRational.
One of these caused log(10^100000) to hang for a long time.

One or two trivial cleanups in code that was touched anyway.

Change-Id: I3494a8ed28f681fb26634ecd90042e2f2a8008a8
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
19e93c9806c245084d81df601d5ebc38c73dd5a7 20-Jun-2015 Hans Boehm <hboehm@google.com> Reflect conversion of CR to unchecked exceptions

Bug: 20667245

Update calculator code to reflect the fact that CR Errors have
become exceptions, and they are now delcared local to CR.

Change-Id: Ie9c9e10cef2f98a23856aa9e49328ae7ba52c9bc
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
c1ea091ae1a4d5145069bfc6248f83f5ca8f0b31 20-Jun-2015 Hans Boehm <hboehm@google.com> Improve logic for evaluation/animation interruption

Bug: 21471857
Bug: 20819212

End rather than cancel() in-progress animation in the event of user
interaction.

Discard input that interrupted a computation only for delete,
and clear, where it seems to make sense.

Use similar interruption logic for physical keyboard input as for
touch.

Make integer exponentiation more interruptible. This remains
imperfect; the latencies in a single BigInteger multiplication
can be high. Filed b/21957088 to track that.

Clear "instant" result before launching reevaluation. Otherwise the
example from b/21957088 shows incorrect instant results for an
uncomfortably long time as it's being entered.

Correct some of the state maintenance operations in Calculator.java.
The ANIMATE state was not being used correctly.

Remove redundant cancelAll() and onCancelled() calls.

Add an option to cancel without a message. Use it for clear.

Change-Id: Ibab90dca0cb894e7985642f212ff41030f2fc52d
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
cd740596f6f8bdacf02f735f116ffc544f922893 14-Jun-2015 Hans Boehm <hboehm@google.com> Fix digitsRequired() for negative denominators

Bug: 21789679

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

Change-Id: Ib007e753f90c019c37666d71c1cfd02301dcd360
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
50ed320c1cdd1b3624f73956a80eb2f2c2f5a01d 09-Jun-2015 Hans Boehm <hboehm@google.com> Fix getShortString(), tune evaluation heuristics.

Bug: 21474616

Rewrite getShortString() to also look at the least significant digit
information when available, and try to mimic the display formatting
code wherever appropriate. As a result, when the user hits "=",
followed by "+" transitions are now more frequently smooth.

Revise the evaluation heuristics so the we are more aggressive
with the initial evaluation precision, and try harder to discover
the leading digit in a near-zero number. Some of this is necessary
to keep getShortString() happy. This version should also now
guarantee that we are never worse than double precision floating
pointing in displaying very small nonzero numbers. If we display
a number as zero, the old calculator would have, too. (And now
you can scroll to see whether it really is.)

Up the BoundedRational bit limit to improve the chances of identifying
exact results.

In general, the incremental computation cost for operating on
larger BigIntegers appears relatively low, so it makes sense to
trade longer computations for fewer calls.

Change-Id: I33066845b832753c109fcaf27f883b48e7e119d2
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
4db31b490443e4454d98a5ae2bc44b87149accfe 31-May-2015 Hans Boehm <hboehm@google.com> Implement percent and new inverse functions

Bug: 21493470

Add x^2 10^x and e^x functions, to make the recently added INV key
work as expected.

Implement % functionality.

10^x is essentially just macro expansions for now.

% and x^2 need trivial evaluator support to provide reasonable display
syntax.

We decided to add evaluator support for exp() as well.

Add corresponding exp() support to BoundedRational and its tests.

Tiny incidental changes for problems uncovered in the process:
Fix bug in tests/README.txt
Evaluate the constant e only once.
Add one more power test along with the exp() test.
Fix proguard.flags so BRTest runs again.

Change-Id: I26cfcaf6d99aeec11387297cc5586e2ddcab6add
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
c023b734310f231244f17189788ae9c17c90b9a8 29-Apr-2015 Hans Boehm <hboehm@google.com> Fix timeout, cancellation and instant display logic

Bug: 20668802
Bug: 20484451
Bug: 20734909
Bug: 20738335

Change the timeout implementation to not rely on removing scheduled
timeouts when an evaluation completes. This seems simper, more clearly
correct and, unlike its predessessor seems to work reliably.
Correctly display "cancelled" message, but only when cancelled for a
reason other than timeout.

Change SyntaxError to not be a Java "Error", since we routinely recover
from it.

Better support evaluation of partial expressions by removing trailing
operators. Make some attempt to avoid redundant evaluations, since
they're not as cheap as they used to be. Change the display logic to
avoid frequent clearing of the display, which resulted in
visible flashing.

Note that the evaluation logic can now stop evaluating before the end
of an expression.

Substantially lengthen timeout used when computing an unrequested
result. Inverse trig functions can be slow. See b/20729963.

Make large factorial computations interruptable, as I mistakenly
thought they were.

Ignore "=" on an empty expression.

I'm not positive this fixes the b/20484451, but I haven't been able to
reproduce it with this patch.

I have no idea whether it addresses b/20738335, but it cleans up code
in that vicinity, so it would be interesting to know whether that is
still reproducible.

Change-Id: I67af5afc00e19a6156e1547ce4e2060c70ca367f
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
fbcef7005de4436682072927f83000b502928d25 28-Apr-2015 Hans Boehm <hboehm@google.com> Fix strings, stability bug, easy UI & correctness issues

Bug: 20625562
Bug: 20649711
Bug: 20561890
Bug: 20561528
Bug: 20442590
Bug: 15473140
Bug: 20503008
Bug: 20503007

- Improve timeout text.

- Recalculate when Calculator is rotated, e.g. in error state, thus
reproducing message. It's unclear this is good enough, but it's
better.

- Fix square root parsing.

- Fix concatenation of numbers when pasting by adding explicit
multiplication.

- Display divide by zero error differently from other domain errors.

- Improved advanced keypad layout of portrait-mode tablet calculator.

- Improved overflow menu order. (More to be done.)

- Report zero division as zero division when we can recognize it.

- Switch to floating menus for copy/paste.

Change-Id: I3875414f293e62a59b0e41f0de822f29bd5ac6a6
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
9e855e82dc86b3038b3e048a56be77af114e24f4 23-Apr-2015 Hans Boehm <hboehm@google.com> Fix decimal log(), rename positive_den()

Bug: 20485102

The rational version of base 10 log(0) got into an infinite loop.

Change-Id: Id6263f7244c71260daa124e5eb4eea8592d0e6c7
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
013969e98ce9e3eb4f87ec6159b06a74d07b2592 14-Apr-2015 Hans Boehm <hboehm@google.com> Internationalize display again. Plus minor cleanups.

Adds code for internationalization of numeric results, both in the
result and formula displays.

Update some now obsolete TODO comments.

Change-Id: I42731bf87f5488375457f1c5c094c7f0d17b71da
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
4a6b7cb235c305761af5d7f40e74d4704e5058c8 04-Apr-2015 Hans Boehm <hboehm@google.com> Fix UI holes and bugs. Fix eval bugs.

Change layout to make the result display use a fixed font
size and limit the number of characters when it appears below the
formula. This allows us to always get the proper expansion effect
and prevents scrolling from affecting the font size.

Add copy support for result display.

Add paste support for the formula.

Add keyboard input support.

Copy/paste can be used to remember old results in the calculator.
We save an identifying tag URI in the clip, in addition to text,
allowing us to paste old calculator results without precision
loss.

Copy/paste currently does not rely on selection at all.
I had trouble making it work that way in the formula. It's
unclear that would be better, since we only allow copy of the
entire text and paste at the end.

Add a couple of alternate result display options to the
overflow menu. (These appear quite useful, were trivial to
implement, and give us a better excuse for the overflow menu.)

Changed the behavior of the delete key in error state.
Changing it to CLEAR seemed unfriendly, since it prevents
corrections. This is a change from L.

Made it clear that the CalculatorHitSomeButtons test is
currently 95% worthless. It was apparentlly failing (due to test
infrastructure issues) but throwing an exception in a thread from
which it was not getting reported. Decided to keep it, since I
would like a place to continue collecting regression tests, even
if we can't actually run them yet.

Includes some easy drive-by fixes for expression evaluation:

a) 2 / 2 * 3 was mis-parsed as 2 / (2 * 3).

b) Cosine evaluation had the sense of the test for a rational result reversed.

c) Constants without leading digits, like .1, are now handled correctly,
and decimal points in the formula are now internationalized.
(That's not yet true for the result.)

Change-Id: Ic24466b444b4a4633cfb036c67622c7f4fd644ec
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
75ca21c698808b61238a3aff3e0a3dfd5ba95d0e 12-Mar-2015 Hans Boehm <hboehm@google.com> Add tests, fix BoundedRational bugs

Add back some basic test infrastructure from the KitKat calculator,
updated to make it work with the current code base and asynchronous
expression evaluation.

Add BoundedRational tests, designed to check that we get all the
corner cases, particularly for degree-based trig functions, right.

Fix a couple of BoundedRational typos that caused these tests to fail.

Change-Id: I81d8f3d9bde6aa6c20f9958eabd62979babeff5b
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java
682ff5e8ad465d74b289590e5c88e0cf129ca90b 09-Mar-2015 Hans Boehm <hboehm@google.com> Add BoundedRational evaluation

We would like to display finite representations of calculator results
when they clearly exist and are easy to identify, such as when adding
currency values. We do this by computing a rational representation
of the result when it exists, and using that to compute the number
of digits in a finite representation.

Since rational arithmetic can become very expensive, we bound the
size of the results we are willing to keep. If things get too large
we fall back on the standard constructive real arithmetic. Finite
representations are extremely unlikely in such cases anyway. This
also gives us a clear rule for when to normalize fractions, which
is often a challenge with rational number packages.

This also adds a couple of routines to set degree mode, but does
not include the UI to actually invoke them. Thus there is still
no way to test some important pieces of functionality.

Change-Id: I3c1aca5aefd8d8c19bce79095bde59ee3b4127fe
/packages/apps/ExactCalculator/src/com/android/calculator2/BoundedRational.java