UI is hell: four-function calculators
138 comments
·January 24, 2025gf000
dhosek
When I used to teach math, the difference in how calculators handled precedence was always a problem. I would tell students to enter 1+2×3 into their calculators and if they got 9,¹ they should throw away the calculator and buy one that worked properly. Not respecting the precedence rules is bad design.
The calculator built into Windows (at least back then, maybe they’ve changed it), gave 9 to my test instead of 7.
⸻
1. If they got some number other than 9 or 7, I recommended dousing it in holy water, and then gasoline, burning it, encasing the ashes in concrete and dropping that concrete block into the deepest part of the ocean because that calculator is cursed.
kweingar
Not respecting precedence is ok, as long as the user is aware of it.
When I use a calculator, I don't necessarily care if I can punch in a written expression verbatim from left to right. I just want to do a calculation.
dhosek
Except that if you’re a high school student who is using a calculator for stuff most if not all of the readers on this site would do in your head, having a calculator that ignores precedence just makes things that much harder.
gf000
These "four-function" calculators only display the last number. It would be widely unexpected if doing x3 when all I see is a 2 would sometimes give 6 or some other value (e.g. the student next to me just passed a calculator with a value displayed, so I don't know if it had any previous instructions and no way to find out)
The UX is pretty clear - it's ANS operator newNumber. That's it, if not somehow displayed, then it shouldn't have a more complicated model inside either.
It can freely do "normal" math the moment they write out the whole expression tree, as the result of an expression is the one calculated by precedence rules. The result of a two-operand operation is not that, though.
evalstate
just tested this. in "standard" mode you get 7, switch to "scientific" you get 9.
ATMLOTTOBEER
You’re right it was terrible for a while. I’m glad they fixed it.
Suppafly
>Due to how it looked I assumed it works as a standard four-function calculator, but it was actually storing the whole expression in memory and executing it as per the standard precedence rules.
Isn't that how even fairly cheap 'real' calculators do it now though or do really cheap ones really just do one operation at a time from left to right?
SoftTalker
I would have assumed that cheap (i.e. 4-function, LCD) calculators today have the same internals as cheap calculators from the 1980s. Why reinvent what works, when you can just keep selling it.
Someone
Why would you assume iOS, not being the cheap option for a smartphone, would choose to emulate a cheap calculator?
There were non-cheap calculators in the 1980s, too, that did proper evaluation. The ones I remember had keys for parentheses, though, allowing you to enter, for example
(3 + 4) * 7 =
to get 49.For an example see the TI-25 at http://www.datamath.org/Sci/Slimline/TI-25.htm
the-grump
One of first things I do when I set up a new iThing is to delete the calculator app and install a third-party calculator.
ghaff
I've been RPN for so long that I pretty much need an RPN calculator app even for relatively simple stuff.
kstrauser
I didn’t start using RPN until many years after graduating college, but took to it like a fish to water. I never again have to guess what the calculator might be doing behind the scenes. It’s doing exactly what I told it, no more or less, in precisely the order I told it.
airstrike
this is me too. I literally can't use a regular calculator anymore and I'm not even doing fancy math. my brain was just permanently rewired by the HP-12c
ChrisMarshallNY
I’ve been using PCalc, since last century (it does have an RPN mode).
kqr
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.)
7thaccount
I own several RPN calculators (they are very cool), but still find modern TI or Casio calculators to be superior now when I'm not using a spreadsheet or something like Mathematica.
Even 15 years ago, you could type out an entire equation that would have to be used multiple times and just update a single part of it and hit enter and then scroll up, modify, and hit enter again. All of that without having to resort to keystroke programming.
RPN is easier to reason about, but in aggregate doesn't save much time when you have a really slick modern interface and can scroll around and see things pretty printed. In engineering it is common to have some crazy looking things that are just easier to reason about when you can see it matches what is on the paper in front of you.
kstrauser
I have an HP 50g that makes it easy to flip between RPN and textbook-style graphical equations where you can arrow around and tweak any value. You can have both approaches in the same place.
7thaccount
Agreed, but the 50g is ancient and is the only calculator to my knowledge that has that capability.
For the most part the options are an older style RPN calculator (even the super nice swiss micros don't have equation scroll, although you can at least see the contents of the stack on the screen like the later HP calcs could), or what TI and Casio have.
bruce343434
> things [] are just easier to reason about when you can see it matches what is on the paper in front of you.
I have this sentiment every time I dive into some "other" programming math notation. Such as (lisp), rpn, infix without precedence, etc. In theory I like it, but then in practice it's just kind of painful.
We humans are entrenched in our bad but default notation!
roelschroeven
Is infix notation even that bad?
I guess RPN has advantages for use as an input method for calculators, but as an actual algebraic notation? It seems to me it's very cumbersome to perform standard algebra on equations in RPN notation (or prefix notation, for that matter).
I can't be sure but as far as I know nobody has using anything other than infix for algebra. Every time I see a text explaining the advantages of RPN, it's always in the context of calculator input.
benhurmarcel
I’ve been thinking that an RPN calculator app could easily display the full natural expression while you enter it in RPN. And let you modify it afterwards.
I haven’t found such an interface yet.
billmcneale
I used to be in love with RPN and owned multiple HP calculators (HP-41, HP-15) but RPN is cheating by offloading effort on the user.
When you see a complicated, parenthesized expression, it's up to you to figure out the deepest expression, enter it first, and then work your way out.
Standard calculators require more keystrokes but they can be entered without much thought, left to right. And these days, you can also edit and modify your input, so it's hard to justify RPN.
kccqzy
Why would you need to enter the deepest expression first?
kibwen
Agreed, my spicy take is operator precedence was a mistake that we regrettably inherited from mathematical notation. Parentheses for grouping are fine, at least.
PeterStuer
In my youth my treasure was a HP41cv. I always found RPN far more logical, conceptually cleaner and as a result far simpler to use.
7thaccount
If you want a recreation:
https://www.swissmicros.com/product/dm41x
They also have their own versions of the 15c and so on. I have 3 different models (and one original HP RPN calc) and can confirm they are really high quality. Again though, spreadsheets are a lot more useful to me when I have more than just a simple calculation or two. I think these are mostly getting picked up by hobbyists, collectors, and those who work in labs and need a dedicated device with physical buttons.
Note that it doesn't have the big expansion pack things that I think the 41c came with if I'm thinking about the right calculator.
DarkSucker
That link made my day! Thank you. From what I read, their builds perform better than the originals, and they even make landscape versions. Woo hoo! RPN rules in my book. I picked up an HP 32 before they were gone forever, but I've been afraid to use it. It's great to know about SwissMicros; they'll get my business soon.
ghaff
I've been really tempted as my 41CV doesn't work any longer but I do real calculating so little that it would just be a nostalgia trip. (And I have a couple of the horizontal format HP calcs that still work for any of the calculator stuff I do sometimes do outside of an app.)
genewitch
OK I always see this sentiment.
Can someone show something useful in RPN? House odds on 3 on roulette, or don't come odds for 10; or the standard deviation of 1, 1, 1, 1, 7, 29.
I have never seen anyone show an example more complex than 2 2 12 + * or whatever it looks like.
koblas
Plus, for owners of the HP41, if you had an essay question in your chemistry test you could type in cliff notes... Naw, I never did that.
PeterStuer
I did do that! But data entry was so laborious that by the time you entered the formulas you basically knew them by hard.
layer8
Yes, RPN is easier to implement, and for the same reason also easier to reason about as a user,
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.
enriquto
> You pay for that by having a stack rather than a small fixed number of variables.
you can easily add variables to your rpn calculator. For example ">x" pops the top of the stack into the variable x, and "<x" pushes the value of x to the stack.
You can also interpret parentheses as whitespace to enable users to group parts of the computation (but this may become confusing when they write nonsensical parentheses).
ropejumper
They meant that the implementation of a four-function calculator only needs a few fixed variables rather than a stack.
_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.
taeric
I'm super intrigued on the slide rule idea! Where did you get one and what sort of stuff do you typically do with it?
alexjm
I have a kitchen slide that I use for ratios in recipes. It's an old plastic one from Think Geek.
For example, I usually put 15 grams of coffee with 8 oz of water (please excuse the mixed units). To make a different amount, I align the 1.5 on the top rule with the 8 on the bottom rule to set the ratio. Then each number on the top rule (coffee in grams) matches the scaled value on the bottom rule (water in oz). The 6 on the bottom rule aligns with ~1.1 on the top, meaning I should brew my little six-ounce cup with 11g of coffee. In practice, I do this a lot with bread, but the "baker's percent" convention for writing bread recipes makes it a more complicated example.
Another way to use a kitchen slide rule is when scaling a recipe. Say I want to make 2/3 of a batch of cookies. I line up the 3 on top with the 2 on the bottom. Then for each ingredient, I find the recipe's quantity on top, and read off the scaled quantity on the bottom. This works better with recipes that use weights, to avoid awkward fractions or converting between units so you can subdivide.
Kim_Bruning
Do you also use dc? (can be used on terminal / termux)
kqr
Yes, but mainly in scripts and oneliners (never learned the Bash arithmetic substitution thing). For interactive use I often have a shorter time-to-arrival to either Emacs or RealCalc.
carb
If anyone else was curious about the final image and the `x÷` button: http://www.vintagecalculators.com/html/sharp_el-8.html
----
> The keyboard on this calculator has the number of keys reduced to the minimum by the use of only three function keys, including a combined "x÷" key.
> Here, pressing "x÷" gives the multiplication function if "+=" is subsequently pressed to give the answer, and gives the division function if "-=" is subsequently pressed to give the answer, so:
> 4 x÷ 2 += gives the answer 8.
> 4 x÷ 2 -= gives the answer 2.
thih9
Thank you! This looks surprisingly intuitive, perhaps even comfortable, especially when muscle memory kicks in.
Then again…
I guess 4 += 1 += gives the answer 5.
What would be the result of 4 += 1 -= ?
Edit: the answer is “3”, elaborated on the linked page: http://www.vintagecalculators.com/html/calculator_keyboard_l...
humptybumpty
But what about:
4 -= 1 +=
4 -= 1 -=
I’m guessing 3 and 3?
thih9
My guess is different; these calculators did support negative numbers; I’d say:
4 –= 1 += gives: -3
4 –= 1 –= gives: -5
Manual for a similar model: https://www.oldcalculatormuseum.com/m-sharpqt8d.pdf
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 :/.
wruza
iOS calc sometimes registers a press but not a release(?) and a key gets stuck in "pressed" state and doesn't count as an input. Many years issue which renders it completely unusable, unless you're careful with your tapping. A new level of skeuomorphism, I guess, non-physical buttons that get stuck.
saagarjha
This is fixed in the latest beta, which is probably going to ship next week or thereabouts.
anarticle
The AC to backspace change really screwed me up and I'm back on a third party calculator. It's a speed bump that feels like hitting a brick wall every time!
oogetyboogety
those fuckers. If windows wants to add this as a success where Mac has failed, it still works in their calculator app.
actionfromafar
Windows also innovated from TTC to TTCC! (Time to Candy Crush, instead of Time to Crate.)
serviceberry
> I haven't used any 4-function calculator that behaves like this; instead, I get (without clearing):
Then you haven't used very many calculators: https://tedmuller.us/Math/Calculator-1'Introduction.htm
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]).
BlueTemplar
The site linked in comments is gold.
> Shit. I was just about to launch into an explanation of our code review procedures. Every week we sit around a table and carefully and dispassionately analyse and constructively criticise each others code. And it works. We sit there and listen and take it all in. It works really well and team morale is excellent. One day we're even planning to do one when we're sober.
mrbombastic
I think there is a lesson here. Don’t let edge cases define your complexity whenever possible. Check if it is in fits your valid expressions and if it doesn’t do nothing, otherwise you get a mess of if x then y
userbinator
Those "edge cases" actually aren't; they're simply "emergent behaviour" of how the state machine in the ASIC works.
the__alchemist
You don't need an ASIC in the way the article's challenge described the lunch problem. Do it with an MCU. The article goes into how the author did it at the register level, but that is not how a modern audience would do it. If you're doing registers, it's probably for your MCU's CORDIC peripheral etc for trig!
You can even go fancy with floating point if you want (Probably elims the $0.30 MCUs). Tiny, cheap, and you can model it using appropriately expressive data structures (like rust enums) that handle the edge cases at the CPU level.
I don't mean to dismiss your comment; it's important. I think this article is skirting a gray area of which constraints are applied. Then the conclusion is altered. Is it saying making a 4-function calculator is complicated!, or is it saying making a 4-function calculator is complicated if you add a number of specific restrictions and requirements (No modern CPU code, exact behavior replication to all combinations of user input etc). The latter is less interesting.
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.
phreack
To this day I can not remember which is which in calcs with clearing buttons. I know probably one of them clears the operator and the other clears the whole operation, but since I'll never figure out the correct one I just start pressing everything when I want to start over and hope.
genewitch
CE is clear entry to my brain, regardless of the actual etymology.
So 7 + 1 * 5 ÷ 0 CE 4 = 10 And 7 + 1 * 5 ÷ 0 C 4 = 4
bowsamic
I'm not joking when I say I'm 30 and I still don't understand how to use a calculator without a proper display where you see the entire expression to be evaluated. At school in the UK we all had calculators where entire expressions could be typed in including proper fractions and parentheses. I find the more basic kind of digital calculators, including built-in calculator apps, to have totally impenetrable user interfaces
gushogg-blake
I think calculators should have "into" and "from" so you can do division and subtraction on the previous result without retyping it. I made a demo: https://fluent-calc.vercel.app/
Also supports saving a result into a variable for later reference, and x "as proportion of" y, which is just an alias for division.
beeflet
Maybe you could do the same thing by having a negation and reciprocal button?
layer8
Yes, I use those all the time for exactly that purpose. In many cases you don't even need to worry in which order to enter the operands when calculating a difference or ratio, just press negate or reciprocal if the result doesn't look right.
gushogg-blake
Yeah, having dedicated operators for going the other way just fit my mental model better for the types of tasks I had in mind for it.
beeflet
Hmm, well then maybe it would be simpler to have a single button that swaps the input and the accumulator?
into and from may be easier for the average joe to read though
manchmalscott
This is why every calculator ever should have a RPN mode. Postfix notation removes all of that parsing ambiguity.
I ended up making my own RPN calculator in C++/Dear ImGui because I wasn’t happy with any of the options for desktop Linux and, implementation wise, RPN is dead simple. Grab values off the stack, apply operator, push back onto stack.
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
rbanffy
I remember fondly having figured out how to implement precedence with two stacks, one for operations and other for operands, the same way my Texas Instruments calculator did.
It is possible to implement a desktop accessory calculator in a lunch break, but only because you get a whole lot of abstraction done by the GUI environment. Modelling the UI as a state machine in this case is essential so you don’t get crazy with all the corner cases.
Just putting it out here - before the recentish redesign of ios's default calculator, it had a widely non-intuitive behavior. Due to how it looked I assumed it works as a standard four-function calculator, but it was actually storing the whole expression in memory and executing it as per the standard precedence rules.
I do usually prefer the latter behavior, but without it being displayed as in more advanced calculators (it has been changed to show it) it's just not what I would expect at all, resulting in wrong calculations. But maybe it was just a "me-problem".