Advertisement
desdemona

jogurty potaniały...

Mar 7th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 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. ofstream wyjscied;
  10.  
  11. int sgn(long double x)
  12. {
  13.     if(x>0)
  14.         return 1;
  15.     else
  16.         return -1;
  17. }
  18.  
  19. void licz_double(long double x, long double epsilon)
  20. {
  21.     long double suma = x;
  22.     stack<long double> stos_sum;
  23.     if(x <= 1 && x >= (-1))
  24.     {
  25.         suma = x;
  26.         stos_sum.push(suma);
  27.         long double potega = x;
  28.         long double nastepny = 1000.0;
  29.  
  30.         bool p=0;
  31.         for(int n = 1; nastepny > epsilon; n+=1)
  32.         {
  33.             potega = potega * x * x;
  34.        
  35.             if(p==1)
  36.             {
  37.                 suma = suma + (potega/(2*n + 1.0));
  38.                 stos_sum.push(potega/(2*n + 1.0));
  39.             }
  40.             else
  41.             {
  42.                 suma = suma - (potega/(2*n + 1.0));
  43.                 stos_sum.push(-potega/(2*n + 1.0));
  44.             }
  45.             p = !p;
  46.             nastepny = abs((potega*x*x)/(2*(n+1) + 1.0));
  47.            
  48.         }
  49.     }
  50.     else
  51.     {
  52.         suma = (sgn(x) * M_PI_2) - (1/x);
  53.         long double potega = x;
  54.         bool p = 1;
  55.         stos_sum.push(suma);
  56.         long double nastepny = 1000.0;
  57.         for(int n =1; nastepny >= epsilon; n+=1)
  58.         {
  59.             potega = potega * x *x;
  60.             if(p==1)
  61.             {
  62.                 suma = suma + 1/((2*n + 1)*potega);
  63.                 stos_sum.push(1/((2*n + 1)*potega));
  64.             }
  65.             else
  66.             {
  67.                 suma = suma - 1/((2*n + 1)*potega);
  68.                 stos_sum.push(-1/((2*n + 1)*potega));
  69.             }
  70.             p = !p;    
  71.             nastepny = abs(1/((2*n + 3)*(potega*x*x)));
  72.         }
  73.     }
  74.         long double arctan = atan(x);
  75.         long double wynik =0.0;
  76.         int size = stos_sum.size();
  77.         for(int i=0; i <size; i++)
  78.         {
  79.             wynik = wynik + stos_sum.top();
  80.             stos_sum.pop();
  81.         }
  82.         //cout << "kolejnosc dodawania od najmniejszych do najwiekszych\n";
  83.         //cout << "argument:\t"<< x << "\n";
  84.         //cout << "wynik obliczen:\t"<< wynik << "\n";
  85.         //cout << "wynik atan stl:\t"<< arctan << "\n";
  86.         //cout << "blad wzgledny:\t" << (wynik-arctan)/arctan << "\n";
  87.         //cout << "blad bezwzgledny:\t" << wynik-arctan <<"\n\n";
  88.         //cout << "kolejnosc dodawania od najwiekszych do najmniejszych\n";
  89.         //cout << x << "\t" << suma << "\t" << arctan << "\t" << (suma-arctan)/arctan << "\t" << suma-arctan << "\n\n";
  90.         //cout << epsilon << " " << size << "\n";
  91.         //cout << size << "\n";
  92.         //wyjscied << x << "\t" << wynik << "\n";
  93.         cout << x << "\n";
  94.         wyjscied << x << "\t" << (wynik-arctan)/arctan << "\n";
  95.        
  96. }
  97.  
  98. void licz_float(float x, long double epsilon)
  99. {
  100.     float suma = x;
  101.     stack<float> stos_sum;
  102.     if(x <= 1 && x >= (-1))
  103.     {
  104.         suma = x;
  105.         float potega = x;
  106.         bool p = 0;
  107.         float nastepny = 1000.0;
  108.         stos_sum.push(suma);
  109.         for(int n = 1; nastepny >= epsilon; n+=1)
  110.         {
  111.             potega = potega * x * x;
  112.        
  113.             if(p==1)
  114.             {
  115.                 suma = suma + (potega/(2*n + 1.0));
  116.                 stos_sum.push(potega/(2*n + 1.0));
  117.             }
  118.             else
  119.             {
  120.                 suma = suma - (potega/(2*n + 1.0));
  121.                 stos_sum.push(-potega/(2*n + 1.0));
  122.             }
  123.             p = !p;
  124.             nastepny = ((potega*x*x)/(2*(n+1) + 1.0));
  125.  
  126.            
  127.         }
  128.     }
  129.     else
  130.     {
  131.         suma = (sgn(x) * M_PI_2) - (1/x);
  132.         float potega = x;
  133.         bool p = 1;
  134.         stos_sum.push(suma);
  135.         float nastepny = 1000.0;
  136.         for(int n =1; nastepny >= epsilon; n+=1)
  137.         {
  138.             potega = potega * x *x;
  139.             if(p==1)
  140.             {
  141.                 suma = suma + 1/((2*n + 1)*potega);
  142.                 stos_sum.push(1/((2*n + 1)*potega));
  143.             }
  144.             else
  145.             {
  146.                 suma = suma - 1/((2*n + 1)*potega);
  147.                 stos_sum.push(-1/((2*n + 1)*potega));
  148.             }
  149.             p = !p;        
  150.         }
  151.     }
  152.         long double arctan = atan(x);
  153.         float wynik =0.0;
  154.         int size = stos_sum.size();
  155.         for(int i=0; i <size; i++)
  156.         {
  157.             wynik = wynik + stos_sum.top();
  158.             stos_sum.pop();
  159.         }
  160.         cout << "kolejnosc dodawania od najmniejszych do najwiekszych\n";
  161.         cout << "argument:\t"<< x << "\n";
  162.         cout << "wynik obliczen:\t"<< wynik << "\n";
  163.         cout << "wynik atan stl:\t"<< arctan << "\n";
  164.         cout << "blad wzgledny:\t" << (wynik-arctan)/arctan << "\n";
  165.         cout << "blad bezwzgledny:\t" << wynik-arctan <<"\n\n";
  166.         cout << "kolejnosc dodawania od najwiekszych do najmniejszych\n";
  167.         cout << x << "\t" << suma << "\t" << arctan << "\t" << (suma-arctan)/arctan << "\t" << suma-arctan << "\n\n";
  168. }
  169.  
  170.  
  171. int main()
  172. {
  173.     //cout << fixed;
  174.     long double x=0;
  175.     long double epsilon=0;
  176.     cout.precision(30);
  177.  
  178.     wyjscied << fixed;
  179.     wyjscied.open("outd.txt");
  180.     wyjscied.precision(26);
  181.     if(!wyjscied.good())
  182.     {
  183.         printf("blad pliku wyjsciowego");
  184.         return 12;
  185.     }
  186.     //cout << "arctg z zadana dokladnoscia\n";
  187.     //cout << "podaj argument funkcji\n";
  188.    
  189.     //cin >> x;
  190.     x = -0.999;
  191.     epsilon=1e-10;
  192.     //cout << "podaj zadana dokladnosc\n";
  193.     //cin >> epsilon;
  194.  
  195.    
  196.     for(int i=1; i<2000; i++)
  197.     {
  198.         x=(i*0.001)-1;
  199.         licz_double(x, epsilon);   
  200.     }
  201.     //licz_float((float)x,epsilon);
  202.     wyjscied.close();
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement