views:

363

answers:

6

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%5Fbook/chapter9/maths%5Ffunctions.html

+1  A: 

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

Konrad Rudolph
Yes, I can use it but can't find its declaration. Can somebody find its declaration and implementation in math.h? Please post it here. thanks.
tsubasa
For example, isless function is declared as:# ifndef isless# define isless(x, y) \ (__extension__ \ ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \ !isunordered (__x, __y) }))# endifI expect similar thing that happens to pow(double, double)
tsubasa
A: 

Its here and also here. Also go on wikipedia

You will find pow there.

Ravi
+8  A: 

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.

Greg Hewgill
Note that most compilers do provide source to the library. But it can sometimes be quite a maze.
Michael Burr
+3  A: 

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.

pbos
A lot of hard-to-read code. :)
pbos
+1  A: 

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.

John Bode
+1  A: 

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

Raphael Schaad