如果你想知道实现pow
函数的具体方法,可以查看源代码。在陌生(且庞大)的代码库中查找所需部分有一定技巧,因此最好练习一下。
C库的一个实现是glibc,它在GitHub上有镜像。我没有找到官方镜像,但非官方的镜像在https://github.com/lattera/glibc
我们首先查看名为math/w_pow.c
的文件,这个名称很有前途。其中包含一个名为__pow
的函数,调用了__ieee754_pow
,我们可以在sysdeps/ieee754/dbl-64/e_pow.c
中找到它。(请记住,并非所有系统都符合IEEE-754标准,因此IEEE-754数学代码位于其自己的目录中)。
它从一些特殊情况开始:
if (y == 1.0) return x;
if (y == 2.0) return x*x;
if (y == -1.0) return 1.0/x;
if (y == 0) return 1.0;
稍微往下走,你会发现一个带有评论的分支。
/* if x<0 */
这就引出了我们接下来要谈论的内容
return (k==1)?__ieee754_pow(-x,y):-__ieee754_pow(-x,y); /* if y even or odd */
因此,对于负数x
和整数y
,glibc版本的pow
将计算pow(-x,y)
,如果y
为奇数,则将结果变为负数。
这不是唯一的做法,但我猜这在许多实现中都是常见的。您可以看到,pow
充满了特殊情况。这在库数学函数中很常见,这些函数应该能够正确处理不友好的输入,如非规格化数和无穷大。
pow
函数尤其难以阅读,因为它是经过大量优化的代码,可以对浮点数进行位操作。
C标准(n1548 §7.12.7.4)对pow
有以下规定:
如果x有限且为负数,y是有限的且不是整数值,则会发生域错误。
因此,根据C标准,负数x
应该有效。
还有附录F的问题,它对IEEE-754 / IEC-60559系统上的pow
如何工作给出了更严格的约束。
return y < 0 ? 1.0/0.0 : 0.0;
,这让我想到为什么要返回 1.0/0.0
? - Szymon1.0/0.0
表示无穷大。 - Dietrich Epp/* __ieee754_pow(x,y) return x**y
*
* n
* Method: Let x = 2 * (1+f)
* 1. Compute and return log2(x) in two pieces:
* log2(x) = w1 + w2,
* where w1 has 53-24 = 29 bit trailing zeros.
* 2. Perform y*log2(x) = n+y' by simulating muti-precision
* arithmetic, where |y'|<=0.5.
* 3. Return x**y = 2**n*exp(y'*log2)
*
* Special cases:
* 1. (anything) ** 0 is 1
* 2. (anything) ** 1 is itself
* 3. (anything) ** NAN is NAN
* 4. NAN ** (anything except 0) is NAN
* 5. +-(|x| > 1) ** +INF is +INF
* 6. +-(|x| > 1) ** -INF is +0
* 7. +-(|x| < 1) ** +INF is +0
* 8. +-(|x| < 1) ** -INF is +INF
* 9. +-1 ** +-INF is NAN
* 10. +0 ** (+anything except 0, NAN) is +0
* 11. -0 ** (+anything except 0, NAN, odd integer) is +0
* 12. +0 ** (-anything except 0, NAN) is +INF
* 13. -0 ** (-anything except 0, NAN, odd integer) is +INF
* 14. -0 ** (odd integer) = -( +0 ** (odd integer) )
* 15. +INF ** (+anything except 0,NAN) is +INF
* 16. +INF ** (-anything except 0,NAN) is +0
* 17. -INF ** (anything) = -0 ** (-anything)
* 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
* 19. (-anything except 0 and inf) ** (non-integer) is NAN
*
* Accuracy:
* pow(x,y) returns x**y nearly rounded. In particular
* pow(integer,integer)
* always returns the correct integer provided it is
* representable.
*
* Constants :
* The hexadecimal values are the intended ones for the following
* constants. The decimal values may be used, provided that the
* compiler will convert from decimal to binary accurately enough
* to produce the hexadecimal values shown.
*/
pow()
适用的特殊情况的枚举,还请参见ISO-C99标准第F.9.4.4节(或最新的ISO-C11标准相应部分)。 - njuffadouble pow(double base, double exp)
{
return exp2(exp * log2(base));
}
其中,exp2
和log2
是CPU原语,用于计算以2为底数的指数和对数操作。
不同的CPU天生具有不同的实现方式。
理论上,如果没有pow
函数,你可以这样写:
double pow(double base, double exponent)
{
return exp(exponent * log(base));
}
但是由于累积的舍入误差,这种方法失去了原生版本的精度。
Dietrich Epp指出我错过了很多特殊情况。尽管如此,我还是有一些关于舍入误差的话要说,这应该被允许存在。
base <= 0
时,对于一半的double
会失败 - 这并不是一个真正的“特殊”情况。 - chux - Reinstate Monicapow
对于负数也是有效的。只有当底数为负数且指数不是整数时,它才无法正常工作。
形如ax/y的数字实际上涉及到x的y次根。例如,当您尝试计算a1/2时,您实际上是在寻找a的平方根。
那么如果您有一个负底数和一个非整数指数会发生什么?您将获得一个负数的y次根,这将产生一个复杂的非实数。 pow()
不能使用复数,因此它可能会返回NaN。
pow
函数的精确计算方式是一个较长的故事,但它无法在底数为负且指数为非整数时进行计算,因为结果将会是一个复数,而该复数不能由pow
返回的double
类型表示。不过,可以使用cpow
函数来处理这种情况。 - Wintermuteinline
函数是一种特殊情况)。但我强烈怀疑您找不到实现!至少有一个FOSS C库可用。像“c pow implementation”这样的谷歌搜索词真的那么不常见吗? - too honest for this site-O3
这样的标志,结果可能是经过优化的 SSE/AVX 实现,可以通过反汇编二进制文件进行调查。 - Kh40tiK