function without a body!

closed account (28poGNh0)
We probably all know that sqrt() returns the square root of its argument,I found its prototype within math.h header but does anyone knows where is the body(or implementation) of this functions
and if not exist than how it works(or how it does its job)
closed account (N36fSL3A)
It's inside the C++ dynamicly linked libraries (.dlls) that are downloaded with the compiler.

That's why some programs require c++ redistributables.
closed account (28poGNh0)
so how can I know the body contents of this function?
I do my own functions that do same as c/c++ library function like(strcmp,max,..)
but I wich to get the original implementation of these functions
so is there anyway to seek that?
closed account (N36fSL3A)
Erm, not that I know of. A quick google search bought me this.

X:\Microsoft Visual Studio 9.0\VC\crt\src
Something like that.
You should be able to find the source for the sqrt function in any of the open source C++ libraries.

From
http://stackoverflow.com/questions/9572748/how-to-check-code-inside-math-library-function-sqrt-using-dev-c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <math.h>
#include <errno.h>

extern long double  __QNANL;

long double
sqrtl (long double x)
{
  if (x < 0.0L )
    {
      errno = EDOM;
      return __QNANL;
    }
  else
    {
      long double res;
      asm ("fsqrt" : "=t" (res) : "0" (x));
      return res;
    }
}
closed account (28poGNh0)
Thanks both of you
@Lumpkin The tresor in my house but I can find it google does it again,and you're awsome(fast replies)


@AbstractionAnon
for C++ libraries ,are they all open source ?
Last edited on
If you are looking for an algorithm it isn't difficult to find the square root using an iterative process. Repeat this as required:
estimate = ((n/estimate) + estimate) * 0.5;

It is necessary to (a) choose an initial value for estimate and (b) decide when to terminate.
for C++ libraries ,are they all open source ?

No, Microsoft VC++ libraries are not open source.
GNU G++ (mingw) is open source.
closed account (28poGNh0)
Thanks all of you for the replies top as always
Even though it's not open source licensed, most of the source for the Microsoft CRT is provided with Visual Studio. But the source for the maths functions isn't provided; grep only finds sqrt in headers and in four libraries, all called tran.lib, which I have a suspicion come from hand-written assembly code rather than C++.

(This is for a paid for version of Visual Studio 2008; I only have Express editions of 2010 and 2012, and the libraries aren't included with them.)

But note that when you use Visual C++'s default compile options for a release build, sqrt doesn't come from any library, dynamic or static. It's one of a number of functions which is provided as an intrinsic (or built-in) function. That is, the compiler know what sequence of assembly code is needed for the function and just pastes it directly into your executable.

Compiler Intrinsics
http://msdn.microsoft.com/en-us/library/26td21ds.aspx

Andy

PS GCC does the same thing by default (for optimized builds) as I expect all decent compilers to do.

6.55 Other Built-in Functions Provided by GCC
http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#Other-Builtins
Last edited on
closed account (28poGNh0)
Thank for the great explanation @andywestken
Topic archived. No new replies allowed.