computed digit, we know they are not
equal. But if the two numbers were in
fact the same, this process would go
This still improves on floating-point arithmetic—equality is easily
decidable, but tells us even less about
equality of the true mathematical real
numbers approximated by the floating
This undecidability of equality does
create some interesting issues. If we divide a number by x, the calculator will
compute more and more digits of x until it finds some nonzero ones. If x was,
in fact, exactly zero, this process will
We deal with this problem using two
1. We always run numeric computations in the background, where
they will not interfere with user interactions, just in case they take a long
time. If they do take a really long time,
we time them out and inform the
user that the computation has been
aborted. This is unlikely to happen by
accident, unless the user entered an
ill-defined mathematical expression,
like a division by zero.
2. As we will see, in many cases we
use an additional number representation that does allow us to determine
that a number is exactly zero. Although
this easily handles most cases, it is
not foolproof. If the user enters “ 1÷0”
we immediately detect the division by
zero. If the user enters “ 1÷(π2÷π–π)” we
Zeros Further Than the Eye Can See
Prototypes of our calculator, like
mathematicians, treated all computed results as infinite objects, with
infinitely many digits to scroll through. If
the actual computation happened to be 2
– 1, the result was initially displayed as
1.00000000, and the user could keep
scrolling through as many thousands of
zeroes to the right of that as he desired.
Although mathematically sound, this
proved unpopular for several good reasons, the first one probably more serious than the others:
1. If we computed $1.23 + $7.89, the
result would show up as 9.1200000000
or the like, which is unexpected and
2. Many users consider the result of
1+ 2 to be a finite number, and find it
confusing to be able to scroll through
lots of zeros on the right.
3. Since the calculator could not
ever tell that a number was not going to
be scrolled, it could not treat any result
as short enough to allow the use of a
These problems were largely addressed by evaluating expression to
not just a constructive real number,
but also to a rational number represented as a (numerator, denominator) pair. The latter is unavailable if
the computation involved an irrational number, or the rational representation is too large.
This allows us to tell whether a result has a finite decimal representation, and if so, how many digits there
are to the right of the decimal point.
We simply look at the fraction in lowest terms. If the denominator has
a prime factor other than 2 or 5, the
decimal expansion is clearly infinite;
no number of multiplications by 10
can turn the fraction into an integer.
Otherwise the denominator can be
factored as 2n5m and the number of
nonzero digits to the right of the decimal point is max(m,n).
If the expansion is finite, we prevent scrolling past that point. We
also prevent scrolling through a large
number of trailing zeroes to the left of
the decimal point. This often leaves
us with a short nonscrollable result,
for which we can use a larger font. Unlike the floating-point case, such short,
large font results are always exact, and
never attributable to a number that
was merely close to a short decimal
This is, however, fallible in the other
direction. For example, we do not
compute a rational representation for
1÷(π2÷π–π), and hence it is still possible
to scroll through as many zeros of that
result as you like.
This underlying fractional representation of the result is also used to
directly detect, for example, division by
zero, making it much less likely that a
casual user will ever see a timeout.
The calculator described here is
available through Google Play Store,
and is also the default calculator distributed with Android 6.0 Marshmallow and later.
Initial reviews of the calculator
liked several unrelated UI and functionality changes, but failed to notice
the change in arithmetic.o We were apparently successful in having the accuracy guarantees not interfere with
The calculator now exposes the rational representation to the user when it
is available. That has turned out to be a
useful feature on its own, though it was
motivated by other considerations.
Feedback has been quite positive.
But it, together with our own experience, has also suggested some improvements:
˲Scrolling results have generated
far more interest than the much more
subtle precision improvements. The
latter seem to have been recognized
only by an absence of bug reports. As a
result, performance of a different kind
actually does matter: Users did notice
sluggishness when scrolling through
30,000 digits of π. And we subsequently switched to the better-performing
Gauss-Legendre algorithm for π.
˲ The semantics of calculator expressions are more subtle and controversial than we had realized. Is 50+10%
equal to 50. 1 or 55? If the latter, what’s
50x5%? If 2π is a valid expression, what
˲ The most recent versions of our
calculator explicitly track rational multiples of π and some other common
irrational constants. This allows us to
compute a rational result for sin(π/6)
in radian mode, as we already did for
The calculator UI design and implementation of course relied on contributions from several others, most notably Justin Klaassen.
o For example, see http://www.androidpolice.
Hans-J. Boehm ( firstname.lastname@example.org) is a software engineer
at Google, Palo Alto, CA.
Copyright held by owner/author.