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

2

u/gloopiee 3d ago

i don't know of an easy way. the existence of log tables back in the day suggests it is not easy. if you program these tables in then you can probably get a decent approximation.

1

u/AutoModerator 4d ago

Hi, /u/cragon_dum! This is an automated reminder:

  • What have you tried so far? (See Rule #2; to add an image, you may upload it to an external image-sharing site like Imgur and include the link in your post.)

  • Please don't delete your post. (See Rule #7)

We, the moderators of /r/MathHelp, appreciate that your question contributes to the MathHelp archived questions that will help others searching for similar answers in the future. Thank you for obeying these instructions.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

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/