Where is pow function defined and implemented in C

2019-02-18 14:14发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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



回答5:

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”?



回答6:

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.



回答7:

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



回答8:

Its here and also here. Also go on wikipedia

You will find pow there.