Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Logarithm Lua (Answer has trick that can be used on almost any language)

I'm working on a base converter for lua that can convert the base of any number to another base. For that, I need a logarithm calculator. Example function (where logarithm(100, 10) would equal log10(100), which would equal 2):

function logarithm(value, base)
    ... -- Algorithm here
end
print(logarithm(100, 10))

Which would output:

2

P.S: I want the algorithm to accept any base, any value.

like image 546
Potassium Ion Avatar asked Oct 12 '25 06:10

Potassium Ion


1 Answers

You can always use that

logb(a) = logc(a) / logc(b)

where c is one of the provided bases, like the Euler number e of the natural logarithm or 10, sometimes also the basis 2 logarithm is provided.

like image 127
Lutz Lehmann Avatar answered Oct 16 '25 07:10

Lutz Lehmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!