This commit is contained in:
Miloslav Ciz 2025-03-01 01:35:35 +01:00
parent d89468d6da
commit 521e728375
39 changed files with 2037 additions and 1973 deletions

4
log.md
View file

@ -115,7 +115,7 @@ To get back to the **logarithmic scales** for a moment: these are scales whose v
It won't come as a surprise that we'll find the logarithm function built in most of the popular [programming_languages](programming_language.md), most often present as part of the standard math [library](library.md)/module. Make sure to check which base it uses etc. [C](c.md) for example has the functions *log(x)* (natural logarithm), *log10(x)* and *log2(x)* under *math.h* -- if you need logarithm with different base, the simple formula given somewhere above will serve you to convert between arbitrary bases (also shown in an example below).
Should you decide for any reason to implement your own logarithm, consider first your requirements. If integer logarithm [suffices](good_enough.md), the straightforward "[brute force](brute_force.md)" way of searching for the correct result in a for loop is quite usable since the number of iterations can't get too high (as by repeated exponentiation we quickly cover the whole range of even 64 bit integers). In C this may be done as follows (we have to watch out for [overflows](overflow.md) that could get us stuck in an infinite loop; this could also be addressed by using division instead of multiplication, but division can be very slow):
Should you decide for whatever bold reason to implement your own logarithm, consider first your requirements. If integer logarithm [suffices](good_enough.md), the straightforward "[brute force](brute_force.md)" way of searching for the correct result in a for loop is quite usable since the number of iterations can't get too high (as by repeated exponentiation we quickly cover the whole range of even 64 bit integers). In C this may be done as follows (we have to watch out for [overflows](overflow.md) that could get us stuck in an infinite loop; this could also be addressed by using division instead of multiplication, but division can be very slow):
```
unsigned int logIntN(unsigned int base, unsigned int x)
@ -189,4 +189,4 @@ As for [approximations](approximation.md): unfortunately good ones are often pla
It's not very precise but the advantage is that it looks reasonable on a wide interval from 0 up to many thousands: before *x* gets to higher hundreds the error is somewhere around 3%, then around 2000 gets to some 10% and around 10000 to approx 20% where it then seems to stay for a very long time.
If you have the *[pow](pow.md)* (or even just *exp*) function at hand (which can itself be approximated), you can probably use it to implement floating point logarithm also through [binary search](binary_search.md) with delta, Newton's method or something similar.
Should you have the *[pow](pow.md)* (or even just *exp*) function at hand (which can itself be approximated), you could likely use it to implement floating point logarithm also through [binary search](binary_search.md) with delta, Newton's method or something similar.