waltermilner

Untitled

Mar 6th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var sines = []; //create empty array
  2.  
  3.     function sine(angle) // angle in degrees 0 to 179
  4.     {
  5.         if (angle in sines) // got it already?
  6.         {
  7.             document.write("Cached ..");
  8.             return sines[angle]; // return it
  9.         }
  10.         else
  11.         {   // convert to radians
  12.             var x = angle * Math.PI / 180;
  13.             // Maclaurin series
  14.             var sum = x;
  15.             numerator = x;
  16.             denom = 1;
  17.             for (n = 3; n < 11; n += 2)
  18.             {
  19.  
  20.                 numerator = -numerator * x * x;
  21.                 denom = denom * (n - 1) * n;
  22.                 term = numerator / denom;
  23.                 sum += term;
  24.             }
  25.             sines[angle] = sum; // put in cache
  26.             return sum; // and return
  27.         }
  28.  
  29.     }
  30.     // Try it out
  31.     for (time = 1; time < 3; time++)
  32.         for (d = 0; d < 10; d++)
  33.             document.write(d + " " + sine(d) + "<br>");
Advertisement
Add Comment
Please, Sign In to add comment