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){
- long double suma = x;
- stack<long double> stos_sum;
- if(x <= 1 && x >= (-1)){
- suma = x;
- stos_sum.push(suma);
- long double potega = x;
- long double nastepny = 1000.0;
- bool p=0;
- 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 = abs((potega*x*x)/(2*(n+1) + 1.0));
- }
- }
- else{
- suma = (sgn(x) * M_PI_2) - (1/x);
- long double potega = x;
- bool p = 1;
- stos_sum.push(suma);
- long double nastepny = 1000.0;
- for(int n =1; nastepny >= epsilon; n+=1){
- potega = potega * x *x;
- if(p==1){
- suma = suma + 1/((2*n + 1)*potega);
- stos_sum.push(1/((2*n + 1)*potega));
- }
- else{
- suma = suma - 1/((2*n + 1)*potega);
- stos_sum.push(-1/((2*n + 1)*potega));
- }
- p = !p;
- nastepny = abs(1/((2*n + 3)*(potega*x*x)));
- }
- }
- 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 << "wynik obliczen:\t"<< wynik << "\n";
- cout << "blad wzgledny:\t" << (wynik-arctan)/arctan << "\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);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement