# Calculating Percentages

I have a strange relationship with my calculator. A lot of people who haven’t met many mathematicians assume when they meet me that my status as a mathematician qualifies me as a super-brain who would never resort to working something out using a calculator – once, on being introduced to someone who’d had a few drinks, I was greeted with “Oh, so you’re a mathematician, eh? Then what’s the answer to [mathematical calculation]?” – missing entirely what maths is actually all about – and also thankfully having drunk enough not to be able to come up with a question that was actually difficult. (It turns out, just because numbers are big, it doesn’t mean they’re hard to multiply together, and seven times a million is just seven million).

Being a mathematician is obviously not just all about adding and multiplying numbers. As I hope I’ve been getting across in this column, maths is a rich and beautiful subject full of abstraction and variety, and involves manipulating complicated concepts in your mind to appreciate their beauty. However, it’s unavoidable that mathematicians do sometimes have to do calculations. Occasionally, it’ll be part of work – but most of the time it’s for one of the many mundane reasons that any person has to do a calculation, like working out how much of something you need, or adding up the bill in a restaurant and including a tip. Quite often in these situations, if I want to make sure a small mistake doesn’t result in me being short-changed (or short-changing someone else), I’ll whip out a calculator to save time and effort.

It was on such an occasion recently that I discovered an interesting quirk of how the calculator I was using (the calculator app, on my Android phone) works. I needed to work out the total of my food, plus a 10% service charge – so I added together my main, side and drink, and then calculated that total with 10% added. Normally, I’d do this by typing:

(Here multiplying by 1.1 gives me 110% of the original value, so I’ve added 10%). But I noticed that my friend nearby, who was undertaking a similar process, was using the % key on the calculator. As someone used to converting in my head between percentages and decimals, I’d never really used that key much – but it turns out, Android interprets it in a very natural way. You can type:

And it’ll give the same calculation. What’s more interesting is, you can do this without the brackets, and it’ll still apply it to the whole thing:

As someone who’s involved in both mathematics and maths teaching, this grated on me a little. I appreciate that this is how the average calculator user might want this to work – there aren’t many situations where you would specifically want the percentage only to be applied to the last entry in the sum, so it makes sense that doing this would work in this way. But in my mind, the operation ‘add 10%’ is interchangeable with ‘multiply by 1.1’ – and under the standard operator precedence rules, the multiplication should be calculated before the addition. Indeed,

gives a different answer. I can force the calculator to work it out this way – for instance,

will do the thing I would expect it to do. But it’s intriguing that the calculator takes a natural, but slightly non-mathematical approach here. Since I can get a rough idea of what the answer should be in my head (the total is going to be around 18-19, so adding a 10% tip should make it around or slightly more than 20, since that’ll be around 1.8-1.9), I can usually tell which of these things the calculator has done, but someone relying solely on their calculator to give them the answer is unlikely to notice.

It hasn’t always been this way – stock apps like the calculator are updated all the time, and with new versions of the operating system they can change their behaviour. Previous versions of Android included a calculator app which would interpret ‘10%’ just to mean 0.1 (which is precisely what it means, and much closer to my mathematical interpretation of the calculation). So you’d get

It’s taken the sum of the three numbers, then added 0.1. Unhelpful! Under this interpretation of what % means, to work out my bill I could just multiply by 110%, which the calculator would treat as multiplying by 1.1 as I’d usually do. But would it make sense to someone who doesn’t think of it that way?

#### Problems with calculating

Writing a calculator app can be a big challenge. Phones can perform incredible feats of processing – streaming video, running games and presenting huge complex websites, coordinating multiple input and output devices, all on something that fits in your pocket. So you’d think that to make something that you operate by pressing buttons on a screen, which output the answer to a mathematical calculation – something computers do all the time – would be trivial.

But calculators have always been an interesting challenge for programmers. The way computers store and process numbers often causes hiccups and issues with doing precise calculations. For instance, numbers are stored to a particular degree of precision – a fixed number of total decimal places. Called ‘floating-point arithmetic’, it involves storing a fixed number of digits, as well as recording the position of the decimal point (using a power of the base you’re calculating in, just like the standard scientific form of [number] × 10^{[power]}). So if you’re calculating with very big, or very precise values, sometimes rounding occurs, and can cause errors.

Another issue is with certain numbers which behave differently in binary. Some numbers in decimal can’t be stored using a finite string – for example, 2/3 = 0.666666 (recurring forever) – so it’ll be stored by a calculator with a finite memory as 0.666666667 (for a given size of memory). When computers convert numbers to binary, they can still store non-whole numbers – for example, ½ in binary is 0.1. The columns which are numbered 1,2,4,8… going off to the left are just 2^{0}, 2^{1}, 2^{2}, 2^{3}… – so the columns off to the right are 2^{-1}, 2^{-2}, 2^{-3} and so on, giving ½, 1/2^{2}, 1/2^{3}… In this way, ¼ is 0.01 and ⅛ is 0.001.

This means that certain numbers can’t be stored in binary using a finite string of digits either – and it’s a different set of numbers than it is in decimal. For example, 0.34 has a finite decimal expansion, but in binary it’s 0.0[10101110000101000111], where the section in brackets repeats forever. So this number, along with infinitely many others, can’t be stored exactly in binary, meaning errors can creep in if not handled carefully.

But as well as all these technical challenges, designers of calculator apps also have to consider the way people will use the calculators. Given a concept like the percentage button, there are any number of ways to interpret what the user could mean when they use it. This seems strange, since it’s a specific mathematical concept, and ideas in maths are usually well-defined – but it’s also a concept in general use, so it needs to work the way most normal people (one way to define non-mathematicians) expect it to.

Another popular type of calculator people carry around in their pockets is the iPhone – and the built-in calculator there actually behaves differently. If you type in a calculation like 12.50+2.95+3.5+10%, as soon as you press the percent symbol, it’ll calculate and display the percentage – in this case, 10% of (12.50+2.95+3.5). If you then proceed to press equals, it’ll add this to the total, and show you the correct value.

This is equivalent to what the Android calculator does – it’s still giving you the answer you need so you can hand the server the correct amount of money – but it’s at least acknowledging that there’s an intermediate step, so you can see a bit of what’s going on.

Perhaps this strange percentage handling is a function of calculators designed for general use. Scientific calculators, made for engineers and mathematicians to do precise and complex calculations, handle the percentage key very differently. I’ve been playing with the scientific calculator I used for my school exams, and it gives some very strange answers when I try to add a percentage – multiplying by one is fine, as you would expect, but it turns out that even different calculator manufacturers have implemented wildly differing interpretations of what ‘18.95 + 10%’ means, and it took me a few minutes to work out what my Casio was even doing.

I suppose the real lessons here are, 1. Don’t take your scientific calculator to a restaurant, 2. Make sure you know what you’re asking a calculator to do so you can check it’s doing the right thing, and 3. Always tip your server (the right amount).