0

my code for the program looks like this

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

double pi (double);

int main()
{
 cout << "Enter n to value of pi: "; double n; cin>>n;
 cout << pi(n) << endl;

 return 0;
}


double pi (double n)
{
 if (n==1)
  return 4*1;
 else
  return ( 4*(pow(-1,n+1)*(1/(2*n-1))) + pi(n-1) );
}

except it's in C (I literally only changed the include statements, scanf, printf).

The program keeps crashing if i enter 0.00001 or anyhting less for epsilon

where epsilon means (i just rearrange epsilon in that equation to find n, then use n as the paramaeter for recursion) http://puu.sh/7ot1P.png

I think it's because it's doing too much recursion. Any way to fix it?

BTW the error is segmentation fault (core dumped) when I run it on a unix server

  • possible duplicate of [Change stack size for a C++ application in Linux during compilation with GNU compiler](http://stackoverflow.com/questions/2275550/change-stack-size-for-a-c-application-in-linux-during-compilation-with-gnu-com) – munk Mar 09 '14 at 05:41
  • Should `n` be an `int` or a `double`? – GWW Mar 09 '14 at 05:42
  • It should be double, sorry. My code fixed the minor details, and my code is in C, not C++. Does the "duplicate question" thing work for C? I'm not sure how to incorporate their little code segment into my C program. – user3397709 Mar 09 '14 at 05:44
  • 1
    @user3397709: The code in your question is C++, not C. – R.. GitHub STOP HELPING ICE Mar 09 '14 at 06:16
  • I'm saying MY code (not the one I posted) is in C; sorry that is confusing haha. The one I posted is in C++, but MY code is in C. Everything is the same, I only changed the syntax. This shouldn't matter. I have the same problem with both languages. I know it's not a problem with the code because it works for big values of epsilon (>0.0001). I'm asking how to allocate more memory for recursion. – user3397709 Mar 09 '14 at 06:32
  • 1
    @user3397709 Why are you not posting your code? Why are you posting an irrelevant snippet? – user3383733 Mar 09 '14 at 06:39
  • `pow(-1,n+1) = (n % 2 ? -1 : 1)` for every n, but will perform better on most CPUs – Tomer W Oct 01 '18 at 21:06

3 Answers3

2

pi() expects a double, you give int.

Philip
  • 1,068
  • 3
  • 12
  • 21
  • Hey I fixed it, sorry about that. But that wasn't the problem, because this works for medium values of epsilon (up to 0.001). Any less (0.0001) crashes. – user3397709 Mar 09 '14 at 05:46
  • That's no problem because of the implicit casting of the `int` to a `double`. – halex Mar 09 '14 at 07:54
0
  1. type of n should be int.
  2. I think n should be greater or equal to 1.
  3. the recursion function pi() need add end condition when n<1.
0

As you correctly suppose you are recursing too deep and therefore get a stack overflow. On my machine (Linux Mint 16 64 Bit) the maximum size of the stack is 8MB, and your code runs well up to a call of ca. pi(230000).

To solve this problem you have 3 options:

  • Increase the size of the stack with the command ulimit -s size_in_kB. That only works on Unix based systems and can lead to problems when you use a too large value for the stack size.
  • Change your approximation algorithm from an recursive to an iterative one.
  • Use an algorithm that faster converges, see https://en.wikipedia.org/wiki/Approximations_of_%CF%80 for some ideas
halex
  • 16,253
  • 5
  • 58
  • 67