Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include <stack>
- #define M_PI_2 1.57079632679489661923
- using namespace std;
- int sgn(long double x)
- {
- if(x>0)
- return 1;
- else
- return -1;
- }
- void licz_double(long double x, long double epsilon)
- {
- if(x <= 1 && x >= (-1))
- {
- long double suma = x;
- long double potega = x;
- bool p = 0;
- long double nastepny = 1000.0;
- stack<long double> stos_sum;
- stos_sum.push(suma);
- for(int n = 1; nastepny >= epsilon; n+=1)
- {
- potega = potega * x * x;
- if(p==1)
- {
- suma = suma + (potega/(2*n + 1.0));
- stos_sum.push(potega/(2*n + 1.0));
- }
- else
- {
- suma = suma - (potega/(2*n + 1.0));
- stos_sum.push(-potega/(2*n + 1.0));
- }
- p = !p;
- nastepny = ((potega*x*x)/(2*(n+1) + 1.0));
- }
- long double arctan = atan(x);
- long double wynik =0.0;
- int size = stos_sum.size();
- for(int i=0; i <size; i++)
- {
- wynik = wynik + stos_sum.top();
- stos_sum.pop();
- }
- cout << "kolejnosc dodawania od najmniejszych do najwiekszych\n";
- cout << "argument:\t"<< x << "\n";
- cout << "wynik obliczen:\t"<< wynik << "\n";
- cout << "wynik atan stl:\t"<< arctan << "\n";
- cout << "blad wzgledny:\t" << (wynik-arctan)/arctan << "\n\n";
- cout << "kolejnosc dodawania od najwiekszych do najmniejszych\n";
- cout << x << "\t" << suma << "\t" << arctan << "\t" << (suma-arctan)/arctan << "\n\n";
- //cout << x << "\n";
- }
- else
- {
- long double suma = (sgn(x) * M_PI_2) - (1/x);
- long double potega = x;
- bool p = 1;
- for(int n =1; n<1000; n+=1)
- {
- potega = potega * x *x;
- if(p==1)
- suma = suma + 1/((2*n + 1)*potega);
- else
- suma = suma - 1/((2*n + 1)*potega);
- p = !p;
- }
- long double arctan = atan(x);
- cout << x << "\t" << suma << "\t" << arctan << "\t" << (suma-arctan)/arctan << "\n" << fixed;
- //cout << x << "\n";
- }
- }
- void licz_float(float x, long double epsilon)
- {
- if(x <= 1 && x >= (-1))
- {
- float suma = x;
- float potega = x;
- bool p = 0;
- long double nastepny = 1000.0;
- for(int n = 1; nastepny >= epsilon; n+=1)
- {
- potega = potega * x * x;
- if(p==1)
- suma = suma + (potega/(2*n + 1.0));
- else
- suma = suma - (potega/(2*n + 1.0));
- p = !p;
- nastepny = ((potega*x*x)/(2*(n+1) + 1.0));
- }
- float arctan = atan(x);
- cout << x << "\t" << suma << "\t" << arctan << "\t" << fabs((suma-arctan)/arctan) << "\n" << fixed;
- //cout << x << "\n";
- }
- else
- {
- float suma = (sgn(x) * M_PI_2) - (1/x);
- float potega = x;
- bool p = 1;
- for(float n =1.0; n<1000.0; n+=1.0)
- {
- potega = potega * x *x;
- if(p==1)
- suma = suma + 1/((2*n + 1)*potega);
- else
- suma = suma - 1/((2*n + 1)*potega);
- p = !p;
- }
- long double arctan = atan(x);
- cout << x <<"\t" << suma << "\t" << arctan << "\t" << fabs((suma-arctan)/arctan) << "\n" << fixed;
- //cout << x << "\n";
- }
- }
- int main()
- {
- cout << fixed;
- long double x=0;
- long double epsilon=0;
- cout.precision(30);
- cout << "arctg z zadana dokladnoscia\n";
- cout << "podaj argument funkcji\n";
- cin >> x;
- cout << "podaj zadana dokladnosc\n";
- cin >> epsilon;
- licz_double(x, epsilon);
- //licz_float((float)x,epsilon);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement