Can logl and sqrtl be hacked?

Revision en2, by Phantom_Dreams, 2024-08-20 08:45:47

Say you wanted to calculate the floored square root of a number in C++, and you do: (int)sqrtl(x).

Or you wanted to calculate the floored base b logarithm of some number, and you do: (int)(logl(x)/logl(b)).

Could either of the 2 produce incorrect results due to precision errors?

I would assume since long double has 64 significant bits, both of the cases above should work correctly for all possible 64-bit integer values of x, but I am a bit skeptical of the logarithm, since there is an extra division step.

And if they are unreliable, what are good alternatives?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Phantom_Dreams 2024-08-20 08:45:47 3 Tiny change: 'logarithm some numb' -> 'logarithm of some numb'
en1 English Phantom_Dreams 2024-08-20 08:44:50 611 Initial revision (published)