Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var sines = []; //create empty array
- function sine(angle) // angle in degrees 0 to 179
- {
- if (angle in sines) // got it already?
- {
- document.write("Cached ..");
- return sines[angle]; // return it
- }
- else
- { // convert to radians
- var x = angle * Math.PI / 180;
- // Maclaurin series
- var sum = x;
- numerator = x;
- denom = 1;
- for (n = 3; n < 11; n += 2)
- {
- numerator = -numerator * x * x;
- denom = denom * (n - 1) * n;
- term = numerator / denom;
- sum += term;
- }
- sines[angle] = sum; // put in cache
- return sum; // and return
- }
- }
- // Try it out
- for (time = 1; time < 3; time++)
- for (d = 0; d < 10; d++)
- document.write(d + " " + sine(d) + "<br>");
Advertisement
Add Comment
Please, Sign In to add comment