guitar-player

get_pi_digits.cpp

Aug 9th, 2012
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4. #include <string>
  5. #include <cmath>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9. template <typename T>
  10. std::string to_string(T const& value) {
  11.     stringstream sstr;
  12.     sstr << value;
  13.     return sstr.str();
  14. }
  15.  
  16.  
  17. __float80 sum(vector< __float80 > &vect, int terminos){
  18.     __float80 suma=vect.front();
  19.     for (int i=1 ; i < terminos ; i++){
  20.         suma+=vect[i];
  21.     }
  22.    
  23.     return suma;
  24. }
  25.  
  26. vector <__float80 > pi_vector(int &i){
  27.     vector <__float80> pi_vect;
  28.     __float80 cte = 1.0;
  29.     for (int j = 0 ; j < i; j++){
  30.         cte=pow((1.0/(16.0)),j);
  31.         pi_vect.push_back(cte*((4.0)/(8*j+1)-(2.0)/(8*j+4)-(1.0)/(8*j+5)-(1.0)/(8*j+6)));
  32.     }
  33.     return pi_vect;
  34. }
  35.  
  36. string str2pi(vector <__float80 > &vect){
  37.     string pi_str="3.";
  38.     long int p_val;
  39.     __float80  p2_val;
  40.     __float80 ten = 10;
  41.     for (int i=1 ; i < int(vect.size())-1 ; i++){
  42.         if (i > 300 ) break;
  43.         p2_val=((sum(vect,i))/pow(10.0,-i)) ;
  44.         p_val=(long int)(fmod(p2_val,ten));
  45.         pi_str+=to_string(p_val);
  46.     }
  47.    
  48.     return pi_str;
  49. }
  50.  
  51.  
  52. int main(int argc , char *argv[]){
  53.     if ( argc <= 1 ){
  54.         cout << "necesitas un argumento  el numero de terminos" << endl;
  55.         exit (1);
  56.     }
  57.     vector <__float80> p1_vect;
  58.     int terms=atoi(argv[1]);
  59.     p1_vect=pi_vector(terms);
  60.     string pi=str2pi(p1_vect);
  61.     cout << pi << endl;
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment