Advertisement
desdemona

taniej już chyba nie będzie

Mar 7th, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 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. using namespace std;
  8.  
  9. int sgn(long double x){
  10.     if(x>0)
  11.         return 1;
  12.     else
  13.         return -1;
  14. }
  15. void licz_double(long double x, long double epsilon){
  16.     long double suma = x;
  17.     stack<long double> stos_sum;
  18.     if(x <= 1 && x >= (-1)){
  19.         suma = x;
  20.         stos_sum.push(suma);
  21.         long double potega = x;
  22.         long double nastepny = 1000.0;
  23.         bool p=0;
  24.         for(int n = 1; nastepny > epsilon; n+=1){
  25.             potega = potega * x * x;   
  26.             if(p==1){
  27.                 suma = suma + (potega/(2*n + 1.0));
  28.                 stos_sum.push(potega/(2*n + 1.0));
  29.             }
  30.             else{
  31.                 suma = suma - (potega/(2*n + 1.0));
  32.                 stos_sum.push(-potega/(2*n + 1.0));
  33.             }
  34.             p = !p;
  35.             nastepny = abs((potega*x*x)/(2*(n+1) + 1.0));  
  36.         }
  37.     }
  38.     else{
  39.         suma = (sgn(x) * M_PI_2) - (1/x);
  40.         long double potega = x;
  41.         bool p = 1;
  42.         stos_sum.push(suma);
  43.         long double nastepny = 1000.0;
  44.         for(int n =1; nastepny >= epsilon; n+=1){
  45.             potega = potega * x *x;
  46.             if(p==1){
  47.                 suma = suma + 1/((2*n + 1)*potega);
  48.                 stos_sum.push(1/((2*n + 1)*potega));
  49.             }
  50.             else{
  51.                 suma = suma - 1/((2*n + 1)*potega);
  52.                 stos_sum.push(-1/((2*n + 1)*potega));
  53.             }
  54.             p = !p;    
  55.             nastepny = abs(1/((2*n + 3)*(potega*x*x)));
  56.         }
  57.     }
  58.         long double arctan = atan(x);
  59.         long double wynik =0.0;
  60.         int size = stos_sum.size();
  61.         for(int i=0; i <size; i++){
  62.             wynik = wynik + stos_sum.top();
  63.             stos_sum.pop();
  64.         }
  65.         cout << "wynik obliczen:\t"<< wynik << "\n";
  66.         cout << "blad wzgledny:\t" << (wynik-arctan)/arctan << "\n";       
  67. }
  68. int main(){
  69.     cout << fixed;
  70.     long double x=0;
  71.     long double epsilon=0;
  72.     cout.precision(30);
  73.  
  74.     cout << "arctg z zadana dokladnoscia\n";
  75.     cout << "podaj argument funkcji\n";
  76.     cin >> x;
  77.     cout << "podaj zadana dokladnosc\n";
  78.     cin >> epsilon;
  79.  
  80.     licz_double(x, epsilon);   
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement