This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Waitdev_'s profile picture

Uses for logarithm

Started by Waitdev_, 15 May 2016 - 11:48 AM
Waitdev_ #1
Posted 15 May 2016 - 01:48 PM
I've just started learning about the whole "logarithm" stuff, and I heard it was used in programming as well. I was wondering, how can I use it? Where will it come in handy? Thanks. Also, please give example code as well :)/>
Bomb Bloke #2
Posted 15 May 2016 - 03:36 PM
Since you're asking where to use them, I feel a quick recap on what they do is in order:

You're probably well familiar with the concept of raising a number exponentially. You take two numbers, a base and an exponent, and you raise the former by the latter (by multiplying the base by itself the number of times the exponent specifies) to get a power. For example, 2 (base) ^ 8 (exponent) = 256 (power).

With logarithms you're going the other way - you've already got the power and the base, but you want to figure out the exponent. For example, when you solve the power 10,000 using log base 10 you get the exponent of 4, because 10 to the 4 is 10,000.

One handy-dandy use for logarithms (which you're more likely to use than any other) is to figure out the root of a given number… but since that's such a common operation, most languages offer a specific function which does it for you.

In ComputerCraft, the only time I've even considered using logs directly is to resolve the exponents of colour values, though I quickly discarded the idea in favour of building a dedicated lookup table for the purpose. Logs're slow.

As ever, Stack Overflow has a lot of great info. The example re the interest calculator strikes me as a fun project if you're looking for an excuse to try them out.
apemanzilla #3
Posted 15 May 2016 - 06:25 PM
Expanding on what BB said.

The main use for logarithms in CC (for me at least) is to turn color numbers from the colors API into blit-able hex codes:


local hexColorChar = ("%x"):format(math.log(colors.purple) / math.log(2))

For this you'll notice that I'm actually using two logarithms instead of one. This is because CC doesn't let you do logarithms with bases other than 10, so you have to use two instead of one. log(number, base) is the same as log(number) / log(base)
HDeffo #4
Posted 15 May 2016 - 07:11 PM
I just had a possible use for log in a program I am working on now and decided to comment it in here. Essentially I wanted to determine how many bytes a given decimal number would take. the formula for this would be


local bytes = math.ceil((math.floor(math.log(n)/math.log(2)) + 1) / 8)

I decided it would be faster to just turn the decimal number into a binary and get the length of the string (more accurate too) but it was still a possible use for log
Waitdev_ #5
Posted 16 May 2016 - 12:49 AM
Nice, thanks everyone.
Bomb Bloke #6
Posted 16 May 2016 - 12:50 AM
This is because CC doesn't let you do logarithms with bases other than 10,

Truth be told, putting aside tricks for changing base (such as the one you're using there), it can also do natural logs (base e) - and those're what you're using. ;)/>