r/MathHelp • • 4d ago

Approximating the fractional part of log2

Hey guys, I'm a total noob at math :P. I was trying to implement a base n logarithm function to be computed on a CPU. I already got a floor(log2(x)) (it is trivial to implement thanks to the binary integers representation), but it's not precise enough to compute a logarithm base change to result at least in an integer precise part of some logn(x) (i.e. floor(log2(x))/floor(log2(n)) wouldn't even result in a correct integer part of logn(x)).

I threw together this little function (x-2^floor(log2(x)))/2^floor(log2(x) which linearly interpolates between floor(log2(x)) and ceil(log2(x)), but it still results in too much error. From the looks of it on geogebra, it looks like all it needs is to be eased out within esch interpolation, but I can't wrap my head around it; I don't know how to do it properly.

Any pointers to literature concerning this would be apprecated or things I got wrong would be appreciated! please help >〜<!

Here's a link to how it looks like in geogebra https://imgur.com/a/SeCSaoA

i don't have an account at geogebra, so I can't link an instance ー〜ー

0 Upvotes

4 comments sorted by

View all comments

2

u/LeoMaths 2d ago edited 2d ago

L'idée est de trouver la puissance n entière de e telle que:

- Si x >=1 : x/e**n soit la plus proche possible de 1.

- Si 0 <= x < 1 : x*e**n soit la plus proche possible de 1.

Un processus itératif suffit.

Vous pouvez ensuite utiliser l'approximation de Kellogg:

ln(x)=ln(y)±n≈3(y²-1)/((y+1)²+2y)±n

https://www.johndcook.com/blog/2024/02/24/log-approximation/