UI is hell: four-function calculators
34 comments
·January 24, 2025kqr
persnickety
RPN is definitely easier to implement. I helped someone do that as a student project and while it was minimally complex, there were no edge cases with the operators.
You pay for that by having a stack rather than a small fixed number of variables.
_moof
My HP RPN calculator only has four positions available in its stack, which I imagine makes the implementation a bit simpler than a stack of arbitrary size.
persnickety
The typical 4-function calculator doesn't even allow multiple subtrees of computation, so I think it works out to having something like 2 entries on the stack.
tgv
Classical 4-func calcs are easier, I think. They don't even need a stack, just a place to store the previous value and the current input.
userbinator
Reading this article and the thought process behind it reminds me of this almost-10-year-old comment I made about how they actually work: https://news.ycombinator.com/item?id=9456444
Selecting an operator loads this value into the accumulator and makes room for the second operand to be typed in.
Here is where I think the first mistake was made; if you observe the effect of repeatedly pressing '=', that obviously does not clear any registers, but merely repeats the last operation.
But there’s more to the story: it’s actually a side effect of a little-known “K-constant” feature that retains one of the previously-entered operands and the operator, and lets you vary the other operand.
I haven't used any 4-function calculator that behaves like this; instead, I get (without clearing):
2 + 1 0 = 12
5 = 22
0 = 32
saurik
Apple's recent iOS Calculator update broke this entirely and now = multiple times does nothing :/.
oogetyboogety
those fuckers. If windows wants to add this as a success where Mac has failed, it still works in their calculator app.
gnatolf
This article makes it sound more complicated than it is, collecting all those edge cases. Many of them can be simply ignored, as in being no-op. Which is also what most cheap calculators do...
mazambazz
I, too, have been caught in the trap of trying to accommodate all possible user flows.
Sometimes you just have to put your foot down and just say "No, that is not how you use this tool".
jansan
Traditionally this would be solved by inserting a comment into the code that insults the user (for example the classic "The user is a wanker" comment [1]).
contravariant
I still wish to find the idiot who thought the reasonable thing to do when someone types 2 followed by SQRT is to return 2sqrt(
openrisk
> Let’s start with the basics: the simplest calculator has ten digit keys, a decimal dot, four arithmetic operators (+, -, ×, ÷), a result button (“=”), and a “C” key to reset state.
Mathematically there is already quite a lot happening here and in addition (pun) we use base-10 representation of numbers, which is not the simplest.
Might be interesting to explore if decomposing the problem into even simpler "sub-calculators" (e.g., starting with binary addition) and then putting everything back together as a sort of progressive enhancement would reveal the most logical or economical UI.
andrewfromx
"those cheap plastic rectangles hide decades of edge cases, quirks, and questionable design choices. Want to change a number's sign? That's a special case. Chain operations? Special case. Divide by pressing equals repeatedly? That's a feature called K-constant, except multiplication plays by different rules because... reasons. By the time you've handled every possible button combination, you'll wish you'd stuck to programming smart fridges"
labster
But the calculators last much longer than smart fridges because they don’t rely on central server existing to continue operating.
bathtub365
They fixed that by making solar calculators that rely on the continued existence of our temporary sun.
TeMPOraL
I thought those solar panels are fake decorative elements only; the calculator still needs a battery to work and stops working when it runs out?
bawolff
I always found percent key behaviour to be such a bizarre choice.
theamk
When I was a student, I thought the same.
Later when I designed some expression evaluators I've started to appreciate the language design (and hacks) required to make "100 - 5%" work "as expected" (that is, return 95). It's a pretty unusual grammar, and it is neat that it was implemented on something with only few dozens bytes of RAM.
kazinator
Google's calculator on Android is an absolute shitshow. Sorry for omitting details, but this issue is easy for anyone to repro without any specific steps.
troupo
Oh, it doesn't even get to the juicy bit of percentages.
Can't remember how physical calculators deal with them, but software calculators deal with them differently.
To the point that iOS calc and MacOS calc are different. And variations of Windows calc (regular/scientific/engineering) are different
quesomaster9000
TL;DR PEDMAS is bullshit
If you look at the internal state of the typical physical calculate it's a beautifully simple machine that tries to catch the balance between RPN and logic... except it has stupid nonsensical human rules interjected.
So, every time this article where points out "actually it's more complicated than it seems" is where historically somebody has made deliberate design decisions to do it 'the dumb way' even though it adds complexity, to force something unnaturally.
And what you end up with is something that's half intuitive, it's in between the two systems, but it's more complicated than it needs to be.
Please, I would much prefer device for RPN calculation, that follows consistent logic, rather than silly imposed rules.
4+7*3 etc.
or 4 7 + 3 * =
Where the latter is much closer to what we do in our heads and conceptually think about it.
dlcarrier
My grandma, born in the 30's, isn't any good at computers or technology in general, and has trouble operating even the simplest TV remotes and cordless phones.
One thing she can operate is her HP-12C calculator. At the time of it's initial release, someone taught her how to use it, knowing that the RPN interface would be much easier to teach her than infix notation that was equally popular at the time. She never figured out infix notation, at least for more than single operations.
Pretty much everything uses infix notation now a days, so that's what everyone learns first, making RPN an additional skill that usually gets passed by.
It's interesting to learn that there was a time when a calculators interface was a toss-up, and some people learned RPN because it was the easiest to learn, as opposed to it now being an additional skill that would conflict with earlier experience.
kazinator
Infix isn't "nowadays". It's been with us for hundreds of years. Moreover infix constructs occur in natural languages. You know "apples and oranges".
Someone born in 1930s would have learned infix arithmetic in school just like somebody born in the 2010s.
Cannot work a remote, but can use RPN on an HP calculator? That sounds AI generated.
dlcarrier
It's nowadays that everyone learns infix calculator input methods as children. In the 40's they sure weren't learning any calculator user interfaces.
It's stateful and modal interfaces that are an extra step she never figured out, and some TV remotes require different modes for different functions, especially for different equipment. CEC has solved this on modern TVs, but it used to be common to have control the VCR/DVD player by selecting a mode, then control the TV by selecting another mode, and even without the other equipment, she could accidentally get the remote in the wrong mode.
She never got the hang of the states that are possible in a calculator with an interface using infix notation, so instead of using parentheses or order of operation, if she didn't have access to an RPN calculator, she'd write down results and enter them back as needed, to only have one operation in progress at a time, in the calculator.
ziml77
I don't see how PEMDAS is relevant for a 4 function calculator. The implicit equals when you press an operator means that they always work sequentially rather than jumping around.
airstrike
I think they're saying it's bullshit because if we used (the superior) RPN across the board, we wouldn't need PEMDAS at all
ziddoap
>RPN
"Reverse Polish notation", I think, for people like me who aren't familiar with the acronym.
nejsjsjsbsb
You thank!
x3n0ph3n3
RPN requires implementing a stack on silicon, which adds even more complexity and uncertainty. How deep can the stack go? What happens to your calculation when you exceed its capacity?
Pinus
Classic HP calculators used a fixed stack of four registers. In practice, you can do quite complicated things before you blow that up. Especially if you understand how the mechanism works, and plan ahead a bit.
Besides, infix-style calculators also seem to use a stack to handle operator precedence — if you squint a bit at the manual, you can usually see the shunting-yard algorithm at work behind the curtains.
Actually, some really early HP:s (only desktops, though, like the 9100) used a three-level “stack”, which I put in quotes, because it relied on the user to shift the registers up and down as needed. But they cheated a bit and displayed all three registers, so that the user could see what was where.
kazinator
Compared to what? RPN allows for complex, nested expressions for which infix requires nested parentheses. Nesting requires a stack, the same way.
I realised reading this that, even though I used to be quite adept with these common four-function calculators, I have since forgotten most of their operation. The past ten years or so I have exclusively been using RPN calculators – I find them so much easier to work with, and going by this article, possibly even easier to implement sensibly?
(My main drivers are Emacs Calc on the computer and RealCalc on Android devices. For kitchen work I use a slide rule, which is something I think more people should do! There is nothing better for translating proportions.)