1 + 1: Not as easy at it looks
February 16, 2025 1:04 PM   Subscribe

(10100) + 1 - (10100) = 0
"That picture above is from iOS calculator. Notice anything? It's wrong. (10^100) + 1 − (10^100) is 1, not 0. [...] The purpose of a calculator app is to give you correct answers. Floating point numbers are imprecise - they cannot represent 0.3 or 10^100. This means a calculator built on floating point numbers is like a house built on sand. [...] A calculator should show you the result of the mathematical expression you entered. That's much, much harder than it sounds. What I'm about to tell you is the greatest calculator app development story ever told." posted by Rhaomi (18 comments total) 16 users marked this as a favorite
 
One link?

Who are you, and what have you done with Rhaomi?
posted by Lemkin at 1:23 PM on February 16 [3 favorites]


This is great, thanks! Until recently, I was a math tutor working with college students, and really had to push some of them not to try to solve things with their phone calculators. Though even on the venerable TI-84's most recent iteration, there's a way it expresses scientific notation that you have to remember that if it shows you, say, "1.6E10" you actually need to write, "1.6 x 10^11) I may have the details wrong) because of something about its calculation and expression of it that doesn't quite match what we learned to do on paper.
posted by Well I never at 1:47 PM on February 16 [1 favorite]


This was really fascinating. I don't think I quite followed it all the way to the end, but I love getting a glimpse into the underlying complications of what does indeed look like a very simple thing.
posted by Well I never at 1:56 PM on February 16


Floating point is simultaneously awesome and terrible. Give me enough bits and I can depict any scale of number, but at the cost of precision. You can have very very very big, or very very very small, but what you can't have is both at once, that is, the bigger the number the fewer decimal places you get.

If your number gets big enough, you start losing whole integers. An amazing resource, I find, in explaining some computer science topics is the Mario 64 A-Button Challenge guy, PannenKoek, the "parallel universes" person. Here's his video explaining floats, in a Mario 64 kind of context.
posted by JHarris at 1:57 PM on February 16 [3 favorites]


This is also a solid explainer for rational analysis, the common "for any tiny delta you give me, I can find a smaller epsilon closer to the actual answer" of college math (what do I know, I only did UK) university maths.

Thanks Ryvar. Anyone know if this is used in the new Magic Calculator?
posted by k3ninho at 2:02 PM on February 16


Python 3 supports “infinite” precision integers, limited only by your system memory, so this particular exercise wouldn’t “break” like the article. Irrational numbers remain problematic.
posted by funkaspuck at 2:07 PM on February 16


> Python 3 supports “infinite” precision integers

Came here to say "LISP bignums are the answer to this"

Though think the example would exceed the physical storage on your computer and I'm not sure how long the operation would take, is you had enough physical bytes of store.
posted by Aardvark Cheeselog at 2:13 PM on February 16


This is a delightful article, does a great job of explaining just how many barriers you hit trying to actually implement calculations on even a reasonably small subset of "real" numbers. Integers and rational numbers are reasonably well behaved (although even there you can end up using more memory than you'd like if you insist on exactness), but once you want to fill in those pesky gaps? There be dragons.
posted by Proofs and Refutations at 2:28 PM on February 16




The Apple calculator has improved, until very recently it would give the wrong answer to (1+2)2 which is something you are much more likely to run into than using powers of 100.
posted by Lanark at 2:35 PM on February 16


Significant Digits have always rubbed me the wrong way...

No, all the digits are significant says my mathematical brain.

This seems similar
posted by Windopaene at 2:40 PM on February 16


Python 3 supports “infinite” precision integers, limited only by your system memory, so this particular exercise wouldn’t “break” like the article. Irrational numbers remain problematic.
posted by funkaspuck at 17:07 on February 16
[+] [⚑]


The second example in the article is Python failing at rational numbers (1/10 and 2/10 are both infinitely repeating when expressed in binary)
posted by I paid money to offer this... insight? at 2:48 PM on February 16


I suppose this is why it took 14 years to get a calculator app for iPad.
posted by theory at 2:49 PM on February 16


Came here to say "LISP bignums are the answer to this"

The article says:

Some problems can be avoided if you use bignums.

Most numeric types are always 2 or 4 bytes. Not so for bignums. These are integers without bounds. They grow in memory as needed.

This solves the (10^100) + 1 - (10^100) example. But bignums are integers. How about fractions?

posted by crazy_yeti at 3:21 PM on February 16 [1 favorite]


the greatest calculator app, you say?

echo '10 100 ^ 1 + 10 100 ^ - p' | dc
1


H-J Boehm is cool and all, but Lorinda Cherry wins it
posted by scruss at 3:29 PM on February 16


Am I the only one for which the missing newline on this post fucks up the formatting of the front page?
posted by Rhomboid at 3:33 PM on February 16


spigot (windows .exe + portable source code) is an infinite precision ("exact real") calculator using a neat set of algorithms called "spigot algorithms" that can incrementally compute to any desired precision. However, it doesn't have the smarts to know e.g., that 'cos-1(cos(1))' is exactly zero, while the Android calculator does.
posted by the antecedent of that pronoun at 3:39 PM on February 16 [1 favorite]


Python 3 supports “infinite” precision integers... Came here to say "LISP bignums are the answer to this"

Y'all might find the article we're discussing interesting.
posted by Nelson at 3:50 PM on February 16 [4 favorites]


« Older Pesto the penguin seen canoodling with a friend   |   The Library of Congress' unique copy of James... Newer »


You are not currently logged in. Log in or create a new account to post comments.