The humble Android calculator

4 min read Original article ↗

Jurjen Bos

Even among mathematicians, I am a calculator nerd. I literally have ten apps on my phone to do calculations (not counting a web link to Wolfram alpha). This includes the standard calculator, the insanely accurate free42, the graphical desmos, and also power tools like pari, xcas and python.

Each of these tools had their own strengths and weaknesses. Some are simple to use, but not very powerful, while others are extremely powerful, but harder to use.

The standard problem with calculators is that you do not know how accurate a result you get.

Try for yourself

A good and simple test to see what your calculator can do is tan(355/226) = -7.497.258,1853255871129…

What does your calculator give? Try it out. Don’t forget to set it to radians, of course.

Even the very high quality HP48SX gives -7497089.06508, a value that is almost 200 too high!

Cause of the problem

Why is that? If you do calculations with a limited precision, you have no choice than to round off intermediate results. For certain computations, these small roundoff errors may have large effects. This calculation shows the problem because 355/226 is very near ½π, where the tangent tends to ∞.

The problem is that it is really hard to understand when this occurs: the branch of mathematics called “numerical mathematics” studies these situations.

Surprising precision

But the standard Android calculator gives all the right digits. And if you turn your phone sideways, you get a insane amount of accuracy:

Press enter or click to view image in full size

Android calculator, sideways

I had been using this calculator for simple calculations, but never realized how powerful it was. Then I read an article titled “Towards an API for the Real Numbers” by Hans-J. Boehm. (Later I found a more readable version for the general public.)

He describes a way to encode numbers that is supposed to make computation results easier to understand for non-mathematicians. These numbers have the following properties:

  • Calculations with exact results are recognized as such: 5/4=1.25
  • All visible digits are correct

It is pretty hard to implement this, so the article explains all the details needed to do this right.

The idea is that a user can trust what is shown, without needing to understand the details about numerical accuracy.

I read the article, pondering the complexities of recognizing for example that √2.56 is exact while √1.000002 isn’t. Then, at the end, the authors revealed that the numbers are implemented in the calculator app on the very phone I used to read the article!

I started playing with it.

Press enter or click to view image in full size

1.6 means exactly 1.6 here

And indeed, it gives no extra digits if it knows for sure the results are exact:

  • sin(π/6) = 0.5
  • cos(π/3)² = 0.25
  • ln(1/e^5) = -5
  • √1234567654321 = 1111111
  • 2,25^(3÷2) = 3.375

But it cannot recognize all exact numbers:

Press enter or click to view image in full size

Exact, but hard to prove

This is not a bug; it is impossible to determine for every calculation of the result is exact (that’s one of those deep theorems of mathematics). The app had to stop somewhere.

But there’s more: you can scroll the number to the left. Then you can see for yourself that there are as many zeros as you care to check. But because the calculator doesn’t know for sure it’s exact, it refuses to make this claim.

That’s is unlike every other calculator. To prove my point, let’s take arguably the most powerful calculator there is.

Wolfram alpha bug?

Wolfram alpha knows more about math than I will ever learn. But unlike the Android calculator it doesn’t care checking its precision. You can see that here:

Press enter or click to view image in full size

Wolfram alpha sometimes lies to you

If you know a bit of mathematics, you know that the cosine cannot be zero at any value given as decimals. How can such a powerful tool make such a silly mistake?

Apparently, Wolfram alpha calculated a few digits, got zero, and concluded somehow that it is exactly zero. It didn’t bother proving that it is zero.

The actual value of cos(193098546,944) is about 0.000000000000613, which is small, but clearly not zero. This is caused by me choosing a very particular number; these are extremely rare.

What does this mean?

Google made a calculator that will always give you enough accuracy. This makes this calculator very useful for scholars, who will never be surprised by problems caused by the limitations of normal calculators.

And the beautiful thing is that you don’t have to understand anything about how this works. All the complexities of the math are beautifully hidden while they work for you. Technology at its best.

Thank you, Hans-J. I wish I could shake your hand.

Edited: I found that Calculator Culture already made a YouTube video about it. Thanks, Jan-Joris, for pointing that out.