Hi all people
I've written program below for a physics problem.

#include <iostream>
#include <conio.h>
#include <math.h>
#define g 9.8342998109761958158459616185564
using namespace std;

long double theta(unsigned long int n,unsigned long int N){
	return (M_PI/2)-n*(M_PI/(2*N));
}

long double V(unsigned long int n,unsigned long int N,unsigned long int h){
	if(n!=0){
		long double r=4*g*(h*M_PI/(4*N));
		return sqrt(r*sin(theta(n,N))+pow(V(n-1,N,h),2));
	}
	else return 0;
}

int main(){
	unsigned long int N,h;
	cout<<"Enter the number of segments:";
	cin>>N;
	cout<<"Enter the radius of the curve:";
	cin>>h;
	cout<<"\nTerminal velocity = "<<V(N,N,h)<<"\n";
	getch();
	return 0;
}

As you've already understood,the higher the value of N,the higher accuracy for the answer.But when I enter Ns more than 21711 for every h I give no answer.I wanna know how can I do sth that I get an ansewr for higher Ns too.
thanks

You should probably not use built in types like doubles, ints and longs for these kind of calculations that require high precision and big numbers. Use GMP or something similar.

http://gmplib.org/

Thank you.But I can't do that.you know I should print the code in a paper and give it to my professor.So I can't have third party code.I'm sure he doesn't have the desire to read and understand that.But I keep it for later use.
So do you have another solution?
thanks again

First off I would like to second SasseMan's comment, for very high precision you need the GMP.

Second your algorithms is going to kill you, you have 27111, copies of a function call on the stack, that is a block of memory for each function call. If you wish to use this algorithm please reformulate it as a loop!

BUT, we don't know g to that many decimal places, and g changes dependent on height.
Every 1mm of altitude change alters g by about 1e-10 m/s. So unless you know your altitude to +/- nanometers, long double is sufficiently accurate.

Also long double is defined by added an L after the number e.g. long double X(1.2L); . You should use that on you #define of g otherwise it will be converted to a double, which will lose you some accuracy.

Finally, you have a strange value of g, [assuming the problem is Earth bound], typical value [sea-level], is about 9.807m/s.

Yes what StuXYZ said. C++ is not designed for recursion.

I thought about that but there is no algorithm for it that works with a loop.
and thanks for other suggestions but I'm still getting the same result.Ok not important.I don't think my professor understands the level of accuracy because he just wants the codes and one print of its result.I just wanted it to learn sth.thanks all of you

I can guarantee you that you can reformulate the recursion as a loop. Try to go through the algorithm by hand for a very small case that only takes 2-3 recursions and you will se that the solution is obvious, especially in this case where you only have one recursive call in your function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.