Advertisement
OIQ

Untitled

OIQ
Sep 12th, 2021
836
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <math.h>
  5. #include <iomanip>
  6.  
  7. // #define pi 3.14159265
  8.  
  9. using namespace std;
  10. using ll = long long;
  11.  
  12. ll factorial(ll x) {
  13.     if (x <= 1)
  14.         return 1;
  15.     return x * factorial(x - 1);
  16. }
  17.  
  18. int getPers(double e) {
  19.     int count = 0;
  20.     while (e < 1) {
  21.         e *= 10;
  22.         count++;
  23.     }
  24.     return count;
  25. }
  26.  
  27.  
  28. int main() {
  29.     long double num, e;
  30.     long double pi = 3.14159265;
  31.     cin >> num >> e;
  32.  
  33.     int sign = 1;
  34.     num = fmod(num, 2 * pi);
  35.  
  36.     //while (num - 2 * pi > pi / 2)
  37.     //    num -= 2 * pi;
  38.     //while (num + 2 * pi < - pi / 2)
  39.     //    num += 2 * pi;
  40.  
  41.     if (num < 0.0) {
  42.         sign *= -1;
  43.         num = num * -1;
  44.     }
  45.  
  46.     if (num > pi / 2) {
  47.         num = pi - num;
  48.         // sign = -1;
  49.     }
  50.  
  51.     long double result = 0;
  52.     int iter = 0;
  53.     long double next = num;
  54.  
  55.     while (true) {
  56.         result += next;
  57.         // result += pow(-1, iter) * pow(num, iter * 2 + 1) / factorial(1LL * iter * 2 + 1);
  58.         iter++;
  59.         // long double next = pow(-1, iter) * pow(num, iter * 2 + 1) / factorial(1LL * iter * 2 + 1);
  60.         next *= num * -num / ((2.0 * iter) * (2.0 * iter + 1.0));
  61.         if (fabs(next) <= e)
  62.             break;
  63.     }
  64.     // cout << fixed;
  65.     cout << setprecision(-log10(e));
  66.     //if (fabs(result) <= e) {
  67.     //    cout << 0 << "\n";
  68.     //}
  69.     //else
  70.         cout << sign * result << "\n";
  71.     cout << iter;
  72. }
  73.  
Advertisement
RAW Paste Data Copied
Advertisement