3 ms·
If I ever make a programming language the log function will take two parameters, value and base. It's too confusing when engineers, mathematicians, and computer
by 3pt14159 9y ago
If I ever make a programming language the log function will take two parameters, value and base. It's too confusing when engineers, mathematicians, and computer scientists all use different implicit bases.
- 52-6F-62 9y agoI had to do a double take for that reason. They're 100% correct, but I assumed what I'd learned from math. Whenever I otherwise work with logarithms in programming I choose to work explicitly to at least save myself confusion. By default I assumed log()/Math.log() referred to common logarithms. I usually end up re-implementing the log function with 2 values, as with ln/logE above, but substituting the constant. long double logn( long double n, long double x ) { return log(x) / log (n); } But, yes, all my above code could be shorter. I haven't worked with any explicitly scientific languages like Mathlab— do any languages take a more traditionally mathematical approach? Or any libraries that have taken that torch up?
- jwilk 9y agoFor common cases of n=2 and n=10, consider using dedicated functions log2() and log10(), because they are more precise. For example: log(1E42)/log(10) == 42 - 7.105427357601002e-15 log10(1E42) == 42.0
- 52-6F-62 9y agoNoted. It’s more for other cases. While doing any algebra coursework I usually (loosely) confirm my thoughts by running them in a repl with said functions to at least keep me on track— so logn usually serves uncommon bases. My thinking that way might not make much sense, but I start to explain it in other comments. Anyway— thanks, I appreciate the insights. That’s the reason I like this forum