sqrt(x)和pow(x,0.5)的差异

时间:2022-09-06 16:32:52

I was wondering why there is sqrt() function in C/c++ as we can achieve the same using

我想知道为什么在C/c++中有sqrt()函数,因为我们可以实现相同的使用。

pow(x,0.5);

how is sqrt(x) different for pow(x,0.5) . Is there a specific reason of having sqrt function?

对于pow(x,0.5), sqrt(x)是如何不同的。是否有一个特定的原因导致sqrt函数?

3 个解决方案

#1


10  

I ran a test for you to check the performance of sqrt(x) and pow(x,0.5)

我运行了一个测试来检查sqrt(x)和pow(x,0.5)的性能

1.

1。

for(int i=0;i<100000000;i++) 
    pow(double(i),0.5);

2.

2。

for(int i=0;i<100000000;i++)
    sqrt(double(i));  

1st one took around 20 seconds where as 2nd one took around 2 seconds on my computer. So performance is way better. As other have already mentioned readability is other reason.

第一个花了20秒,第二个在我的电脑上花了2秒钟。所以性能更好。正如其他已经提到的可读性是另一个原因。

#2


5  

Sure, if you think of only the mathematical equivalence...

当然,如果你只考虑数学等价…

But in terms of algorithms to compute the result, sqrt is specific to one thing whereas pow is generic.

但是在计算结果的算法方面,sqrt是特定的,而pow是通用的。

So you could (rightly) assume that it's possible to write a faster function for sqrt than it is to write the generic pow function.

因此,你可以(正确地)假设,为sqrt编写一个更快的函数,比编写通用的pow函数是可能的。

#3


5  

I remember reading somewhere that sqrt() is a special case that is guaranteed by the IEEE specification to be rounded correctly. I'll look that up to find a source. It should be a little faster too, because it only has to handle one case.

我记得在某个地方读到sqrt()是一种特殊的情况,它是由IEEE规范保证正确的。我要找个来源。它也应该快一点,因为它只需要处理一个情况。

Even if they were the same, it's nice to have a builtin alias for a commonly used function!

即使它们是相同的,有一个常用函数的内置别名也很好!

Edit: According to the IEEE-754, both the pow() function and sqrt() are supposed to be implemented such that the rounded value is the closest possible floating point representation to the real value. However, sqrt() should still be faster.

编辑:根据IEEE-754, pow()函数和sqrt()都应该被实现,这样圆的值是最接近实际值的浮点数表示。但是,sqrt()仍然应该更快。

#1


10  

I ran a test for you to check the performance of sqrt(x) and pow(x,0.5)

我运行了一个测试来检查sqrt(x)和pow(x,0.5)的性能

1.

1。

for(int i=0;i<100000000;i++) 
    pow(double(i),0.5);

2.

2。

for(int i=0;i<100000000;i++)
    sqrt(double(i));  

1st one took around 20 seconds where as 2nd one took around 2 seconds on my computer. So performance is way better. As other have already mentioned readability is other reason.

第一个花了20秒,第二个在我的电脑上花了2秒钟。所以性能更好。正如其他已经提到的可读性是另一个原因。

#2


5  

Sure, if you think of only the mathematical equivalence...

当然,如果你只考虑数学等价…

But in terms of algorithms to compute the result, sqrt is specific to one thing whereas pow is generic.

但是在计算结果的算法方面,sqrt是特定的,而pow是通用的。

So you could (rightly) assume that it's possible to write a faster function for sqrt than it is to write the generic pow function.

因此,你可以(正确地)假设,为sqrt编写一个更快的函数,比编写通用的pow函数是可能的。

#3


5  

I remember reading somewhere that sqrt() is a special case that is guaranteed by the IEEE specification to be rounded correctly. I'll look that up to find a source. It should be a little faster too, because it only has to handle one case.

我记得在某个地方读到sqrt()是一种特殊的情况,它是由IEEE规范保证正确的。我要找个来源。它也应该快一点,因为它只需要处理一个情况。

Even if they were the same, it's nice to have a builtin alias for a commonly used function!

即使它们是相同的,有一个常用函数的内置别名也很好!

Edit: According to the IEEE-754, both the pow() function and sqrt() are supposed to be implemented such that the rounded value is the closest possible floating point representation to the real value. However, sqrt() should still be faster.

编辑:根据IEEE-754, pow()函数和sqrt()都应该被实现,这样圆的值是最接近实际值的浮点数表示。但是,sqrt()仍然应该更快。