Datareign

At one time, there was a widely held belief that all programmers were mathematicians. Of course, we know that this ”ain't necessarily so”. However, some understanding of math is a valuable tool for any programmer.

A Problem With Math

It is probably useful to define what we mean by the term “mathemetics”. Let's start with how Wikipedia defines it:

Did that help or did you find your eyes crossing and your head aching? If your answer is “yes”, then you're in very good company.

Before going any further, we're going to make an arbitrary delineation between arithmetic and the rest of mathematics. Most people appear to have an instinctive grasp of numbers and how to manipulate them. The operations of adding, subtracting, multiplying and dividing are so widely used that we can reasonably describe basic numeracy as a universal skill. When we refer to math then, we are discussing what might properly be called symbolic mathematics, skills such as algebra, trigonometry, calculus, etc.

In the “highly educated” western world, it's very doubtful that as much as 10% of the population is capable of understanding these 'advanced' forms of math. Take these interesting numbers from the UK: The government has a target for 56,000 children to study “A” level mathematics by 2011, out of a population of 1,500,000 children aged 17 and 18. That's 3.5% and will represent a large increase on earlier years. On the government's own evidence from previous years, half of these new students will simply drop out of the programme halfway through the course.

Later, we will discuss just some of the reasons why the above should be so. I believe, though, that anyone who can write a commercial programme, can understand enough mathematics to move themselves into that mathematically literate 10%. All that's required, is to approach the task with the right attitude and follow some simple procedures.

Learn the Language

As programmers, we're used to the concept of artificial languages - we use them all the time. Just as C, Java, Perl, etc., are languages created to permit us to accomplish certain tasks, so we can consider mathematics as an artificial language.

The main hurdle for most people, is the strange character set used with mathematics. For some reason, mathematicians just love Greek, or at least, the Greek alphabet. They'll take a sensible word like “sum” and replace it with “Σ”, leaving the ordinary person wondering what on earth they're talking about. So the first thing you need to do is arm yourself with a list of mathematical symbols and their meanings.

Once you begin to build up an understanding of the main symbols, you'll find it easier and easier to use mathematical formula. As a programmer, you don't need to do mathematics. There's a huge body of existing knowledge that you can make use of. All you want is to understand a formula well enough to translate it into useable code.

Dense Versus Detailed

Math notation bears a passing resemblance to some forms of shorthand writing, in that it assumes specialist understanding by the reader and trades clarity to outsiders for speed of use. Inevitably, such an approach makes math opaque to those who lack the time or inclination to learn the code. It might be unkind to suggest that such a situation offers a certain satisfaction to mathematicians who have taken the trouble to aquire the skill. On the other hand, the vast majority of westerners will have unhappy memories of math classes at school, where both teachers and those who did 'get it', were less than kind to the majority who did not.

Any reasonably skilled programmer, by which I mean anyone who can put together a shell script or a simple spreadsheet macro, does possess the ability to understand and use a mathematical formula. Just 'reverse engineer' a formula you wish to use and translate it into your favourite scripting language. Having done that, you may be surprised by just how simple 'advanced mathematics' turns out to be.

Converting a Formula

To see just how easy it is to use a mathematical formula as the basis of a programme, we'll take a commonly used example that comes up a lot in commercial programming, the formula for calculating American style monthly mortgage payments:

The first thing you need to know is what each of the terms means:

  • M = Derived monthly payment.
  • P = initial amount of the loan.
  • J = monthly rate of interest.
  • N = total number of monthly payments to be made.

We can translate this into the following simple Perl programme…

# ------------------------------------------------------
# Perl implementation of U.S. style mortgage calculator.
# ------------------------------------------------------

sub MonthlyPayment
{  
  my $InitialValue = $_[0];
  my $AnnualInterest = $_[1]; 
  my $LengthInYears = $_[2];
  my $NumberOfPayments = $LengthInYears * 12;
  my $MonthlyInterest = $AnnualInterest / (12 * 100);
  return($InitialValue * ($MonthlyInterest / (1 - (1 + $MonthlyInterest) ** (-$NumberOfPayments))));
}

printf("Your monthly payment is \$%6.2f\n", MonthlyPayment(10000, 5, 25));

Some points to note about this:

  • Interest rates are quoted annually but we need to convert that to the monthly value, as used by the formula.
  • The length of the agreement also needs to be altered from an annual to a monthly figure.
  • The single character math terms have been replaced with explanatory variable names. Contrast this with the highly condensed form of the original formula.

Once you have converted a few formulae to code in this way, you should no longer be intimidated by mathematical shorthand, which has the welcome side effect of removing a major obstacle to your understanding and use of mathematics.

Last modified: 2009/01/31 09:40