I want to know how C
library implements the sqrt()
function. I got the header file math.h
in /usr/include
. But where is the actual implementation? I am doing a numerical computation project and would like to see the implementations of all the functions available in math.h
.
Asked
Active
Viewed 137 times
-3

Sourav Ghosh
- 133,132
- 16
- 183
- 261

user2277550
- 553
- 7
- 22
-
5`Googling gives no good results whatsoever`-- somehow I don't believe that. Pardon me. – Sourav Ghosh Jun 12 '15 at 09:31
-
1And see also [Implementation of ceil() and floor()](http://stackoverflow.com/questions/6208488/implementation-of-ceil-and-floor) – Paolo Gibellini Jun 12 '15 at 09:32
-
On linux: `man sqrt` tells me to link `-lm`, meaning `libm`, short for libmath. Most often these are part of glibc (GNU libc) which is the "standard" C library in most linux distributions. It's not likely that your distro by default provides the source for it installed on your computer, but a pre-compiled shared library. To get the source the best way is to go to the webpage for the library. Google `GNU C Library` and you shall find. – Jite Jun 12 '15 at 09:33
-
1Best advice is given in http://stackoverflow.com/a/5233569/2564301 – Jongware Jun 12 '15 at 09:33
-
While from your question it can be inferred that you're talking about Linux, it would be a good idea to tag your question appropriately so there's no room for doubt. – user4520 Jun 12 '15 at 09:48
1 Answers
1
You'll have a generic answer for your query in this SO thread.
However, to be specific to this question, to get the math related functions, you need to search libm
source code inside glibc
. That is present in the directory named math
.
An online browsable version is available here.

Community
- 1
- 1

Sourav Ghosh
- 133,132
- 16
- 183
- 261
-
The GNU C library **do** contain math functions, you just have to link them in specifically if needed. – Jite Jun 12 '15 at 09:41
-
@Jite Yeah, I updated the wording. I realized the wrong meaning after writing. Thanks. – Sourav Ghosh Jun 12 '15 at 09:42
-
Well the source code is still just in one repository so your wording is still a bit confusing. You'd more likely look for math functions in the math folder (although it might not be obvious exactly where stuff are implemented). – Jite Jun 12 '15 at 09:45
-
English is not my native language either, but I think in this case it's not about the language itself but rather that you try to hide a lack of knowledge by blaiming the language. As I wrote in my comment to the author the `man` pages are often a good start to find out where things come from. In this case it's most likely in the `glibc` source code, that can be found by googling. We now also know it's a math function and the source code contains a math folder, so it's most likely there. `libm` in this case is just a library object put together from certain pieces of glibc. – Jite Jun 12 '15 at 09:51
-
@Jite I did not blame the language, I know it's my fault not knowing the proper wordings and a very bad excuse (as you _may_ find) but it is what it is. However, I don't think we should be discussing it here anyway. I have updated my answer with a link to the same, if you kindly review and confirm, it will be helpful. Thank you fro your time. – Sourav Ghosh Jun 12 '15 at 09:58
-
It would be enough to say that he/she should search for the implementation in the glibc source folder and that math functions *most likely* (unless you know for sure) reside in the math folder. – Jite Jun 12 '15 at 10:01