3

I am just wondering where and how functions like sine, cosine and sqrt are implemented inside cmath. I know cmath includes math.h and math.h has a bunch of stuff but where is the actual "math" behind some of these functions?

EDIT: I am more interested in whether there is tangible source code associated with math functions or are those headers the lowest I am going to go.

Dave
  • 7,283
  • 12
  • 55
  • 101

4 Answers4

3

The math for those operations is almost certainly not done in C code, at least for x86 architectures. It is done on chip. Ever since the integration of the 8087 math co-processor into the 486, the x86 family has had instructions on the chip to do sine, cosine, and square root, among others. This page has a nice roundup.

Edit:

Just found possible duplicates at How does C compute sin() and other math functions?, and How do Trigonometric functions work?.

Community
  • 1
  • 1
EmeryBerger
  • 3,897
  • 18
  • 29
  • On chip implementations use same mathematical formula. The question asks for the "math" behind the calculations, not whether they are done in CPU or FPU – Sarwar Erfan Jan 13 '11 at 05:28
  • I assumed that when the poster wrote "where is the actual math" that he did not mean "what is the actual math." – EmeryBerger Jan 13 '11 at 05:31
  • The word math is put inside quote in the question. Anyways... It does not matter... It is not a very vital issue and if Dave gets answer from any of the answers or comments, the purpose is served – Sarwar Erfan Jan 13 '11 at 05:41
  • Maybe its my bad wording again but what I really wanted to know is where is the math stored. Either in code in some c file or does the compiler do magic in order for the CPU to do it. – Dave Jan 13 '11 at 05:43
  • On x86, the implementation is an assembly language instruction that has the CPU actually do the work (for example, to compute sin, it would invoke fsinf). – EmeryBerger Jan 13 '11 at 05:44
1

The functions are probably only declared in the header, whether that's <cmath> or <math.h>. The implementation is normally in library functions, which have to deal with all sorts of weird stuff before settling down to the real calculation. See Plauger's slightly dated but still interesting book, 'The Standard C Library', for a discussion of some of the issues involved in implementing a good maths library.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

I once interested in the same thing as you are now, and finally I found a C reference implementation of these Math functions. This is great not only because of the simplicity of the codes and also rich and complete comments. I hope you would like it as much as I do
These algorithms are available from the well-known network library netlib as the package "Freely Distributable Math Library," fdlibm.
Read e_sqrt.c first. That's what you asked for.

Haozhun
  • 6,331
  • 3
  • 29
  • 50
0

Most of them are done using Taylor series http://en.wikipedia.org/wiki/Taylor_series

Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57
  • 1
    I expect that is not true, at least for on-chip implementations. – EmeryBerger Jan 13 '11 at 05:25
  • The question is how this is computed, not whether this is done by software or hardware. On chip implementations use same mathematical formula. The question asks for the "math" behind the calculations, not whether they are done in CPU or FPU – Sarwar Erfan Jan 13 '11 at 05:27
  • 1
    Incorrect. See the comments on http://stackoverflow.com/questions/2284860/how-does-c-compute-sin-and-other-math-functions, especially the part about CORDIC. In short, interpolation. – EmeryBerger Jan 13 '11 at 05:32