Click here to Skip to main content
15,888,340 members

Comments by Love and Hope (Top 4 by date)

Love and Hope 28-Feb-22 22:54pm View    
I totally agree with you, but this is for school the teacher wanted it to be done like that, I guess she wanted us to stumble upon double comparison
Love and Hope 28-Feb-22 21:49pm View    
Thanks alot indeed, finally it is working, look:

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

/*

epsilon function to compare double

*/

bool cmpf(double A, double B, double epsilon = 0.000005f)/* we set the precision to our needs here*/
{
return (fabs(A - B) < epsilon);
}



int main() {



int how_many_terms=0,one_time=0;
int counter = 1;
double odd = 1;

double sum = 0;
int max = 200000;


bool bSign = true;

double pi = 0;
//4-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+...

do {
if(bSign)
{
pi += 4 / odd;//FIRST ODD IS ONE MAKING THIS A WHOLE NUMBER AS IN JUST 4
bSign = false;
}
else
{
pi -= 4 / odd;//FIRST ODD IS ONE MAKING THIS A WHOLE NUMBER AS IN JUST 4
bSign = true;
}

odd+=2;//NEXT ODD NUMBER WE STARTED IN 1 PLUS 2 WE HAVE A SERIES OF ODD NUMBERS

double desired_pi = 3.14159;




counter++; //this increases each cycle in order to count the amount of cycles, of interations
sum += pi;// TO ADD UP ALL VALUES TO CALCULATE AVERAGE
cout <<" PI SO FAR "<< pi <<" times " <
Love and Hope 28-Feb-22 21:49pm View    
#include <iostream>
#include <cmath>
#include <limits>using namespace std;

/*

epsilon function to compare double

*/

bool cmpf(double A, double B, double epsilon = 0.000005f)/* we set the precision to our needs here*/
{
return (fabs(A - B) < epsilon);
}



int main() {



int how_many_terms=0,one_time=0;
int counter = 1;
double odd = 1;

double sum = 0;
int max = 200000;


bool bSign = true;

double pi = 0;
//4-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+...

do {
if(bSign)
{
pi += 4 / odd;//FIRST ODD IS ONE MAKING THIS A WHOLE NUMBER AS IN JUST 4
bSign = false;
}
else
{
pi -= 4 / odd;//FIRST ODD IS ONE MAKING THIS A WHOLE NUMBER AS IN JUST 4
bSign = true;
}

odd+=2;//NEXT ODD NUMBER WE STARTED IN 1 PLUS 2 WE HAVE A SERIES OF ODD NUMBERS

double desired_pi = 3.14159;




counter++; //this increases each cycle in order to count the amount of cycles, of interations
sum += pi;// TO ADD UP ALL VALUES TO CALCULATE AVERAGE
cout <<" PI SO FAR "<< pi <<" times " <
Love and Hope 28-Feb-22 21:48pm View    
Thanks a lot indeed, I found the solution, the code is working look is pretty much what you and jeron1