Thursday, April 25, 2019

Humble Pi


Recently I happened upon a YouTube video by Matt Parker called What Happens When Maths Goes Wrong? (https://www.youtube.com/watch?v=6JwEYamjXpA). I’ve listened to a number of his videos over the years on channels like standupmaths and Numberphile, so I knew I was in for an entertaining hour. Matt is a mathematician from the UK. This particular video was based on a book he recently wrote called Humble Pi (available on amazon.com), and is subtitled A Comedy of Maths Errors. (Note that “Maths” is the appropriate word in British English for what we would call “Math” here in the US.)

I knew even before finishing watching the video that I needed to buy the book. It came this week and I’ve absolutely enjoyed reading it. Since I have a degree in computer science from the college of engineering and I also have a minor in math, I have all the background necessary to understand many of the nuances in his entertaining examples, but even if you do not, you would still enjoy it.

I’m not going to be a spoiler, but I’d like to give a few examples from my own experience that illustrate some of the points in the book. So, without delay, here goes.


Y2K

When describing the coming problems with Y2K38, Matt makes a passing reference to the Y2K problem when he says, “Through a massive effort almost everything was updated… It’s risky to be complacent because Y2K was handled so well.” But as one of those who spent a couple of years of my professional life being part of that “massive effort”. I’d like to recount a few real examples of the kinds of math errors that we had to correct.

Error Checking – In an effort to try and detect errors in incoming data, one program I encountered checked to see if any dates were for years before 1940 as that was the year the company started business. But with only 2-digit years that was coded IF YEAR < 40. When the year rolled over from 99 to 00 that would have rejected all the good data as well. It was a well-intended reasonability check, but with unintended consequences after Y2K.

Negative Charges – One system measured flow rate between two times and used the product of the two to calculate how much product to be charged for. But that presumed that the difference between the starting time and ending time would be a positive number. When the clocks we “backward” from year 99 to year 00 the time difference was negative and thus the system calculated a negative amount in the multiplication and gave a product credit instead of a charge.

Early/Late Errors – It’s easy to assume that all the Y2K errors happened during the actual rollover of the year at midnight on 31 December 1999. But sometime we actually perform mathematical calculations on dates make it happen earlier or later. One such example is that invoices from many companies give a discount for quick payment or a late charge for late payments. Such a system might give a 2% surcharge for any payments after 30 days. So if an invoice was dated on say 15 December [19]99, they would add 30 days to get the late payment date, coming up with 14 January [20]00. But with a 2-digit year, the result would be that a payment on say, 25 December, would look like it was 99 years too late. There are similar errors in any look-back calculations.


Pre-test v. Post-test

Higher-level computer languages have one or more constructs that enable the repetitive execution of a block of code. This block of code is repeated until some condition is met. There are two ways of checking this condition – before the block is executed the first time (called a pre-test) of after the block has been executed at least once (called a post-test). For a good explanation see the “While loop” in Wikipedia (*1) and the “Do while loop” (*2). Each of these constructs is valuable, however, some computer languages only have one of them, or if they have both the syntax may be so similar as to be confusing. Consider the examples in Wikipedia:

            While (A = TRUE) Do B End While
                                    v.
            Do B While (A) End While

Errors can be introduced into a program in at least a couple of ways. First, if a programmer is used to one language and its syntax, he/she may inadvertently choose the wrong type of pre/post-test if moving to another language. Secondly, the test may be coded in a way that an Off-By-One-Error (an OBOE as Matt calls it) may occur. As an example, consider the following:

            I=1; While (I < 5) Do B; End While;
                                    v.
            I=1; Do B While (I < 5); End While;

            B: Print “Hello World”; I=I+1;

The first construct prints “Hello World” 4 times, while the second construct prints it 5 times.


Conclusion

The above few examples are just a few from the world of programming, but the book contains many, many examples from architecture, engineering, finance, and other fields. All the examples have a common thread in that the introduction of a “maths error” caused either abject failure or at least an unintended consequence. Some of these failures resulted in death, so they are not just abstract textbook examples. I heartily recommend this book.


Notes:



No comments:

Post a Comment