smith-logarithms
smith-logarithms
Logarithms make a lot of people anxious. A lot of this has to do with the way
they’re often taught in high school and secondary school: by memorizing all
the proper steps, without imparting much deeper meaning. For example,
maybe you were once taught to solve problems like this:
log7 49 = ?
If you are like most people, you knew how to solve this years ago, when
it was important to pass a math test. You were probably taught how to
rearrange this into an exponential equation using the following equivalence:
logb x = y ⇐⇒ by = x,
and from there you solved for the unknown. But without any solid intuition
for what this all really means, you were probably just going through a series
of memorized steps in order to get the right answer. This isn’t helpful: if
you just memorize without understanding, you’ll forget all about it once it’s
no longer important.∗
The problem is that logarithms are important. They are especially cru-
cial in computer science when one wants to analyze two different algorithms,
figuring out which one is the more efficient. Thus, the purpose of this work is
to give you a more intuitive feel for what a logarithm represents, making you
more comfortable with what they mean. You’ll be able to estimate them in
your head, and hopefully this knowledge will stay with you for a long time.
Scales of Ten
The most important thing to know about logarithms is this:
Think of all the one-digit numbers, 1 through 9. (For now we’re skipping
over 0.) Of course these numbers are all different, but they’re close enough
to each other to be easily comparable. However the two-digit numbers, 10
through 99, are on a totally different scale. They’re easily comparable to each
∗
Maybe it even convinced you that you’re “not a math person”, even though you had
done perfectly fine in math class before. This is a problem, because when people become
convinced that they “can’t do math”, it can limit their career options in the future.
Appendix X: Understanding Logarithms Intuitively 2
other, but most of them are so much bigger than the one-digit numbers, that
it’s hard to think of them in quite the same way. If you were drawing them
next to each other on a graph, the two-digit numbers would dominate, and
you could barely read the one-digit values at all. Next come the three-digit
numbers, 100 through 999. And these are on an even higher scale: it’s easy
to compare them with each other, but harder to compare them with the two-
digit numbers and even harder to compare them with the one-digit numbers.
They’re all on different orders of magnitude.
These scales of size 10 are the base-10 logarithms, which are represented
by “log10 ” in equations. Base-10 logarithms are sometimes called common
logarithms. That small “10” is the base, and it can be any positive number
except 1 (though we’ll soon see that only a few bases are commonly used).
If you have a round number like 1, 10, 100, and so on, the base-10 logarithm
of that number is just the number of zeros it has:
For a number that lies between two of these round numbers, the loga-
rithm will be some intermediate decimal value (probably one that goes on
forever without repeating). So all the one-digit numbers greater than or
equal to 1 have a logarithm between 0 and 1. Likewise, the two-digit num-
bers have a log between 1 and 2, and so on. So if you want to estimate the
logarithm of a number, all you have to do is count its digits. For example the
number 83,176,000 has eight digits, and therefore its log must be between 7
and 8. And since it’s a large eight-digit number, the log is closer to 8 than
7. (In fact, the log of this number is approximately 7.92.)
Here’s the graph of positive base-10 logarithms:
Look at how flat it is—it increases very slowly. Nevertheless, it always goes
up: bigger numbers always have bigger logarithms. You can also see that
it gets flatter and flatter as it goes on; it takes bigger and bigger inputs for
each subsequent increase of 1. But it never gets totally flat: if you increase
the input, the output is always at least a little bigger. And unlike other
functions, it doesn’t have an asymptote that it approaches forever but never
reaches. It will reach any value you can think of, eventually.
In the base-10 logarithm, each scale is 10 times bigger than the previous
one. Another way to think of it is “Starting from 1, how many times do I
3 Appendix X: Understanding Logarithms Intuitively
So how many factors of ×10 are there in this number? It’s easy to see that
there are 7, and then most of an 8th (the 8.3176 is “most of” an 8th ×10).
So the log of about 7.92 makes sense. If the one-digit numbers are “in the
zeroth scale”, and the two-digit numbers are “in the first scale”, then we can
think of 83,176,000 as being “in the seventh scale, but almost to the eighth
scale”.
Remember that log10 100 is 2, and log10 1000 is 3. So which number has
a log of 2.5, halfway between them?
log10 x = 2.5.
To solve this, we need to multiply 1 by a factor of ×10, two and a half times.
But how do you multiply half a time? The answer is to use a square root:
√
x = 10 × 10 × 10
x =∼ 316.23.
log10 a = 6.96
log10 b = 4.26
So we really can tell a lot about numbers by just looking at their loga-
rithms, without calculating them. But we can calculate a and b if we choose,
by raising 10 to the two logs:
a = 106.96 = ∼ 9,120,108
b = 104.26 = ∼ 18,197
An Interplanetary Example
So why are logarithms useful? Sometimes you might want to compare items
that are so different from each other, that it becomes difficult. For example,
let us say that we wanted to compare the sizes of the major bodies of the
solar system. We want to communicate clearly, so we decide to make a graph
of the volumes in cubic megameters (Mm). (A megameter is 1000 km: about
the distance from Portland, Oregon to Salt Lake City, Utah, or from Paris,
France to Madrid, Spain. It’s a good unit to use when talking about sizes
of planets.)
The bar graph above shows what happens when we display these values
in the traditional way. We can see the problem right away: the Sun is so
much bigger than everything else, that it’s almost impossible to read any
of the other values! Is Saturn bigger or smaller than Neptune? How does
the size of Venus compare to the size of the Moon? The only thing that’s
obvious here is that the Sun is gigantic, but most people already know that.
This graph just isn’t that useful.
But what if instead make a log-scaled graph, like this:
5 Appendix X: Understanding Logarithms Intuitively
Here, the height of each bar is the logarithm of the value. See how every
notch on the left is 10× the value of the one before it? This makes the
overall graph much clearer, because it means that we can graph values that
differ by quite a lot. Effectively, we’re graphing the scales of the volumes.
The Sun is still clearly the biggest thing, but now we can see better how the
planets compare. Jupiter and Saturn are about three scales smaller than
the Sun, so they’re about 1000× smaller. Uranus and Neptune are one scale
smaller still: each one is about one tenth the size of Jupiter or Saturn. And
Earth and Venus are two scales smaller still. So therefore Earth is six scales
smaller than the Sun—or about a million times smaller! This graph is much
more useful and more readable than the one above, thanks to logarithms.
Be careful with log-scaled graphs—they can be misleading if you don’t
read them carefully. For example, it might look to some people that Earth
is about one third the size of the Sun, because its bar is about a third the
height. This just isn’t true—it’s six whole scales smaller. Log-scaled graphs
should only be used when there’s a clear benefit like there is here.
log10 31 ≈ 1.49.
e × e × e ≈ 20.
∗
Since “Euler” is a German name, it is pronounced much like the English word “oiler”.
7 Appendix X: Understanding Logarithms Intuitively
ln 20 ≈ 3.
Calculating natural logs in your head is more difficult than the other
common logs, since e is a non-repeating decimal number. Fortunately,
they are rare when studying computer science. You will encounter
them much more when you study calculus, physics, or chemistry.
log2 64 = 6.
20 = 1 23 = 8 26 = 64 29 = 512
21 = 2 24 = 16 27 = 128 210 = 1024
22 = 4 25 = 32 28 = 256
Analyzing Algorithms
It turns out that logarithms are very important for analyzing and comparing
different algorithms to each other. Sometimes, there are two different ways
to solve a problem, and both of them will get the right answer. But if
one method is much quicker and more efficient than the other, than that
method should be used—even if the slower method would eventually solve
the problem.
For example, let us say that you are programming some kind of word
game in which the players take turns playing cards or tiles with letters on
them, in order to spell out words. The computer needs to judge whether or
not a certain play is a legitimate word, rejecting nonwords like “AQUR” and
“NUMPY”. Say you have a big list containing all the allowed words, and
you need to check it every time a play is made, to make sure it’s allowed.
We will call the word we’re checking a “query”. If a query is “valid”, that
means that it’s in the list. An “invalid” query is not.
One approach is to do an exhaustive search, like is shown on the next
page. You just check all the words in the big list one at a time, seeing if
you can find the queried word. But this can take a lot of time. If the query
is valid, on average you’ll have to search through half of them before you
find it. But if the query is invalid, you’ll have to look through every word
in the list and rule them all out one at a time. This search is an example of
a linear algorithm, because the number of checks it makes is proportional
to the number of words in the list. If there were twice as many words, the
9 Appendix X: Understanding Logarithms Intuitively
is much more efficient than a linear algorithm like exhaustive search. Many
other computer algorithms depend on a doubling or halving factor like this,
and so their analysis will involve binary logs. Thus, understanding logs can
help you pick out the fastest method to solve a problem.
Negative Logarithms
So far, we’ve been concentrating on the positive numbers. But not every
logarithm is positive—so it’s natural to ask what the relation is between
negative numbers and logarithms.
First, we usually don’t allow the base or the input of a logarithm to
be negative. We start running into the same problem as when taking the
square root of a negative number: the result is often an imaginary or complex
number. So we’re not going to worry about those cases here.
But if you take the logarithm of a positive number less than 1, the result
will be negative. This makes sense: 1 has a logarithm of 0, so smaller
numbers must have logs less than 0. Negative logarithms represent smaller
and smaller scales. For base-10 logs:
1 1 1
log2 1 = 0 log2 8 = −3 log2 64 = −6 log2 512 = −9
log2 12 = −1 1
log2 16 = −4 1
log2 128 = −7 1
log2 1024 = −10
log2 14 = −2 1
log2 32 = −5 1
log2 256 = −8
No matter which base we’re using, the logarithms get steeper and steeper as
the input approaches 0. But you can’t really take the logarithm of 0 itself—0
just isn’t on any scale, so the answer isn’t defined. However, you can say
that the log approaches negative infinity as x approaches 0. The log of any
infinitesimally small number will be a very negative number.
Let’s try to estimate the log of a number less than 1. What is the binary
log of 31 ? We know that 13 is between 14 and 12 , whose logs we already know.
If we take the logs of all the numbers in this relation, we can find upper and
lower bounds on the log of 13 :
1 1 1
4 < 3 < 2
1 1 1
log2 4 < log2 3 < log2 2
−2 < log2 13 < −1
Therefore, we know that the binary log of 13 must be between −2 and −1.
(Indeed, it is ∼ −1.58—roughly halfway between them.)
Logarithm Identities
Finally, let’s work through some common logarithm identities. These are the
equations that you might see when studying logarithms in a more formal way.
After each identity, we’ll explain why it is true on an intuitive level.
The first identity can be used to relate different logarithms with bases b
and d to each other:
1
logb d = .
logd b
What does this mean? For now, imagine that b = 2 and d = 10. Remember
that the binary log is counting the number of doublings, and the base-10
Appendix X: Understanding Logarithms Intuitively 12
log is counting the number of ×10 factors, to make up some number x. But
how many doublings are there in one ×10 factor? That’s just the binary log
of 10:
log2 10 ≈ 3.32.
In other words, multiplying something by 10 is the same thing as doubling
it about 3.32 times. Multiplying a number by 100 is the same as doubling it
about 6.64 times. The two logarithms are very closely related. We can also
go the other way:
log10 2 ≈ 0.301.
This means that doubling something is the same thing as multiplying it by 10
about 0.301 times. And those numbers log2 10 (∼ 3.32) and log10 2 (∼ 0.301)
must be reciprocals of each other, since they represent the “exchange rate”
between the two logarithms.
The above boxed equation simply makes it clear that this is true no
matter what b and d are. So long as they’re valid logarithmic bases, you
can find this “exchange rate”, and the two rates must be reciprocals of one
another.
Furthermore, we can take advantage of this to calculate binary logarithms
on calculators that can only do base-10 logarithms. This is expressed in our
next important equation:
This could easily be done on a calculator that does base-10 logs. Of course,
the above boxed formula works no matter what b and d are. But you will
13 Appendix X: Understanding Logarithms Intuitively
find this most useful when you’re trying to calculate binary logarithms, when
your equipment won’t let you do it directly.
Next:
logb xy = logb x + logb y .
All this is saying is that the number of ×b factors in the product xy is just
equal to the number of ×b factors in x plus the number of ×b factors in y.
For example, what is log2 4096? Remember, that means how many doublings
you must do to attain 4096. First, notice that:
4096 = 1024 × 4.
And we already know that you need 10 doublings to get to 1024, and 2
doublings to get to 4. Therefore, the total number of doublings to get to
4096 must be 12:
And of course, this is true no matter what base you are using.
A related formula is:
x
logb = logb x − logb y
y
This one just goes in the opposite direction. What if you’re trying to cal-
culate log2 512, but you don’t have it memorized? But you remember that
log2 1024 is 10, and 512 is half of 1024. So you can think of 512 this way:
start at 1 and double it ten times to get up to 1024, but then halve it once
to get back down to 512. Mathematically, this is:
logb xp = p × logb x .
If you have some number x raised to a power p, that means that you’re
multiplying together p different instances of x. For example:
x5 = x × x × x × x × x.
Appendix X: Understanding Logarithms Intuitively 14
Each of those instances of x must have the exact same factors. So if you
know the logarithm of x, the logarithm of xp must be p times as great. For
example, what is the binary log of 85 , which is 32,768?
All this really means is that 85 is five different factors of ×8, and each
individual ×8 is 3 doublings. Therefore, 85 must be 5 × 3 = 15 doublings.
Conclusion
Logarithms can be hard when you’re first getting used to them. They’re
a different kind of math, working with scalings of numbers rather than di-
rectly with the numbers themselves. However, you can be at ease with them
once you develop an intuitive sense for the way they work. Hopefully this
appendix has helped with that, and will let you go forward and use them
with more confidence.