user3409117
user3409117

Reputation: 13

C++ Pi Estimation Program Not Working Properly

I am currently writing a program that estimates Pi values using three different formulas pictured here: https://i.sstatic.net/vjtaG.png .

This is my program so far:

{
double leibniz = 0.0; // pi value calculated from Leibniz                        
double counter = 0.0; // starting value                                          
double eulerall = 0.0; // value calculated from Euler (all integers)             
double eulerodd = 0.0; // value calculated from Euler (odds)                     
double eulerallans; // pi value calculated from Euler series (all integers)      
double euleroddans; // pi value calculated from Euler series (odd integers)      
int terms;
bool negatives = false;

cin >> terms;
cout << fixed << setprecision(12); // set digits after decimal to 12             
while(terms > counter){
leibniz = 4*(pow(-1, counter)) / (2*counter+1) + leibniz;
eulerall = (1/pow(counter+1,2)) + eulerall;
eulerodd = 32*(pow(-1, counter)) / (pow(2*counter + 1, 3)) + eulerodd;
counter++;
eulerallans = sqrt(eulerall*6);
euleroddans =  pow(eulerodd, 1.0/3.0);
cin >> terms;
if (terms < 0){
if(!negatives)
  negatives=true;
}
}

cout << right << setw(14) << "# TERMS" << setw(15) << "LEIBINZ" << setw(15) << "\
EULER-ALL" << setw(15) << "EULER-ODD" << endl;
cout << right << setw(14) << terms << " " << leibniz << " " << eulerallans << " "\
<< euleroddans <<endl;
cout << "There were " << negatives << " negative values read" << endl;

return 0;

}

The sample input file that I am using is:

1

6

-5

100

-1000000

0

And the sample output for this input file is:

1 4.000000000000 2.449489742783 3.174802103936

6 2.976046176046 2.991376494748 3.141291949057

100 3.131592903559 3.132076531809 3.141592586052

When I run my program all I get as an output is:

# TERMS LEIBINZ EULER-ALL EULER-ODD 1

4.000000000000

2.449489742783

1.000000000000

So, I have two problems with my program: 1) It is only reading the first value in the input file and stopping. 2) The equation for eulerodd seems to be off, but I can't figure out what the problem is.

EDIT: Thanks to @RaphaelMiedl, I solved problem 2. Now I only have problem 1 to deal with.

Help is greatly appreciated.

Upvotes: 0

Views: 133

Answers (1)

AliciaBytes
AliciaBytes

Reputation: 7429

I don't have the time to go through all of your code but

euleroddans =  pow(32*eulerodd, 1/3);

immediately jumped me when I skimmed over it. 1/3 is integer arithmetic and gives you 0. Probably not what you wanted, you probably want 1.0/3.0 or something of the like there.


Now a bit of a late addition since I had time to look at your code again. @paddy is right that you only got one input statement in your code.

I'd probably put the whole pi calculation and outputting in a function and then loop over the input like:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void my_pi_func(int term);

int main()
{
    int term;

    cout << fixed << setprecision(12); // set digits after decimal to 12         

    while (cin >> term){
        my_pi_func(term);
    }

    return 0;

}

void my_pi_func(int term)
{
    if (term <= 0){
        cout << "\n\nNegative value or 0 read\n\n" << endl;
        return;
    }

    double leibniz = 0.0; // pi value calculated from Leibniz                     
    int counter = 0; // starting value                                       
    double eulerall = 0.0; // value calculated from Euler (all integers)          
    double eulerodd = 0.0; // value calculated from Euler (odds)                  
    double eulerallans = 0.0; // pi value calculated from Euler series (all integers)   
    double euleroddans = 0.0; // pi value calculated from Euler series (odd integers)

    while(term > counter){
    leibniz = 4*(pow(-1, counter)) / (2*counter+1) + leibniz;
    eulerall = (1/pow(counter+1,2)) + eulerall;
    eulerodd = (pow(-1, counter)) / (pow(2*counter + 1, 3)) + eulerodd;
    counter++;
    eulerallans = sqrt(eulerall*6);
    euleroddans =  pow(32*eulerodd, 1.0/3.0);
    }
    cout << right << setw(14) << "# TERMS" << setw(15) << "LEIBINZ" << setw(15)
    << "EULER-ALL" << setw(15) << "EULER-ODD" << endl;
    cout << right << setw(14) << term << " " << leibniz << " " << eulerallans 
    << " " << euleroddans <<endl;

}

You can see a working version here. Note that I made some more slight adjustments. For one I changed terms into term since that plural s was somehow bothering me. Also I changed counter to be an int since a counter as double variable doesn't make sense in my opinion. Also I changed how negative/0 values output. But all in all you should get what you should change if you compare with yours.

Upvotes: 1

Related Questions