4 ms·
That's fun. I'm recently working my way back through my fundamentals prepping for some future hopeful coursework and had to play with number 1. #include "s
by 52-6F-62 9y ago
That's fun. I'm recently working my way back through my fundamentals prepping for some future hopeful coursework and had to play with number 1.
#include "stdio.h"
#include "math.h"
long double ln ( long double n ) {
return log(n) / log(M_E);
}
long double pi ( void ) {
return pow(ln(6), pow(ln(5), pow(ln(4), pow(ln(3), ln(2)))));
}
int main( void ) {
printf("%LE\n", pi());
return 0;
}
Or in JS
function ln(n) {
return Math.log(n) / Math.log(Math.E);
}
function pi() {
return (ln(6) ** (ln(5) ** (ln(4) ** (ln(3) ** (ln(2))))));
}
If anybody else wants to toy with it.
Getting older and working outside of the fields that employ a lot of certain technicals doesn't leave as much time for exploration as one would like.
- jwilk 9y agoBoth log() in C and Math.log() in JS are natural logarithms, so there's no need to define your own ln() functions.
- 3pt14159 9y agoIf 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
- 52-6F-62 9y agoTIL! I completely and foolishly assumed that when I saw log on its own that it referred to a common logarithm. About 2 seconds on MDN and a math.h reference and I could have known better.
- AtomicOrbital 9y agoinitially I tried pasting ln(6) ^ (ln(5) ^ (ln(4) ^ (ln(3) ^ ln(2)))) into bc to no avail ... magically paste it into google - Bam
- jwilk 9y agobc does have natural logarithm, but it's called l(), and you have to enable the math library with the -l option. There's another problem, though: the ^ operator requires that the second expression is integer.
- jwilk 9y agoAnother weird thing about your C code is that is uses "long double" as argument and return types; but pow() and log() functions use doubles, so there's some unexpected rounding going on. Possible ways to fix that: - Use the "double" type everywhere. - Use powl() and logl() functions instead. - Use tgmath.h instead of math.h. (But when I tried this with GCC 7 + glibc 2.27, the compiler ran out of memory. Go figure…) - Use C++, which has overloads for pow() and log().