Definitions of sqrt, sin, cos, pow, etc. In cmath

Are there any definitions of functions like sqrt() , sin() , cos() , tan() , log() , exp() (they are from math.h / cmath)?

I just wanted to know how they work.

+32
c ++ c math cmath
Dec 27 '10 at 19:10
source share
7 answers

This is an interesting question, but reading the sources of effective libraries will not lead you very far if you do not know which method was used.

Here are some pointers to help you understand classic methods. My information is in no way accurate. The following methods are only classic, other methods may be used in specific implementations.

  • Often used lookup tables
  • Trigonometric functions are often implemented using the CORDIC algorithm (either on the processor or in the library). Note that usually the sine and cosine are calculated together, I always wondered why the standard C library does not provide the sincos function.
  • Square roots use the Newton method with some clever implementation tricks: you can find somewhere on the Internet an excerpt from the Quake source code with the mind bogging 1 / sqrt (x).
  • Exponential and logarithms use exp (2 ^ nx) = exp (x) ^ (2 ^ n) and log2 (2 ^ nx) = n + log2 (x) to have an argument close to zero (one for log) and use approximation of rational function (usually Padé approximations ). Note that this exact trick can give you matrix exponents and logarithms. According to @Stephen Canon, modern implementations favor Taylor's expansion over the approximation of rational function, where division is much slower than multiplication.
  • Other functions may be inferred from these. Implementations may provide specialized procedures.
  • pow (x, y) = exp (y * log (x)), so pow should not be used when y is an integer
  • hypot (x, y) = abs (x) sqrt (1 + (y / x) ^ 2) if x> y (hypot (y, x) otherwise) to avoid overflow. atan2 computed by calling sincos and a bit of logic. These functions are the building blocks for complex arithmetic.
  • For other transcendental functions (gamma, erf, bessel, ...), please refer to the excellent book Numericical Recipes, 3rd edition for some ideas. Good'old Abramowitz and Stegun are also useful . There is a new version of http://dlmf.nist.gov/ .
  • More complex functions use methods such as the Chebyshev approximation, the continuation of continued fractions (actually associated with Padé approximations), or the saving of energy series (if, for example, you read the source code for erf, bessel, or gamma). I doubt that they really use simple simple math functions, but who knows. Consult numerical recipes for a review.
+58
Dec 27 '10 at 19:39
source share

Each implementation may differ, but you can check one implementation from the glibc file (GNU C libraries).

edit: Google Code Search was disabled, so the old link that I was in doesn't get there.

Sources for the glibc-math library are here:

http://sourceware.org/git/?p=glibc.git;a=tree;f=math;h=3d5233a292f12cd9e9b9c67c3a114c64564d72ab;hb=HEAD

+21
Dec 27 '10 at 19:14
source share

See how glibc implements various math functions full of magic, approximations, and assemblies.

+7
Dec 27 '10 at 7:16
source share

Definitely look at fdlibm sources. They are good because the fdlibm library is autonomous, each function is well documented with detailed explanations of the math involved, and the code is very readable.

+5
Dec 28 '10 at 1:45
source share

Having looked at the mathematical code a lot, I would advise against looking at glibc - the code is often quite difficult to succumb to, and much depends on the magic of glibc. FreeBSD's math lib is much easier to read if sometimes slower (but not much).

For complex functions, the main difficulty is the boundary cases - the proper handling of nan / inf / 0 is already difficult for real functions, but this is a nightmare for complex functions. The C99 standard defines many angular cases, some functions have easily 10-20 angular cases. You can refer to Appendix G of the updated standard document C99 for an introduction. There is also a complex one with a long double, because its format is not standardized - in my experience, you should expect quite a few errors with a long double. We hope that the new version of IEEE754 with enhanced accuracy will improve the situation.

+4
Dec 28 2018-10-12T00:
source share

Most modern hardware includes floating point modules that implement these features very efficiently.

0
Dec 28 '10 at
source share

Those are almost always implemented as system calls. If you want to see the sources, you need access to the OS sources, which means that you need to look for open-source OSs such as Linux or BSD.

-fifteen
Dec 27 '10 at 19:13
source share



All Articles