Simple Logarithm and JavaScript Question

437 Views Asked by At

I have a simple formula that I am trying to convert to JavaScript, I'm just stuck trying to reverse it. My math skills have deteriorated over the last few years and im stuck.

Here is the formula enter image description here

And here is my interpretation of it in JavaScript

2 * Math.log2(apx) = fval

Given that I only know the value of fval, and im actually trying to get the value of apx, can anyone tell me how I would go about doing that? Double points if you can help with the javascript conversion :)

1

There are 1 best solutions below

3
On BEST ANSWER

The main thing to remember about logarithms (indeed, their defining property) is that for positive real numbers $a$ and $b$, with $a\neq 1$, we have $$\large a^{\log_a(b)}=b$$

Therefore, if you have $$2\cdot\log_2(N)=A_N$$ then dividing both sides by $2$ we get $$\log_2(N)=A_N/2$$ and then raising $2$ to the power of both sides we get $$N=2^{A_N/2}$$ I don't know whether to expect any floating-point arithmetic problems, but naively turning this formula into Javascript would give

apx = Math.pow(2, fval / 2)