RadioNurshat

Pi Calculation

Sep 27th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <math.h>
  4. using namespace std;
  5. double calculate(double epsilon) {
  6.     double last = sqrt(0.5);
  7.     double current = sqrt(0.5 + (0.5 * last));
  8.     double result = last * current;
  9.     while (abs(current - last) >= epsilon) {
  10.         last = current;
  11.         current = sqrt(0.5 + (0.5 * current));
  12.         result = result * current;
  13.     }  
  14.     return result;
  15. }
  16. int main()
  17. {
  18.     double epsilon;
  19.     cin >> epsilon;
  20.     cout << 4.0/calculate(epsilon);
  21.     system("pause");
  22. }
  23.  
  24.  
Add Comment
Please, Sign In to add comment