使用 sqrt() 查找 log2()
这是我在某个网站上看到的面试题。
有人提到,答案涉及形成 log2() 的递归,如下所示:
double log2(double x )
{
if ( x<=2 ) return 1;
if ( IsSqureNum(x) )
return log2(sqrt(x) ) * 2;
return log2( sqrt(x) ) * 2 + 1; // Why the plus one here.
}
对于递归,显然 +1 是错误的。此外,基本情况也是错误的。 有谁知道更好的答案吗? log() 和 log10() 在 C 中实际是如何实现的。
This is an interview question I saw on some site.
It was mentioned that the answer involves forming a recurrence of log2() as follows:
double log2(double x )
{
if ( x<=2 ) return 1;
if ( IsSqureNum(x) )
return log2(sqrt(x) ) * 2;
return log2( sqrt(x) ) * 2 + 1; // Why the plus one here.
}
as for the recurrence, clearly the +1 is wrong. Also, the base case is also erroneous.
Does anyone know a better answer?
How is log() and log10() actually implemented in C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我已经找到了面试官想要的确切答案。就我而言,我想说在面试压力下得出这一点有点困难。这个想法是,假设您想找到
log2(13)
,您可以知道它位于 3 到 4 之间。另外,3 = log2(8) 和 4 = log2(16)
,从对数的性质,我们知道
log( sqrt( (8*16) ) = (log(8) + log(16))/2 = (3+4)/2 = 3.5
现在,sqrt(8*16) = 11.3137 和
log2(11.3137) = 3.5
。由于11.3137<13
,我们知道我们想要的 log2(13) 将位于 3.5 和 4 之间,并且我们继续找到它,很容易注意到它有一个二分搜索解决方案,并且我们迭代直到我们的值收敛到我们希望的log2()
的值。查找代码如下:Perhaps I have found the exact answers the interviewers were looking for. From my part, I would say it's little bit difficult to derive this under interview pressure. The idea is, say you want to find
log2(13)
, you can know that it lies between 3 to 4. Also3 = log2(8) and 4 = log2(16)
,from properties of logarithm, we know that
log( sqrt( (8*16) ) = (log(8) + log(16))/2 = (3+4)/2 = 3.5
Now,
sqrt(8*16) = 11.3137
andlog2(11.3137) = 3.5
. Since11.3137<13
, we know that our desired log2(13) would lie between 3.5 and 4 and we proceed to locate that. It is easy to notice that this has a Binary Search solution and we iterate up to a point when our value converges to the value whoselog2()
we wish to find. Code is given below:我已经很长时间没有写过纯 C 了,所以这里是用 C++ 写的(我认为唯一的区别是输出函数,所以你应该能够理解它):
函数 'mylog2' 做了一些简单的日志技巧获取 1 到 2 之间的相关数字,然后使用该数字调用 log2_aux。
log2_aux 或多或少遵循 Scorpi0 上面链接的算法。每一步,您都会得到 1 位结果。当你有足够的位时,停止。
如果你能拿到一份,《费曼物理学讲座》第 23 期,首先对对数进行了很好的解释,并或多或少地解释了如何进行这种转换。远远优于维基百科的文章。
It's been a long time since I've written pure C, so here it is in C++ (I think the only difference is the output function, so you should be able to follow it):
The function 'mylog2' does some simple log trickery to get a related number which is between 1 and 2, then call log2_aux with that number.
The log2_aux more or less follows the algorithm that Scorpi0 linked to above. At each step, you get 1 bit of the result. When you have enough bits, stop.
If you can get a hold of a copy, the Feynman Lectures on Physics, number 23, starts off with a great explanation of logs and more or less how to do this conversion. Vastly superior to the Wikipedia article.