Does anyone have any idea how to create a function that solves Logarithms? (just curious) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Does anyone have any idea how to create a function that solves Logarithms? (just curious)

I've been trying to figure it out but I'm just out of ideas

5th Mar 2018, 10:26 PM
CommentSense
CommentSense - avatar
1 Answer
+ 2
It's not easy! You can take advantage of how the computer stores floating point numbers. The computer will internally store any float in scientific notation, like a * 2^b. So for example the number 18 = 1.125 * 2^4. If we take the log (base 2) of that, we get log(1.125 * 2^4) = log(1.125) + log(2^4) = log(1.125) + 4 by the usual logarithm rules. The cool thing is that you can read off the exponent b (in our case 4) right from the way the computer stores a float. And for the log(1.125) part you can use approximations, like the so-called mercator series. I'm not sure if that's the way computers and maths libraries actually do it though. It also requires some bit-level hacks that are probably awkward to do in javascript.
5th Mar 2018, 11:23 PM
Schindlabua
Schindlabua - avatar