
-----------------------------------
genius12
Sun Nov 25, 2012 7:00 pm

How to get a logarithm of a number?
-----------------------------------
What is it you are trying to achieve?

I am trying to get turing to find the logarithm of a number. I thought that there would probably be a function for logarithms in Turing but there isn't, or 
atleast I didn't find one.


What is the problem you are having?
I can't find a function or a way to get the logarithm of a number.


Describe what you have tried to solve this problem
I have tried finding a function of method to find the logarithm of a number but was unsuccessful.


Please specify what version of Turing you are using
4.1.1

-----------------------------------
Zren
Sun Nov 25, 2012 8:04 pm

Re: How to get a logarithm of a number?
-----------------------------------
Oh wow, Turing doesn't actually have log10, log(x, base) or any of that jazz.

Well, Turing doesn't have a very full arsenal in this reguard, nor will it ever be expanding. It does have the key function log(x, base) = ln(x) / ln(base). With that we can create our own functions.


/**
 * Return the natural logarithm of x (to base e).
 */
% From the math module (Math.tu)
% function ln (r : real) : real

/**
 * Return the logarithm of x to the given base, calculated as log(x)/log(base)
 */
fcn log (x : real, base : real) : real
    result ln (x) / ln (base)
end log

/**
 * Return the base-10 logarithm of x.
 *
 * Notes: Normally this function would be more accurate than log(x, 10).
 */
fcn log10 (x : real) : real
    result log (x, 10)
end log10

put log (8, 2) % 3
put log (1 / 2, 2) % -1

put log10 (1) % 0
put log10 (10) % 1
put log10 (100) % 2
put log10 (1000) % 3

% put log(0,0) % ERROR
% put log10 (0) % ERROR


-----------------------------------
genius12
Sun Nov 25, 2012 8:42 pm

Re: How to get a logarithm of a number?
-----------------------------------
Yup. Well didn't realise that Turing had a natural logarithm function. Thank you for pointing that out.
