Hans Boehm | 50f1bac | 2015-08-11 19:23:02 -0700 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Calculator Implementation Overview</title> |
| 5 | <meta charset="UTF-8"> |
| 6 | </head> |
| 7 | <h1>M Calculator Implementation Overview</h1> |
| 8 | <p>Although the appearance of the calculator has changed little from Lollipop, and some of the UI |
| 9 | code is indeed the same, the rest of the code has changed substantially. Unsurprisingly, |
| 10 | <b>Calculator.java</b> implements the main UI. The other major parts of the implementation |
| 11 | are:</p> |
| 12 | |
| 13 | <p><b>CR.java</b> in <b>external/crcalc</b> provides the underlying demand-driven ("constructive |
| 14 | real") arithmetic implementation. Numbers are represented primarily as objects with a method that |
| 15 | can compute arbitrarily precise approximations. The actual arithmetic performed by these methods |
| 16 | is based on Java's <tt>java.util.BigInteger</tt> arithmetic, with appropriate implicit |
| 17 | scaling.</p> |
| 18 | |
| 19 | <p><b>BoundedRational.java</b> is a rational arithmetic package that is used to provide finite |
| 20 | exact answers in "easy" cases. It is used primarily to determine when an approximation provided |
| 21 | by CR.java is actually exact. This is used in turn both to limit the length of displayed results |
| 22 | and scrolling, as well as to identify errors such as division by zero, that would otherwise result |
| 23 | in timeouts during computations. It is in some sense not needed to produce correct results, but |
| 24 | it significantly improves the usability of the calculator. It is also used for the "display as |
| 25 | fraction" option in the overflow menu.</p> |
| 26 | |
| 27 | <p><b>CalculatorExpr.java</b> implements calculator arithmetic expressions. It supports editing, |
| 28 | saving, restoring, and evaluation of expressions. Evaluation produces a constructive real (CR) |
| 29 | and possibly a BoundedRational result. Unlike the "arity" library used in earlier versions, the |
| 30 | underlying expression is represented as a sequence of "tokens", many of which are represented by |
| 31 | Button ids, not as a character string.</p> |
| 32 | |
| 33 | <p><b>Evaluator.java</b> implements much of the actual calculator logic, particularly background |
| 34 | expression evaluation. Expression evaluation here includes both using CalculatorExpr.java to |
| 35 | evaluate the expression, and then invoking the resulting CR value to actually produce finite |
| 36 | approximations and convert them to decimal. Two types of expression evaluation are supported: |
| 37 | (1) Initial evaluation of the expression and producing an initial decimal approximation, and (2) |
| 38 | reevaluation to higher precision. (1) is invoked directly from the Calculator UI, while (2) is |
| 39 | invoked from the calculator display, commonly in response to scrolling. When the display requests |
| 40 | a result, a "result" is immediately returned, though it may contains blank placeholders. The |
| 41 | display is then notified when the real result becomes available.</p> |
| 42 | |
| 43 | <p><b>CalculatorText.java</b> is the TextView subclass used to display the formula.</p> |
| 44 | |
| 45 | <p><b>CalculatorResult.java</b> is the TextView subclass used to display the result. It handles |
| 46 | result formatting, scrolling, etc. After the user hits "=", the CalculatorResult widget moves |
| 47 | into the top position, replacing the formula display. Currently it remains in that position until |
| 48 | the formula is again modified.</p> |
| 49 | </body> |
| 50 | </html> |
| 51 | |