0

I am starting to learn C++ and I have a problem with my code. I want to find the pi value using the Leibniz series and also the number of iterations to reach five significant digits (3.14159) but it's doesn't work.

  #include<iostream>
  #include <math.h>
  using namespace std;

 int main(){
      double pi = 0.0;
      int count = 0;
    for ( int i = 0 ; i <= 10000 ; i++){
       pi += 4*pow(-1,i)/(2*i+1);
       if ( pi == 3.14159){
       cout<<i;
       break;
       }
    }
  }
Napole
  • 11
  • 2
  • 2
    Can you explain "doesn't work" in more technical terms? – tadman Mar 21 '21 at 18:36
  • Hint: In floating point math you're extremely unlikely to get a value that matches 3.14159 *precisely*, especially when you're using a method like this that will introduce a lot of noise into that value as it approaches the limit. If you want to test a particular decimal place, consider `if (pi * 100000 % 10 == N)` or something like that. – tadman Mar 21 '21 at 18:40
  • @tadman It didn't output anything. – Napole Mar 21 '21 at 18:40
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – rsjaffe Mar 21 '21 at 18:42
  • @tadman so how can I find the interaction when pi = 3.14159? please help me – Napole Mar 21 '21 at 18:42
  • Tip: Print every intermediate result. You're unlikely to find one that matches that value precisely. – tadman Mar 21 '21 at 18:42
  • 2
    As computed here, `pi` will never, ever equal that value. – tadman Mar 21 '21 at 18:43
  • Hint: To see the *actual* values you can't depend on defaults, you need to ask for more detail, like `std::cout << std::setprecision(16) << pi << std::endl;` via ``. What you get with defaults only shows a fixed number of places and can lead you astray, it's not the actual value. – tadman Mar 21 '21 at 18:46
  • Never ever use the == operator on floating point values. Instead of `x==y` use `abs(x-y) < tolerance` where `tolerance` is a small positive number, e.g. `1e-6` (the actual value must be chosen to fit the situation). – nielsen Mar 21 '21 at 22:16

1 Answers1

0

You may check if the absolute difference between the computed value via Leibniz series and the "true" value of pi is below a given tolerance. Instead of using 3.14159 as value for pi, you may use the built-in constant contained in math.h: M_PI.

#include<iostream>
#include <math.h>

int main(){
  double pi = 0.0;

  for ( int i = 0 ; i <= 10000 ; i++){
      // i-th value via Lieibniz formula
      pi += 4*pow(-1,i)/(2*i+1);
      // Check if the difference is below a given tolerance equal to 0.0001
      if (abs(pi - M_PI)<0.0001){
          // Print the iteration at which the given tolerance is achieved
          std::cout<<i<< std::endl;
          // Break the for cycle
          break;
      }
  }
  return 0;
}

The above code checks if the absolute difference between the approximated value and the "true" value of pi is below 0.0001.

You may also check if the relative difference from the actual value is below a given tolerance, by substituting the check with

if (abs(pi - M_PI)/M_PI<0.0001){
  // Print the iteration at which the given tolerance is achieved
  std::cout<<i<< std::endl;
  // Break the for cycle
  break;
}

In your code, the variable count is unused. Let me give you a little advice: do not use using namespace::std.

Eddymage
  • 1,008
  • 1
  • 7
  • 22
  • 1
    Thanks you so much;) – Napole Mar 22 '21 at 22:41
  • @Napole Glad to help! If you find that the answer solved your problem, mark it as accepted for future users! If you need any guidance about posting question&answers, accepting answers and using SO, have a look at the [tour] and/or visit the [help]. – Eddymage Mar 22 '21 at 22:44