You can try dividing one by N, in the next line multiply again by N, and check for 1.

It may fail for N=3 or N=10 or many others, as 1/3 and 1/10 cannot be represented exactly in binary floating point notation. 1/2, 1/4, 1/8 can, but 1/3, 1/5, 1/10 can't.

Note: even when (1.0/3.0)*3.0 may be printed as 1, that does not mean it is exactly one; the convert-to-string routines are somewhat 'forgiving'.

Therefore, you should not apply an equality test to floating point numbers, instead allow for some tolerance, as in:

Copy Code

`if (abs(currentValue-expectedValue) < epsilon ) ...`

where epsilon is some small value, i.e. small with respect to expectedValue.Note2: if you make epsilon too small, your algorithm may never converge... and that is what you are experiencing right now, as your code basically has epsilon equal to zero.

PS: all of the above applies to all languages I know, it is not specific to C++.

https://www.tutorialspoint.com/what-is-the-most-effective-way-for-float-and-double-comparison-in-c-cplusplus

For a way to method to compare.

#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 " <

#define _USE_MATH_DEFINES

#include <math.h>

const double desired_pi = M_PI;