# Math for programming (logarithms)

### Jasterix ・2 min read

###
*(...part 1)*

Recently, I found myself stuck on a JavaScript challenge. I lost the original link to the problem, but this Exercism.io example is a close match:

- Convert a number, represented as a sequence of digits in one base, to any other base.
- Implement general base conversion. Given a number in base a, represented as a sequence of digits, convert it to base b.

Off the bat, this challenge doesn't read as difficult, but it require additional understanding of logarithms.

Though I did study math up to Calculus 2 in college, little to none of that is within my immediate recollection, which is to say– I don't remember any of it. Never one to give up and always one ready for distraction, I quickly found myself down the rabbit hole of researching math for programming. But eventually, I back to the problem I needed to learn how to solve.

So, because of procrastination, this blog post will only be part one of solving the challenge above. This post will be a review of what logarithms are.

Along the way, the best video I found to quickly bring me up to speed was by the logarithms The Organic Chemistry Tutor. He does a great job explaining the process for evaluating logarithms and provides some examples for you to test your understanding. I've included some cliffnotes below the video.

#### First a refresher on terms:

- Logarithm refers to the power you must raise one number to achieve another number
- Given log
_{3}(9) = 2 because 3^{2}= 9- 3 represents the base, which is the number you start with
- 9 is the argument, or the number you're ending with
- 2 is the exponent, aka the answer, aka the power to which you must raise the base in order end up with the answer

#### Some basic rules to keep in mind:

- log1 = 0
- This is because, if the base isn't specified, it is assumed to be 10
- log100 = log
_{10}(100) = 2

- log100 = log
- Logarithms are the inverse of exponents
- log
_{3}(9) = 2 because 3^{2}= 9

- log
- log
_{9}(3) = (½)- If the base is larger than the argument, the answer will be a fraction corresponding fraction.
- Meaning if the log
_{3}(9) = 2, log_{9}(2) = ½

- Similarly, if the base is smaller, the answer will be a whole number
- since log
_{2}(8) = 3 and log_{8}(2) =^{1}⁄_{3}

- since log
- If the argument is a whole number, the answer will be a positive number
- If the argument is a fraction, the answer will be a negative number
- log
_{3}^{1}⁄_{9}= -(2) - log
_{9}^{1}⁄_{3}= -(½)

- log

I also recommend checking out the Logarithm article on Simple Wikipedia

Part 2 will cover changing the base of a logarithm. As always, let me know if you have any questions or suggestions for the next blog post.

"If the base is smaller, the answer will be a whole number." What about log_4(8) or log_2(10)?

"If the argument is a whole number, the answer will be positive." What about log_2(1)?

"If the argument is a fraction, the answer will be a negative number." What about log_(1/2)(1/2), or indeed log_10(3/2)?

"If the base is larger than the argument, then the answer will be a fraction." What about log_10(2)?

I'm not just being an ass; thinking about these counterexamples can lead to a better understanding of logarithms and the language of mathematics (and the importance of investigating edge cases). As they stand, however, I would be concerned that people reading these basic rules might pick up misconceptions which make it harder to understand logs rather than easier. Maybe we could edit them?

Hey Simon,

Here is something that might cook your noodle.

When is the negative sign used with respect to logs? Why are negative logs important? And more, when would you use fractions and negative signs together?

For example, could you find?

log_5(1/25) =? Or

log_2(sqrt(2)) = ? and

log_2(1 / (sqrt(2)) ) = ?

;))

Sure, these are also good educational examples, some of which can be used as a counterexample to Jasterix's positions. I was just offering a minimum working counterexample to each claim but you can certainly think of more. log_4(8) is a particularly potent counterexample since 8 is an integer and yet the result from log_4 is not an integer (since 8 = 4

^{3/2}) which creates more cognitive dissonance than an example like log_2(sqrt(2)). In my reply to Jasterix below, you can see a more comprehensive summary into which each of your examples can be (exhaustively but not exclusively) categorised.Hi Simon, you raised some great points. I was trying to make this as straightforward as possible, but you're right about considering edge cases. How would you restate these points?

Maybe something like

log_a(a

^{n}) = n and a^{log_a(n)}= n because log_a(x) and a^{x}are inverse functions. For example log_2(8) = log_2(2^{3}) = 3.Therefore we have log_a(x) = y <=> x = a

^{y}log_a(1) = 0 because 1 = a

^{0}log_a(a) = 1 because a = a

^{1}because of the above two points, if 1<b<a then 0<log_a(b)<1. For example, log_4(2) = 1/2, because 4

^{1/2}= sqrt(4) = 2.if a>1 and b>1 then log_a(b) > 0

if a>1 and 0<b<1 then log_a(b) < 0. For example, log_2(1/8) = -3 because 2

^{-3}= 1/8.log_a(b) is an integer if and only if b = a

^{n}for some integer nCan you think of a number x such that 2

^{x}= 10? Nor can I. But it exists and it is defined as x = log_2(10). Because 2^{3}= 8 and 2^{4}= 16, x will be somewhere between 3 and 4. It is hard to estimate x without a calculator!youtu.be/sULa9Lc4pck this is a good resource for thinking about logarithms in the context of other common exponential operations.

Thanks for sharing! This was an awkward first attempt at summating logs