Advertisement
desdemona

matko boska jaka dokladnosc, jogut złoty 1,27!

Mar 5th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #define _USE_MATH_DEFINES
  4. #include <cmath>
  5. #include <stack>
  6. #define M_PI_2     1.57079632679489661923
  7.  
  8. using namespace std;
  9.  
  10. int sgn(long double x)
  11. {
  12.     if(x>0)
  13.         return 1;
  14.     else
  15.         return -1;
  16. }
  17.  
  18. void licz_double(long double x, long double epsilon)
  19. {
  20.     if(x <= 1 && x >= (-1))
  21.     {
  22.         long double suma = x;
  23.         long double potega = x;
  24.         bool p = 0;
  25.         long double nastepny = 1000.0;
  26.         stack<long double> stos_sum;
  27.         stos_sum.push(suma);
  28.         for(int n = 1; nastepny >= epsilon; n+=1)
  29.         {
  30.             potega = potega * x * x;
  31.        
  32.             if(p==1)
  33.             {
  34.                 suma = suma + (potega/(2*n + 1.0));
  35.                 stos_sum.push(potega/(2*n + 1.0));
  36.             }
  37.             else
  38.             {
  39.                 suma = suma - (potega/(2*n + 1.0));
  40.                 stos_sum.push(-potega/(2*n + 1.0));
  41.             }
  42.             p = !p;
  43.             nastepny = ((potega*x*x)/(2*(n+1) + 1.0));
  44.  
  45.            
  46.         }
  47.         long double arctan = atan(x);
  48.         long double wynik =0.0;
  49.         int size = stos_sum.size();
  50.         for(int i=0; i <size; i++)
  51.         {
  52.             wynik = wynik + stos_sum.top();
  53.             stos_sum.pop();
  54.         }
  55.         cout << "kolejnosc dodawania od najmniejszych do najwiekszych\n";
  56.         cout << "argument:\t"<< x << "\n";
  57.         cout << "wynik obliczen:\t"<< wynik << "\n";
  58.         cout << "wynik atan stl:\t"<< arctan << "\n";
  59.         cout << "blad wzgledny:\t" << (wynik-arctan)/arctan << "\n\n";
  60.         cout << "kolejnosc dodawania od najwiekszych do najmniejszych\n";
  61.         cout << x << "\t" << suma << "\t" << arctan << "\t" << (suma-arctan)/arctan << "\n\n";
  62.         //cout << x << "\n";
  63.  
  64.     }
  65.     else
  66.     {
  67.         long double suma = (sgn(x) * M_PI_2) - (1/x);
  68.         long double potega = x;
  69.         bool p = 1;
  70.  
  71.         for(int n =1; n<1000; n+=1)
  72.         {
  73.             potega = potega * x *x;
  74.             if(p==1)
  75.                 suma = suma + 1/((2*n + 1)*potega);
  76.             else
  77.                 suma = suma - 1/((2*n + 1)*potega);
  78.             p = !p;        
  79.         }
  80.         long double arctan = atan(x);
  81.         cout << x << "\t" << suma << "\t" << arctan << "\t" << (suma-arctan)/arctan << "\n" << fixed;
  82.         //cout << x << "\n";
  83.     }
  84. }
  85.  
  86. void licz_float(float x, long double epsilon)
  87. {
  88.     if(x <= 1 && x >= (-1))
  89.     {
  90.         float suma = x;
  91.         float potega = x;
  92.         bool p = 0;
  93.         long double nastepny = 1000.0;
  94.         for(int n = 1; nastepny >= epsilon; n+=1)
  95.         {
  96.             potega = potega * x * x;
  97.        
  98.             if(p==1)
  99.                 suma = suma + (potega/(2*n + 1.0));
  100.             else
  101.                 suma = suma - (potega/(2*n + 1.0));
  102.             p = !p;
  103.             nastepny = ((potega*x*x)/(2*(n+1) + 1.0));
  104.            
  105.         }
  106.         float arctan = atan(x);
  107.         cout << x << "\t" << suma << "\t" << arctan << "\t" << fabs((suma-arctan)/arctan) << "\n" << fixed;
  108.         //cout << x << "\n";
  109.     }
  110.     else
  111.     {
  112.         float suma = (sgn(x) * M_PI_2) - (1/x);
  113.         float potega = x;
  114.         bool p = 1;
  115.  
  116.         for(float n =1.0; n<1000.0; n+=1.0)
  117.         {
  118.             potega = potega * x *x;
  119.             if(p==1)
  120.                 suma = suma + 1/((2*n + 1)*potega);
  121.             else
  122.                 suma = suma - 1/((2*n + 1)*potega);
  123.             p = !p;        
  124.         }
  125.         long double arctan = atan(x);
  126.         cout << x <<"\t" << suma << "\t" << arctan << "\t" << fabs((suma-arctan)/arctan) << "\n" << fixed;
  127.         //cout << x << "\n";
  128.     }
  129. }
  130.  
  131.  
  132. int main()
  133. {
  134.     cout << fixed;
  135.     long double x=0;
  136.     long double epsilon=0;
  137.     cout.precision(30);
  138.     cout << "arctg z zadana dokladnoscia\n";
  139.     cout << "podaj argument funkcji\n";
  140.    
  141.     cin >> x;
  142.     cout << "podaj zadana dokladnosc\n";
  143.     cin >> epsilon;
  144.  
  145.     licz_double(x, epsilon);
  146.     //licz_float((float)x,epsilon);
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement