Log In  

Is there a logarithm function? I seem to try all the obvious things like log, log10, ln, also finding nothing in the manual or forum.

I can think of workarounds storing an old fashioned logarithm table in my program and looking up in it.. or port some other implementation.. I just imagine there is a log function exposed? Am I missing an easy way to calculate them with other functions? It's the inverse of an exponent maybe I can rearrange my problems somehow to use exponent. HMM well figure someone knows off hand thanks

TIMELIME

P#37824 2017-02-25 17:20 ( Edited 2017-02-26 00:11)

As you've noticed, there is no built-in log function. Pico-8 does not have the complete Lua math library and only has a limited set of its own math functions.

Most log substitutes are based on the logarithmic identities and a lookup table. There are some nice resources on the web about approximating logarithms that are easy to find, like this one:

http://www.phy6.org/stargaze/Slog4.htm

Quoting the topmost example: log 57,140.87 = log (5.714087 x 10^4) = log (5.714087) + log (10^4) = log (5.714087) + 4. So you only need a lookup table for the logs of 1 through 10 to the desired degree of accuracy.

Cheap Pico-8 example (log base 10):

log10_table = {
 0, 0.3, 0.475,
 0.6, 0.7, 0.775,
 0.8375, 0.9, 0.95, 1
}

function log10(n)
 if (n < 1) return nil
 local e = 0
 while n > 10 do
  n /= 10
  e += 1
 end
 return log10_table[flr(n)] + e
end

for x=5,500,10 do
 printh('log10('..x..') = '..log10(x))
end

Keep in mind that the Pico-8 number type has a positive max of 32767, so that's your effective domain. log10(32767) is 4.51543..., so that's your range. You may want more digits of accuracy than this example code provides if you're doing a lot of work within that domain.

P#37830 2017-02-25 18:52 ( Edited 2017-02-26 08:17)

Added this to the wiki. Corrections and feedback welcome.

http://pico-8.wikia.com/wiki/Math

P#37833 2017-02-25 19:11 ( Edited 2017-02-26 00:11)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 12:59:13 | 0.005s | Q:10