C 中的 pow 函数在哪里定义和实现?
我读到 pow(double, double) 函数是在“math.h”中定义的,但我找不到它的声明。
有谁知道这个函数在哪里声明的?它是在哪里用C实现的?
参考:
http://publications.gbdirect.co.uk/c_book/chapter9/ maths_functions.html
I read that the pow(double, double) function is defined in "math.h" but I can't find its declaration.
Does anybody know where this function declared? And where is it implemented in C?
Reference:
http://publications.gbdirect.co.uk/c_book/chapter9/maths_functions.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
通常,诸如
这样的包含文件会包含其他头文件,这些头文件实际上声明了您希望在
中看到的函数。 。这个想法是,当程序包含
时,它会得到它所期望的结果,即使实际的函数定义位于其他一些头文件中。找到标准库函数(例如
pow()
)的实现是另一回事。您必须将源代码挖掘到 C 标准运行时库并在那里找到实现。Quite often, an include file such as
<math.h>
will include other header files that actually declare the functions you would expect to see in<math.h>
. The idea is that the program gets what it expects when it includes<math.h>
, even if the actual function definitions are in some other header file.Finding the implementation of a standard library function such as
pow()
is quite another matter. You will have to dig up the source code to your C standard runtime library and find the implementation in there.它的定义位置取决于您的环境。该代码位于某个已编译的 C 标准库内。
它的“定义”位于您的 c 标准库发行版的源代码中。一种这样的发行版是eglibc。这可以在线浏览,也可以在源发行版中浏览:
第 Branches/eglibc-2_10/libc/math/math_private.h?rev=8421&view=markup" rel="nofollow noreferrer" title="math_private.h">math_private.h
简短回答:在 C 标准中库源代码。
Where it's defined depends on your environment. The code is inside a compiled C standard library somewhere.
Its "definition" is in the source code for your c standard library distribution. One such distribution is eglibc. This is browsable online, or in a source distribution:
w_pow.c
math_private.h
Short answer: In the C standard library source code.
pow
的实际实现可能因编译器而异。一般来说,math.h(或 math.h 包含的特定于供应商的文件)为pow
(即其声明)提供原型,但实现隐藏在一些库文件,例如 libm.a。根据您的编译器的不同,pow
或任何其他库函数的实际源代码可能不可用。The actual implementation of
pow
may vary from compiler to compiler. Generally, math.h (or a vendor-specific file included by math.h) provides the prototype forpow
(i.e., its declaration), but the implementation is buried in some library file such as libm.a. Depending on your compiler, the actual source code forpow
or any other library function may not be available.声明:在系统/SDK的包含目录中(例如:/usr/include;/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk/usr/include/architecture /arm/math.h)
定义(实现):
pow 函数声明中的 extern 关键字表示它已定义其他地方。数学函数是低级高性能实现,主要以汇编代码 (*.s) 完成。汇编例程(通过寄存器/堆栈获取参数/给出参数)与 C 库的其余部分链接。函数/例程名称的链接/导出是特定于平台的,如果目标不是深入汇编编码,则并不重要。
我希望这有帮助,
拉斐尔
declared: in the include directory of your system/SDK (e.g.: /usr/include;/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk/usr/include/architecture/arm/math.h)
defined (implemented):
The extern keyword in the function declaration of pow means, that it's defined somewhere else. Math functions are low-level high-performance implementations mostly done in assembly code (*.s). The assembly routines (taking the arguments/giving the parameters via registers/stack) are linked with the rest of the C library. The linking/exporting of the function/routine names is platform specific and doesn't really matter if ones goal is not dive into assembly coding.
I hope this helped,
Raphael
如果您正在寻找计算的实现方式,可以在这里找到:
http://fossies.org/dox/gcc-4.5.3/e__pow_8c_source.html
函数的名称是 __ieee754_pow
这是由 pow 函数调用的。
If you are seeking how the calculation is implemented, you can find it here:
http://fossies.org/dox/gcc-4.5.3/e__pow_8c_source.html
The name of the function is __ieee754_pow
which is called by pow function.
I 实际上是在
math.h
中定义的。您是否尝试过包含math.h
并简单地使用pow
? “找不到”是什么意思?I’s really defined in
math.h
. Have you tried includingmath.h
and simply usingpow
? What do you mean by “can't find it”?下面是 fdlibm 的 C 实现: http://www.netlib.org/fdlibm/e_pow.c
无论如何,当 v8 删除其余弦/正弦表时,它是从 fdlibm 的实现中提取出来的:https://code.google.com/p/v8/source/detail?r=22918
来自更改提交注释:“使用 fdlibm 端口实现三角函数”。
另一方面,Mozilla 调用 cstdlib 数学函数,该函数将根据构建和系统而具有不同的性能(例如:可能会也可能不会调用先验函数的芯片级实现)。虽然 C# 字节码似乎在可能的情况下显式引用芯片级函数。然而,“pow”不是其中之一,iirc(似乎没有芯片级功能)并且在其他地方实现。
另请参阅:https://bugzilla.mozilla.org/show_bug.cgi?id=967709
对于 Mozilla 社区中的余弦/正弦讨论,比较 Mozilla 的实现与旧的 v8 实现。
另请参阅:Math.Pow() 在 .NET Framework 中如何实现?
内在函数是芯片级的,实际上是在处理器上实现的。 (我们不一定再需要查找表。)
Here's a C implementation for fdlibm: http://www.netlib.org/fdlibm/e_pow.c
For what it's worth, when v8 dropped its cos/sine tables, it pulled from fdlibm's implementation to do so: https://code.google.com/p/v8/source/detail?r=22918
From the change commit comments: "Implement trigonometric functions using a fdlibm port."
Mozilla on the other hand calls the cstdlib math functions, which will have variable performance by build and system (ex: may or may not invoke the chip-level implementations of transcendental functions). While C# bytecode seems to make explicit references to chip-level functions when it can. However, "pow" is not one of those, iirc (doesn't seem to have an chip-level function) and is implemented elsewhere.
See also: https://bugzilla.mozilla.org/show_bug.cgi?id=967709
For a cos/sine discussion in the Mozilla community, comparison of Mozilla's implementation vs old v8 implementation.
See also: How is Math.Pow() implemented in .NET Framework?
Intrinsic functions are chip-level, actually implemented on the processor. (We don't necessarily need lookup tables any more.)
其此处以及此处。
也可以去 wikipedia
你会在那里找到 pow 。
Its here and also here.
Also go on wikipedia
You will find pow there.
这是 powq 的实现 GNU gcc-13。
实现的核心思想在代码的最上面:
注意到我们经常使用的
pow
是由硬件或libm
实现的,C编译器只是为它们起了一个别名。Here is an implement of powq in GNU gcc-13.
The core idea of the implement is at the top of the code:
Noted that
pow
we use more often is implemented by hardware orlibm
, C compiler only make an alias for them.