Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <iomanip>
- #include <string>
- #include <cmath>
- #include <sstream>
- using namespace std;
- template <typename T>
- std::string to_string(T const& value) {
- stringstream sstr;
- sstr << value;
- return sstr.str();
- }
- __float80 sum(vector< __float80 > &vect, int terminos){
- __float80 suma=vect.front();
- for (int i=1 ; i < terminos ; i++){
- suma+=vect[i];
- }
- return suma;
- }
- vector <__float80 > pi_vector(int &i){
- vector <__float80> pi_vect;
- __float80 cte = 1.0;
- for (int j = 0 ; j < i; j++){
- cte=pow((1.0/(16.0)),j);
- 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)));
- }
- return pi_vect;
- }
- string str2pi(vector <__float80 > &vect){
- string pi_str="3.";
- long int p_val;
- __float80 p2_val;
- __float80 ten = 10;
- for (int i=1 ; i < int(vect.size())-1 ; i++){
- if (i > 300 ) break;
- p2_val=((sum(vect,i))/pow(10.0,-i)) ;
- p_val=(long int)(fmod(p2_val,ten));
- pi_str+=to_string(p_val);
- }
- return pi_str;
- }
- int main(int argc , char *argv[]){
- if ( argc <= 1 ){
- cout << "necesitas un argumento el numero de terminos" << endl;
- exit (1);
- }
- vector <__float80> p1_vect;
- int terms=atoi(argv[1]);
- p1_vect=pi_vector(terms);
- string pi=str2pi(p1_vect);
- cout << pi << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment