The computations that a computer can perform
 

  • The computer (CPU) can only perform 4 arithemtic operations:

      • +, −, ×, /        

  • What kinds of functions can you compute using these arithmetic operations ?

    Answer:

      • Polynomial functions        

        E.g.: 3x4 − 6x3 + 2x + 9

How does a computer compute more complex functions, like "sin(x)" ?
 

  • Answer:

      • Interpolation !        

  • We can approximate any function using a polynomial to arbitrary accuracy

  • Popular method: Langrange polynomial

    Wikipedia: click here

A simple (but good) approximation for sin(x)

I found this simple approximation for sin(x) on the Internet:

  sin(x) ~= 0.775 * ( (4/π)*x + (4/π2)*x2 )
            + 0.225 * ( (4/π)*x + (4/π2)*x2 )2 

In C:

   #define pi 3.14159265358979l

   double mySine(double x)
   {
       const double B = 4.0/pi;    
       const double C = 4.0/(pi*pi);

       double y;

       y = B*x - C*x*x;             
       y = 0.775*y + 0.225*y*y;    
       return y;
   }

Accuracy of the simple approximation for sin(x)
 

List of function values for x=0 to x ~= 0.5π:

x = 0.0, sin(x) = 0.000000, mySine(x) = 0.000000, Diff = 0.000000 ( NaN%)
x = 0.1, sin(x) = 0.099833, mySine(x) = 0.098954, Diff = 0.000879 (0.88%)
x = 0.2, sin(x) = 0.198669, mySine(x) = 0.197580, Diff = 0.001089 (0.55%)
x = 0.3, sin(x) = 0.295520, mySine(x) = 0.294617, Diff = 0.000903 (0.31%)
x = 0.4, sin(x) = 0.389418, mySine(x) = 0.388895, Diff = 0.000524 (0.13%)
x = 0.5, sin(x) = 0.479426, mySine(x) = 0.479329, Diff = 0.000097 (0.02%)
x = 0.6, sin(x) = 0.564642, mySine(x) = 0.564926, Diff = -0.000284 (0.05%)
x = 0.7, sin(x) = 0.644218, mySine(x) = 0.644781, Diff = -0.000564 (0.09%)
x = 0.8, sin(x) = 0.717356, mySine(x) = 0.718077, Diff = -0.000721 (0.10%)
x = 0.9, sin(x) = 0.783327, mySine(x) = 0.784086, Diff = -0.000759 (0.10%)
x = 1.0, sin(x) = 0.841471, mySine(x) = 0.842168, Diff = -0.000697 (0.08%)
x = 1.1, sin(x) = 0.891207, mySine(x) = 0.891773, Diff = -0.000565 (0.06%)
x = 1.2, sin(x) = 0.932039, mySine(x) = 0.932438, Diff = -0.000399 (0.04%)
x = 1.3, sin(x) = 0.963558, mySine(x) = 0.963792, Diff = -0.000234 (0.02%)
x = 1.4, sin(x) = 0.985450, mySine(x) = 0.985549, Diff = -0.000099 (0.01%)
x = 1.5, sin(x) = 0.997495, mySine(x) = 0.997513, Diff = -0.000018 (0.00%)