Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DAY 008: LOG
#10
Here's a brief write-up for you:

You are likely familiar with the exponentiation (power) operator ^, a kind of repeated multiplication. In general, we have p = b ^ x. Here we'll call x the "exponent" and b the "base".

The logarithm is the inverse of exponentiation, similar to how division is the inverse of multiplication. Instead of calculating p given b and x, it calculates x given p and b. In other words, "what power do I need to raise the value b to so that I get p?"

For instance, 16 = 2 ^ 4. What is the logarithm of 16 with the base of 2? 4.

"But Luke," I hear you cry, "the LOG function only takes one parameter, how do I pass it the 16 and the 2?"

It turns out that over the years the maths and engineering folks have found themselves using the same values for the base parameter over and over. Probably the most common choices for the base when calculating a logarithm are 2, 10 and e (a special number equal to about 2.718 but going on forever like pi).

The LOG function in BASIC calculates what's called the "natural logarithm", which uses e as the base. So when you write LOG(9), you're asking the computer "what value of x do I need so that e ^ x = 9?" or approximately, so that 2.71828 ^ x = 9. It turns out LOG(9) is about 2.197, and if you check you'll see 2.71828 ^ 2.197 is pretty darn close to 9.

Why didn't the designers of BASIC let you use any value for the base? One reason is that it's quite easy to calculate any kind of logarithm with a simple function:

Function logarithm(base, value)
  logarithm = Log(value) / Log(base)
End Function

You just divide by the LOG of the base you want to use and you get your answer. Armed with this, we can take a look at the other common bases, 2 and 10.

When we write down numbers, we use a place value system where each place makes a digit worth 10 times more. This means the number 173 is the sum of powers of 10: 173 = 1 * 10^2 + 7 * 10^1 + 3 * 10^0. Logarithms let us go the other way and work out how many digits (or places) we need to get a certain value. Because we're using decimal, this is a base 10 logarithm:

Print Int(logarithm(10, 173)) + 1 '3
Print Int(logarithm(10, 2914)) + 1 '4

We need Int here because this is a math function that gradually increases to the next value, and +1 because it's actually giving you the exponent of the highest power of 10 in the "173 = 1 * 10^2 + 7 * 10^1 + 3 * 10^0" representation.

This trick works for binary too: doing it with base 2 will tell you how many bits a number has.

A final note: the base of the natural logarithm e (2.718...) can be had with the EXP function. EXP(x) calculates e ^ x, so EXP(1) is e itself.
Reply


Messages In This Thread
DAY 008: LOG - by SMcNeill - 11-13-2022, 11:17 AM
RE: DAY 008: LOG - by bplus - 11-13-2022, 03:24 PM
RE: DAY 008: LOG - by SMcNeill - 11-13-2022, 05:46 PM
RE: DAY 008: LOG - by mnrvovrfc - 11-13-2022, 05:17 PM
RE: DAY 008: LOG - by Pete - 11-13-2022, 05:55 PM
RE: DAY 008: LOG - by TerryRitchie - 11-13-2022, 07:40 PM
RE: DAY 008: LOG - by Pete - 11-13-2022, 08:12 PM
RE: DAY 008: LOG - by CharlieJV - 11-19-2022, 04:57 PM
RE: DAY 008: LOG - by bplus - 11-13-2022, 08:25 PM
RE: DAY 008: LOG - by Pete - 11-13-2022, 08:29 PM
RE: DAY 008: LOG - by luke - 11-19-2022, 06:36 AM



Users browsing this thread: 2 Guest(s)