Advertisement
xlujiax

Lab_2013_1

Sep 15th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <cstdlib>
  2. #include "funciones.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char** argv) {
  8.     int precision = 3;
  9.     double val;
  10.     double resp;
  11.     //newPrec = 1/(potencia1(10,precision));
  12.     //cout << newPrec << endl;
  13.     val = pi(4);
  14.     cout << val << endl;
  15.    
  16.     resp = coseno(45,1,4);
  17.     cout << resp << endl;
  18.    
  19.     return 0;
  20. }
  21.  
  22. #ifndef FUNCIONES_H
  23. #define FUNCIONES_H
  24.  
  25. double pi(int);
  26. double potencia1(double, int);
  27. double abs(double);
  28. double coseno(double, double, int);
  29.  
  30. #endif /* FUNCIONES_H */
  31.  
  32. #include "funciones.h"
  33.  
  34. double pi(int precision) {
  35.     double piR, piRAnt, newPrec;
  36.     int ind1 = 2, ind2 = 1;
  37.     int condicion = 1;
  38.     piR = 2;
  39.     int bandera = 0;
  40.     newPrec = 1 / (potencia1(10, precision)); // 0.001    
  41.  
  42.     while (condicion) {
  43.         piRAnt = piR;
  44.         piR = piR * ind1 / ind2;
  45.        
  46.         if (bandera){
  47.             ind1 += 2;
  48.             bandera = 0;
  49.         }
  50.         else{            
  51.             ind2 += 2;
  52.             bandera = 1;
  53.         }    
  54.         if (abs(piRAnt - piR) > newPrec) {
  55.             condicion = 1;
  56.         } else{
  57.             condicion = 0;
  58.             //piRAnt = piR;
  59.         }    
  60.     }
  61.     return piRAnt;
  62. }
  63.  
  64. double coseno(double x, double tipo, int precision){
  65.     //Tipo = 0, entonces el angulo esta en radianes
  66.     //Tipo = 1, entonces el angulo esta en sexagesimales
  67.     if(tipo == 1) x *= (pi(precision))/180;
  68.    
  69.     //Transformando la precision
  70.     double pre = 1;
  71.     for(int i=1; i<=precision; i++)
  72.         pre /= 10;
  73.    
  74.     double resp1 = 1.0;
  75.     double resp2 = 1.0;
  76.     double a = 1.0;
  77.     double b = 1.0;
  78.     bool resta = true;
  79.     while(1){
  80.         a *= x*x;
  81.         if(b == 1.0) b = 2;
  82.         else b *= ((b+1)*(b+2));
  83.         if(resta){
  84.             resp2 -= (a/b);
  85.             resta = false;
  86.         }
  87.         else{
  88.             resp2 += (a/b);
  89.             resta = true;
  90.         }
  91.        
  92.         if(abs(resp1-resp2) < pre) break;
  93.         else resp1 = resp2;
  94.        
  95.     }
  96.     return resp2;
  97. }
  98.  
  99.  
  100. double potencia1(double base, int exp) {
  101.     double pot;
  102.     int i;
  103.     pot = 1;
  104.     for (int i = 1; i <= exp; i++) {
  105.         pot = pot * base;
  106.     }
  107.     return pot;
  108. }
  109.  
  110. double abs(double x) {
  111.     if (x >= 0)
  112.         return x;
  113.     else return (-1 * (x));
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement