C 中的 pow 函数在哪里定义和实现?

发布于 2024-08-10 17:56:29 字数 318 浏览 7 评论 0原文

我读到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

七分※倦醒 2024-08-17 17:56:29

通常,诸如 这样的包含文件会包含其他头文件,这些头文件实际上声明了您希望在 中看到的函数。 。这个想法是,当程序包含 时,它会得到它所期望的结果,即使实际的函数定义位于其他一些头文件中。

找到标准库函数(例如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.

韶华倾负 2024-08-17 17:56:29

它的定义位置取决于您的环境。该代码位于某个已编译的 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.

神经大条 2024-08-17 17:56:29

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 for pow (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 for pow or any other library function may not be available.

り繁华旳梦境 2024-08-17 17:56:29

声明:在系统/SDK的包含目录中(例如:/usr/include;/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk/usr/include/architecture /arm/math.h)

定义(实现):

  • (编译后的二进制代码):位于系统/SDK 的库目录中(例如:/usr/ lib(对于数学库,它是 libm.dylib)
  • 作为源(程序代码):这是我现在在 Mac OS X 10.6.x 上工作的。 math.h 中声明的函数(例如: extern double pow ( double, double ); )未随安装一起提供(至少我找不到它)。您可能会在系统/SDK 的 C 库中找到这些源。就我而言,数学库(libm)是一个单独的项目,它的一些源由Apple提供:http://www.opensource.apple.com/tarballs/Libm/Libm-315.tar.gz

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):

  • as library (compiled, binary code): in the library directory of your system/SDK (e.g.: /usr/lib (in case of the math library it's libm.dylib)
  • as source (program code): this is the interesting part. I work on a Mac OS X 10.6.x right now. The sources for the functions declared in math.h (e.g.: extern double pow ( double, double ); ) are not shipped with the installation (at least I couldn't find it). You are likely to find those sources in your system/SDK's C library. In my case the math library (libm) is a separate project, some of its sources are provided by Apple: http://www.opensource.apple.com/tarballs/Libm/Libm-315.tar.gz

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

旧时光的容颜 2024-08-17 17:56:29

如果您正在寻找计算的实现方式,可以在这里找到:
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.

夏尔 2024-08-17 17:56:29

I 实际上是在 math.h 中定义的。您是否尝试过包含 math.h 并简单地使用 pow ? “找不到”是什么意思?

I’s really defined in math.h. Have you tried including math.h and simply using pow? What do you mean by “can't find it”?

小耗子 2024-08-17 17:56:29

下面是 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.)

彼岸花ソ最美的依靠 2024-08-17 17:56:29

此处以及此处
也可以去 wikipedia

你会在那里找到 pow 。

Its here and also here.
Also go on wikipedia

You will find pow there.

十级心震 2024-08-17 17:56:29

这是 powq 的实现 GNU gcc-13

实现的核心思想在代码的最上面:

 powq(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 113-53 = 60 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)

注意到我们经常使用的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:

 powq(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 113-53 = 60 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)

Noted that pow we use more often is implemented by hardware or libm, C compiler only make an alias for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文