+ 2

ln vs. log10: accuracy and productivity

I was thinking about cases when you can use either ln (natural logarithm; base-(Euler's number) logarithm), log10 (log; base-10 logarithm) or logarithm of other base (if possible) and faced a question: are there privileges of using one exast function in terms of accuracy of calculations and productivity, speed of calculating? I myself can tell that there is better accuracy as you use logarithm of base closer to 1, but am not sure about productivity and/or other privileges, so I decided to ask. If that means, I can provide examples. And, at the last, I know that this question may not follow present SoloLearn Discuss guidelines, but have still decided to give it a try. That means that I can delete this question if so is said by a moderator or senior Q&A'er and, also, re-post it in other form (for example, Feed Post) if so is also said.

18th Mar 2021, 8:11 AM
#0009e7 [get]
#0009e7 [get] - avatar
3 Respuestas
+ 2
In python math.log can take the base as second parameter: https://www.geeksforgeeks.org/log-functions-python/ C/C++ and javascript don't support selectable base and define log as natural logarithm. As these 3 important languages don't have it and log(a)/log(b) seems to be the recommended way, I'd assume that the possible loss in precision or speed is irrelevant, if even existent. I didn't find an algorithm to calculate logarithm for arbitrary base directly. If the base b is arbitrary but always the same, ln(b) can be provided as a constant. Some libraries provide ln(10) to switch base from e to 10. Most algorithms work by iterative approximation. I found Wikimedia a good resource for overview and explanation: https://en.wikipedia.org/wiki/Logarithm#Calculation
18th Mar 2021, 5:39 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 4
Your question is language agnostic, but still I'd say pretty much every language should have a math library containing a log function with selectable base. These should be highly optimized by algorithm, look up tables for faster initialization, and/or usage of specialized instruction set extensions. It will be hard to beat those functions. You can however optimize for different metrics (runtime, accuracy, memory usage) and if your focus for optimization is different you might find a better solution. Mathematically log_y(x) (base y logarithm of x) = log(x)/log(y) with any base. Normally base 10 is used. But I'm pretty sure a naive implementation if this will reach neither the speed nor the accuracy of a specialized function. I'm not quite sure what you mean by privileges or productivity in this context.
18th Mar 2021, 9:40 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
Benjamin Jürgens, >>> [...], but still I'd say pretty much every language should have a math library containing a log function with selectable base. As far as I know (and Google'd), only Ruby (1.9 and later) and C# support this, while C, C++, JavaScript, Python, Dart, F#, Perl and Scratch do not. This was the one purpose, the other was, for example, to simulate power function in Scratch with "e ^(a * ln(b))" (Euler's number is in use here), as there is no such block. The third purpose is to know about it in general (for example, I guess, I would need this information if I would use assembly; or "just-to-know" (happens to me often)). By privileges I mean factors that make use of one exact function better, recommended, etc. By productivity I mean speed of function executing by videocards (could be for different), assembly or just some programming languages the person answering knows. Anyway, thank you for answer.
18th Mar 2021, 1:28 PM
#0009e7 [get]
#0009e7 [get] - avatar