I’ve spent a day wrestling with troublesome pennies, and it’s all the Institute of Electrical and Electronics Engineers’ fault. Bastards.
It’s that fiduciary time of year again for the company I co-direct. As Saturnalian delights begin to engulf the minds of others, we have to help our book-keeper and accountant prepare the end of year company accounts. It’s not a pleasant task. Customers do stupid things, like underpay or overpay, forget about VAT, demand small refunds and generally act the giddy goat against any prospect of an elegantly balanced set of books. When we started seven years ago, I programmed our company’s invoicing and accounting system. This has grown organically over the years, accreting some wonderfully powerful and tailored capabilities, but also a whole lot of silt. I’m not an accountant, and nor is my co-director. Some of our early decisions in the system’s design have required a lot of untangling, but the most insidious problem turned out to be one that neither of us could have predicted; a problem predicated on a lie told us by our maths teachers.
We’re taught much simplistic tosh at primary school. The silly model of the atom, with all those electrons swooshing round their perfect racetracks, is a famous example. Few A Level students quite shake off the faint stench of betrayal on being shown the messy quantum truth. RE lessons ply more insidious deceits, insinuating that there’s ample evidence for a historical Jesus, whatever one’s opinion of his divinity. The one subject where one might have hoped that subjective whimsy be absent is mathematics. Especially in our early years of arithmetic, we assume that we are being fed vital axioms, each pure, wholesome and incorrigible. All those little tools – carry the 1, move it into the tens column, divide this by that. So useful. So firm in a world of flimflam and fashion. So misleading.
We were taught that rounding up or down was a simple matter of deciding the number of decimal places one desired, and then, if the subsequent digit was five or larger, round the preceding number up. If the digit was between zero and four, round the preceding number down. This is how it was to be done. No arguments. No discussion. One Way to Round Them All. No doubt, had I stayed for A Level Stats, the lie would have been revealed. And a lie it is, one which has had material effect all these years later.
My company’s invoicing system does lots of real-time arithmetic. At the press of a button, for example, it calculates our VAT return with up-to-the-minute data. It lets us find out who owes us what at any moment. It shows what services are earning what income. All of these functions, of course, require mathematics. If one is calculating VAT on the fly, one will round the resulting number to two decimal places, which is all the decimal places that money deserves. To make things even easier, the database has a built in rounding function, so it can spit out subtotals, VAT included, without having to do anything fancy in the programming language that communicates with the database. For years, we have been using the system blithely, but over time, we have noticed anomalies creep in. Pennies here and there just didn’t seem to add up. Credit notes didn’t quite to balance out the full VAT-added invoices. Just by pennies, of course, across hundreds of invoices, so nothing particular to worry about. But pennies add up. Slowly but surely, we began to realise that, somewhere in the huge forest of code was some pernicious weed, tangling our data, its vines warping our results just enough to annoy our book keeper. What could it be? My co-director and I read all our code, examined our algorithms and refactored the more knotty areas. Noting seemed to help – the pennies just kept popping discrepantly into what should have been a balanced set of accounts. They constituted a little froth, eventually forming a scum on our financial books.
We pared our code to the bones, until the answer stared us in the face, astonishing in its simple depravity: both the programming language and the database were simply not rounding numbers consistently. Sometimes they rounded up, sometimes down. We did some research, not quite believing that something so fundamental could be so askew, and made the astounding discovery that what we had been taught in school as the incorrigible rule in rounding numbers was just a fashion. A fad. A debate. A whim.
The Institute of Electrical and Electronics Engineers, or IEEE, sets standards which programming languages and their ancillary libraries follow. Amidst their fiddling, prescribing and proscribing, they decided that our normal school-taught notions of rounding numbers are flawed: why should 5 always be rounded up? It sits in the middle, so why should it not, half the time, be rounded down? Spread the love! So, how does one achieve such ordinal egalitarianism? They decided that, if the number before the 5 is odd, then it shall round up. If the number before the 5 is even, then lo, it shall round down. So, according to the IEEE, 1.5 rounds to 2. And 2.5 also rounds to 2! And what the IEEE sows, our C libraries, perl sprintf functions and MySQL ROUND statements reap. Now, imagine how the IEEE’s capricious juggling affects VAT calculations and the like, which require consistency. Yes, statistically, the IEEE’s rounding produces a “fairer” distribution of roundees. For financial applications, however, such wobbling idiocy is useless. Tellingly, neither the programming language nor the database offered as an alternative the traditional and financially useful rounding as taught at school. We ended up having to hand-write our own rounding functions, and substitute them throughout our code and database query. This wilful lack of such a useful function built in is an interesting insight into standards bodies and the designers who slavishly follow them. In researching this problem, we found we were not the only ones affected by the little bombshell. People are constantly reinventing the wheel in trying to get rounding back to the comfortable certainties of their school days. Language designers and mathematicians have little sympathy. One online complainant received the following retort:
“I wonder whether some people are missing the point here. There is no law of nature which says that 0.5 must round up”.
Could someone please go back in time and tell that to my maths teachers?