Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include "funciones.h"
- #include <iostream>
- using namespace std;
- int main(int argc, char** argv) {
- int precision = 3;
- double val;
- double resp;
- //newPrec = 1/(potencia1(10,precision));
- //cout << newPrec << endl;
- val = pi(4);
- cout << val << endl;
- resp = coseno(45,1,4);
- cout << resp << endl;
- return 0;
- }
- #ifndef FUNCIONES_H
- #define FUNCIONES_H
- double pi(int);
- double potencia1(double, int);
- double abs(double);
- double coseno(double, double, int);
- #endif /* FUNCIONES_H */
- #include "funciones.h"
- double pi(int precision) {
- double piR, piRAnt, newPrec;
- int ind1 = 2, ind2 = 1;
- int condicion = 1;
- piR = 2;
- int bandera = 0;
- newPrec = 1 / (potencia1(10, precision)); // 0.001
- while (condicion) {
- piRAnt = piR;
- piR = piR * ind1 / ind2;
- if (bandera){
- ind1 += 2;
- bandera = 0;
- }
- else{
- ind2 += 2;
- bandera = 1;
- }
- if (abs(piRAnt - piR) > newPrec) {
- condicion = 1;
- } else{
- condicion = 0;
- //piRAnt = piR;
- }
- }
- return piRAnt;
- }
- double coseno(double x, double tipo, int precision){
- //Tipo = 0, entonces el angulo esta en radianes
- //Tipo = 1, entonces el angulo esta en sexagesimales
- if(tipo == 1) x *= (pi(precision))/180;
- //Transformando la precision
- double pre = 1;
- for(int i=1; i<=precision; i++)
- pre /= 10;
- double resp1 = 1.0;
- double resp2 = 1.0;
- double a = 1.0;
- double b = 1.0;
- bool resta = true;
- while(1){
- a *= x*x;
- if(b == 1.0) b = 2;
- else b *= ((b+1)*(b+2));
- if(resta){
- resp2 -= (a/b);
- resta = false;
- }
- else{
- resp2 += (a/b);
- resta = true;
- }
- if(abs(resp1-resp2) < pre) break;
- else resp1 = resp2;
- }
- return resp2;
- }
- double potencia1(double base, int exp) {
- double pot;
- int i;
- pot = 1;
- for (int i = 1; i <= exp; i++) {
- pot = pot * base;
- }
- return pot;
- }
- double abs(double x) {
- if (x >= 0)
- return x;
- else return (-1 * (x));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement