1

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 :)

Cheyne
  • 113
  • 4

1 Answers1

1

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)
Zev Chonoles
  • 129,973
  • Excellent! that did it. Thank you very much! – Cheyne Jul 21 '15 at 00:00
  • I'm not a photography expert but it looks to me like you've got things backwards in the code you posted: the picture you posted says that $A_N$ is the aperture value and $N$ is the f/number, so if I've interpreted your variable names correctly, you should want

    2 * Math.log2(fval) = apx, which would of course make the correct conversion in the other direction just fval = Math.pow(2, apx / 2).

    – Zev Chonoles Jul 21 '15 at 00:02
  • Yes, I think I had it backwards to start with. I have a mapping of numbers and equated values to reference my answers with, but there are others I needed to calculate on the fly, hence this question. Thanks for your help! – Cheyne Jul 21 '15 at 00:05