Logarithmic notation: Mathematics vs. computer programming

One of my concerns as an educator is the way in which peccadilloes of mathematical notation can get in the way of understanding. In the case of logarithms, this has become more troublesome as general education about numeric bases at the secondary level has apparently evolved primarily into an interest of computer programmers.

The notation of logarithms is strange to begin with. As Steve Kelly explains, a logarithm is really just a way to rewrite the focus on an exponential expression. For instance, \(2^3=8\) can be expressed as \(\log_2 8 = 3\). Both express the same concept: \(2\cdot 2\cdot 2 = 8\). However, in the first case, that concept is stated plainly; in the second, the focus is changed to answer the question, “How many times must I multiply 2 by itself to get 8?”.

In an alternate timeline, we could have developed distinct symbols for powers and for inverse functions, and then we could have simply indicated logarithms as the inverse of powers. For instance, let us say that we used @ instead of -1 for the inverse function. In that case, we could write \(2^3=8\) and \(2^8@=3\). But we don’t live in such a timeline.

In this timeline, we write the inverse of \(b^p=n\) as \(\log_b n = p\). From that alone, it’s easy to see why students get confused.

To make matters worse, though, there are two bases for which logarithms are particularly useful: e and 10 (there is also some call for logs of base 2, enough that ISO has a standard abbreviation for it). Depending on the context, it is tempting to leave the base off and use one of these as the default. But: Which one?

Historically, mathematicians have used \(\log x\) as shorthand for \(\log_{10} x\). This makes sense for a few reasons. For one, slide rules (remember those?) were based on \(\log_{10}\). For another, mathematics tends to use base 10 as the default; indeed, according to my students, the only time they heard “base” in public school mathematics class was in terms of logarithms. There is also a separate abbreviation, ln*, that is commonly used for \(\log_e\).

However, there is some defense for using e as the default base, particularly since \(\log_e\) is typically called the natural log. From a computer programmer’s perspective, 10 is an easier base to explicitly specify than is. Also, e is an extremely important value in a variety of fields. In accounting, for instance, it is central to determining a maximum return on investment, characterized in the formula \(A=Pe^{rt}\) (in a continuously compounded account, the total amount in the account is equal to the original investment times e to the power of the interest rate times the number of time periods).

The current trend is that mathematicians tend to use log without qualification to indicate \(\log_{10}\) while computer programming languages tend to use it to indicate \(\log_e\). For the most part, this doesn’t need to lead to any particular confusion, since computer programmers who are dealing with logarithms probably have enough savvy to understand bases. An understanding of bases is fairly crucial to effective computer programming, since computers function at base 2; base 16 (=2^4) is also important.

Also, especially in computer languages that allow overloaded functions, this method becomes particularly tantalizing: log(x) can refer to \(\log_e x\) while log(x,b) can refer to \(\log_b x\). For that matter, the programmer who would rather not leave anything to default can write log(x,e) (assuming the language has or can build a constant reference equal to e).

A quick survey of languages shows that some use overloading with a default of base e (C#, Python, Ruby) while others (Perl, PHP, Haskell) require using the algebraic relation \[\log_x y = \frac{\log_e y}{\log_e x},\] possibly with an explicit function for base 10 or base 2.

I say “for the most part” because there are a few overlaps between the world of mathematics-savvy computer programmers and the world of general mathematics. One overlap is Microsoft Excel, which uses =LOG(x[, base]) with a default of base 10 and =LN(x) for the natural log. In other words, Excel (being designed for non-programmers) uses the defaults taught in mathematics classes. However, Excel’s VBA function LOG(x) returns the natural log (as a computer programmer would expect) and does not take an optional parameter as a base. This clash could potentially lead to confusion, particularly since VBA macro writers don’t always have the same depth of programming experience as those who deal with more robust programming languages.

Another overlap is Wolfram Alpha (and Wolfram’s software package Mathematica), which defaults to the natural log. Unlike the programming languages I looked at, though, if there are two parameters of the function, the first one is the base, not the second one. That is, log(10) is assumed to be the natural log, while log(10,100) returns 2 in Wolfram Alpha but 0.5 in C# and similarly behaving languages.

I do have to admit: Given how the mathematical notation is read, Wolfram’s version is the most logical. However, because of its apparent uniqueness, it’s also the most prone to confusion. At least Wolfram Alpha notes that it’s using the natural log as the default and provides a link for using the common (base 10) log instead.

* I’m inclined to think this is short for logarithme népérien, that is, “Napier’s logarithm”, despite the standard English name “Natural logarithm” (French logarithme naturel).

Leave a Comment

Your email address will not be published. Required fields are marked *