Are there any definitions of functions like sqrt()
, sin()
, cos()
, tan()
, log()
, exp()
(these from math.h/cmath) available ?
I just wanted to know how do they work.
Join Stack Overflow to learn, share knowledge, and build your career.
Are there any definitions of functions like sqrt()
, sin()
, cos()
, tan()
, log()
, exp()
(these from math.h/cmath) available ?
I just wanted to know how do they work.
This is an interesting question, but reading sources of efficient libraries won't get you very far unless you happen to know the method used.
Here are some pointers to help you understand the classical methods. My information is by no means accurate. The following methods are only the classical ones, particular implementations can use other methods.
sincos
function.atan2
is computed with a call to sincos
and a little logic. These functions are the building blocks for complex arithmetic.exp
and log
because multiplication and addition are so much faster than division on modern hardware. It is still used for stiffer functions, however.
– Stephen Canon
Dec 27 '10 at 23:46
Every implementation may be different, but you can check out one implementation from glibc's (the GNU C library) source code.
edit: Google Code Search has been taken offline, so the old link I had goes nowhere.
The sources for glibc's math library are located here:
Have a look at how glibc
implements various math functions, full of magic, approximation and assembly.
Definitely take a look at the fdlibm sources. They're nice because the fdlibm library is self-contained, each function is well-documented with detailed explanations of the mathematics involved, and the code is immensely clear to read.
Having looked a lot at math code, I would advise against looking at glibc - the code is often quite difficult to follow, and depends a lot on glibc magic. The math lib in FreeBSD is much easier to read, if somehow sometimes slower (but not by much).
For complex functions, the main difficulty is border cases - correct nan/inf/0 handling is already difficult for real functions, but it is a nightmare for complex functions. C99 standard defines many corner cases, some functions have easily 10-20 corner cases. You can look at the annex G of the up to date C99 standard document to get an idea. There is also a difficult with long double, because its format is not standardized - in my experience, you should expect quite a few bugs with long double. Hopefully, the upcoming revised version of IEEE754 with extended precision will improve the situation.
ldexp
on MSVC renders the function pretty much useless for instance)
– Alexandre C.
Jan 5 '12 at 19:53
Most modern hardware include floating point units that implement these functions very efficiently.
Usage: root(number,root,depth)
Example: root(16,2) == sqrt(16) == 4
Example: root(16,2,2) == sqrt(sqrt(16)) == 2
Example: root(64,3) == 4
Implementation in C#:
double root(double number, double root, double depth = 1f)
{
return Math.Pow(number, Math.Pow(root, -depth));
}
Usage: Sqrt(Number,depth)
Example: Sqrt(16) == 4
Example: Sqrt(8,2) == sqrt(sqrt(8))
double Sqrt(double number, double depth = 1) return root(number,2,depth);
By: Imk0tter
Those are almost always implemented as system calls. If you want to look at the sources, you'd need access to the OS sources, which means you need to look at an open-source OS like Linux or BSD.
sin
as a syscall? That'd be quite a waste of kernel space, unless the OS in question is extremely graphical -- and even then, it'd be slower than a RTL implementation due to context switching. There are instructions that can calculate sin, cos, etc, but syscalls? I doubt it.
– cHao
Dec 27 '10 at 19:19