Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. # define PI 3.1415926
  5.  
  6. double currentPow = 1;
  7. int currentPowIndex = 0;
  8. double currentFactNumber = 1;
  9. int currentFactNumberIndex = 0;
  10.  
  11. double cached_pow(double number, int powIndex) {
  12. for (int i = currentPowIndex; i < powIndex; i++) {
  13. currentPow *= number;
  14. }
  15.  
  16. currentPowIndex = powIndex;
  17. return currentPow;
  18. }
  19.  
  20. double cached_fact (int factNumberIndex) {
  21. for (int i = factNumberIndex; i > currentFactNumberIndex; i--) {
  22. currentFactNumber *= i;
  23. }
  24.  
  25. currentFactNumberIndex = factNumberIndex;
  26. return currentFactNumber;
  27. }
  28.  
  29. int main() {
  30. double x = 120.0 / 180.0 * PI;
  31. double sinx = x;
  32. int sign = 1;
  33. double nextValue = 0.0;
  34. //877 iterations
  35. for (int i = 1; 0 != (nextValue = (cached_pow(x, 2*i + 1) / cached_fact(2*i + 1))); i++) {
  36. sign *= -1;
  37. sinx += sign * nextValue;
  38. }
  39.  
  40. cout << sinx << endl;
  41.  
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement