# You're reading: Posts Tagged: calculator

### Now I’m calculating with constructive reals!

A while ago I made myself a calculator. I don’t know if anyone else uses it, but for the particular way I like doing calculations, it’s been really good. You’d think that if a calculator does anything, it should perform calculations correctly. But all calculators get things wrong sometimes! This is the story of how I made my calculator a bit more correct, using constructive real arithmetic.

One thing you need to think about when making a calculator is precision. How precise do the answers need to be? Is it OK to do rounding? If you do round, then it’s possible that errors accumulate as you compose operations.

I’ve always wanted to make a calculator that gives exactly correct answers. This isn’t strictly possible: there are more real numbers than a finite number of bits of memory can represent, or a digital display can show, no matter how you encode them. But I’m not going to use every real number, so I’ll be happy with just being correct on the numbers I’m likely to encounter.

### I’ve made myself a calculator

For the past couple of weeks at work, I’ve been checking through our computer-based assessments before the students have a go at them. That means I’ve had to do lots and lots of calculations by hand, to confirm the computer’s got the right answer. Well, not quite by hand – I use a calculator for the stuff that I can’t keep in my head.

I’ve got a calculator app called RealCalc Plus on my phone, which I highly recommend. The main thing it’s got going for it is its RPN mode. While most calculators ask you to type in expressions pretty much as you’d see them on a page, read from left to right with numbers separated by operators and brackets, reverse Polish notation looks like this:

1 5 √ + 2 ÷

That computes $\phi = \frac{1+\sqrt{5}}{2}$. You put the numbers in first, and then say what to do with them. This might look obtuse, but it can make long expressions much easier to type in. Because there are no brackets, you don’t need to remember to close them. And if the same fragment appears more than once in the expression, you can just duplicate it on the stack, instead of typing it all out again. I’ve been using an RPN calculator for a few years now, and I get a warm mathmo feeling when I think about all the time it’s saved me.

But there are a couple of things that regularly trip me up. Once you’ve performed a calculation, you can see the result but you can’t see how you got there, so when you’ve got a couple of long numbers sitting on the stack and you can’t remember which is which, you just have to start again. And if you want to repeat a calculation but with a slightly different starting value, you’ve got no choice other than to type it all in again.

So, about halfway through my marathon of testing, with thumbs sore from tapping calculator buttons, I decided there was nothing for it except to make my own calculator. It would mainly be like RealCalc, but do something to solve those two problems. I headed straight to my favourite gaudily-decorated coding environment, Glitch.com, and set to work.

In this case, I think the day or so I spent making the calculator was worthwhile. I’ve been using the new calculator to finish off my testing duties, and it feels much better to use: I can do calculations quicker, and I lose track of what I’m doing less often.

The first change I made was to display not just the results of calculations, but how they were obtained. When you press 1 2 +, a box is pushed to the stack with a 3 at the bottom, but also the 1 and 2 and a + symbol above it. This works for nested operations, too, but the ingredients are hidden by default until you tap the box. There’s an ‘undo’ button, which throws away an operation and puts the operands back on the stack – very handy when you tap the wrong operator!

Numbers that you type in directly are shown in blue. You can tap any blue number and enter a new value, and any operations it feeds into are recalculated. I’ve used that quite a few times to set up a formula, such as the quadratic equation, and each time I used it I just need to change the input values and the result appears at the bottom.

It’s also useful for factorising numbers: I can start by typing something like 10199 3 ÷, and I just replace the 3 with different prime numbers until the result is an integer.

After a while, I realised that there was another optimisation to be made: sometimes a formula uses the same variable more than once! For example, in the quadratic formula, $b$ appears twice:

$x = \frac{ -b \pm \sqrt{b^2 – 4ac}}{2a}$

It would be nice if you could use the same number in more than one place, and have every instance update when you change the value of one of them. Every RPN calculator has a ‘copy’ button, to push another copy of the last item on the stack, so I just decided to make copies remember they’re linked. You can move copies of a number around, but when you overwrite one of them, all the others take the new value too.

In order for this to not make your head explode, I needed a way of showing which number boxes are linked together in this way. Asking the user for a name felt like overkill, and would interrupt your flow unnecessarily if you’re not planning on using the overwriting feature.

I came up with a nice solution: when you copy a number, it’s assigned an emoji. All copies of the number have the emoji stuck onto them, so you can quickly tell who they are.

This was way more useful than I expected! While I was testing a horrible question involving cancelling fractions with very large denominators, the linking feature really came into its own. By setting up $n/p$ and $d/p$, I could quickly find common factors of $n$ and $d$ just by changing the value of $p$ until both divisions produced integers. Bonza!

I’m quite happy with my calculator and I haven’t felt the need to go back to RealCalc yet. Hooray!

You can use it too: go to nice-calculator.glitch.me. If you open it on your phone, you can add it to your home screen and it should act like a normal app, instead of a web page.

### Maths Object: Correntator

Here’s another one of my favourite maths objects: the Correntator. It’s a simple mechanical tool to add up amounts of money. I bought it for about a tenner (new money) at a market.

This video is extremely shonky. Blame my phone, which can’t bring itself to record for more than 250 seconds at a time.

### Review: CALX, a calculator for iPhone

Reader Danial Clelland wrote in to tell us about his new calculator app for iPhone, CALX.

None of us owns an iPhone, but I borrowed someone else’s for a while and had a brief look at the app.

### App review: Tydlig, a calculator app for iOS

You may remember that we previously posted about Tydlig, a new calculator app for iOS. We asked if anyone would be interested in writing a review, and Aoife, who’s written the article below, kindly obliged.

Tydlig is a reimagined calculator on iOS and provides an innovative, freeform canvas where multiple calculations can be built and organised in one space. It functions as a scientific calculator, but on an open workspace that you can control, with additional visual features. Elegant in its simplicity, Tydlig captures all of the necessary components of a calculator, while maintaining refined and intelligible functionality.

### Babbage’s difference engine is really, really pretty

Hands up if you knew there was a working replica of Babbage’s difference engine in California.

(My hand is not up.)

This glorious machine lives in the Computer History Museum in Mountain View, California. A company called xRez Studio, which specialises in taking extremely high resolution gigapixel photos of things, has taken some extremely high resolution gigapixel photos of the difference engine. They’re so lovely that it feels wrong to be looking at them at work.

The photos are only avaiable on xRez Studio’s website, or as part of the Babbage exhibit at the Computer History Museum, but xRez has also released a video of the machine in action, which we can embed here.