Simple Recursive Function- returning nan

In summary, the conversation discusses a program that simulates population growth. The program returns "nan" as the final value, indicating an error. The issue is resolved by passing the variables by reference instead of by value, allowing the program to properly calculate and return the final population value. The use of pointers in this process is also mentioned.
  • #1
sandy.bridge
798
1
Simple Recursive Function-- returning "nan"

Homework Statement


I have a program that is supposed to simulate a growth population. The details are not of much importance. It is a rather simple program, however, I am having problems with the program returning the final value that it has stored in X. It returns "nan", which means not a number, however, I am not seeing why. Perhaps one of you do? Thanks in advance!

Code:
#include <iostream>
using namespace std;

double growth(int timeSteps, double r, double X)
{
	if(timeSteps >= 0)
	{
		if (X < 0)
		{
			X = 0;
		}
		else if (X > 1)
		{
			X = 1;
		}		cout << timeSteps << " " << X << endl;
		X = r*X*(1-X);
		timeSteps = timeSteps - 1;
		growth(timeSteps, r, X);
		if(timeSteps==0){return X;}
	}
}int main() {
		cout << "Specify model parameters (Time limit, Growth, Initial Population): ";
		double X, r;
		int timeSteps;
		cin >> timeSteps >> r >> X;
		cout << "Time " << "Population" << endl;
		double R = growth(timeSteps, r, X);
		cout << "The final population was:  " << R;	return 0;
}

Here is an example of the console:
Code:
Specify model parameters (Time limit, Growth, Initial Population): 5 1 0.5
Time Population
5 0.5
4 0.25
3 0.1875
2 0.152344
1 0.129135
0 0.112459
The final population was:  nan

I am wanting the final population to output Population when Time is 0.
 
Last edited:
Physics news on Phys.org
  • #2
What happens if X is between 0 and 1? I.e., 0 <= X <= 1. You're checking only for X < 0 and X > 1. That could be dangerous in a recursive routine.

Also, I would be more inclined to use double variables rather than floats.

Judicious use of a debugger would show exactly why you're getting NAN for your final value.
 
  • #3
Only the last instance of growth will "return X". The others, including the one called from main, terminate without a return.

Variables are passed by value, so neither timeSteps nor X are modified by the recursive calls to growth. You need to pass a pointer for timeSteps and catch the return value of growth.
 
  • #4
I edited my post to make it a bit easier to follow. I also changed the little things that I can.

I will also have to read into pointers a bit more.

I have declared timeSteps as a pointer in the main function. I then passed the pointer into the function. What do you mean by "catch the return value of growth"?
 
Last edited:
  • #5
Try it with:
Code:
double growth(int timeSteps, double r, double& X)
It means that X is passed by reference instead of by value.
 
  • #6
Do I still want to pass the time value as a pointer? I did as you said and I am no longer getting "nan"; I am however, getting 0.0998122.
Code:
#include <iostream>
using namespace std;

double growth(int timeSteps, double r, double &X)
{
	if(timeSteps >= 0)
	{
		if (X < 0)
		{
			X = 0;
		}
		else if (X > 1)
		{
			X = 1;
		}

		cout << timeSteps << " " << X << endl;
		X = r*X*(1-X);
		timeSteps = timeSteps - 1;
		growth(timeSteps, r, X);
	}
	return X;
}
int main() {
		cout << "Specify model parameters (Time limit, Growth, Initial Population): ";
		double X, r;
		int timeSteps;
		cin >> timeSteps >> r >> X;
		int *timeSteps_ptr = &timeSteps;
		cout << "Time " << "Population" << endl;
		double R = growth(*timeSteps_ptr, r, X);
		cout << "The final population was:  " << R;	return 0;
}
EDIT: I think I know the issue though. It is giving me the value for X after the time value passes zero. I will tinker a bit with it and see what I come up with.EDIT: I got the desired results! THANKS A BUNCH!
 
Last edited:
  • #7
sandy.bridge said:
Do I still want to pass the time value as a pointer? I did as you said and I am no longer getting "nan"; I am however, getting 0.0998122.

You are not passing the time value as a pointer.
As it is, you first convert it into a pointer, but then you dereference it again, so you still pass it by value.
Anyway, it serves no purpose to pass the time value as a pointer or as a reference.
 
  • #8
sandy.bridge said:
EDIT: I got the desired results! THANKS A BUNCH!

Good! :smile:
 
  • #9
I have not learned about this '&' yet, and I don't think I will considering I am at the end of my course! Is a nifty little trick, though. Thanks.
 

What is a simple recursive function?

A simple recursive function is a function that calls itself repeatedly until a specific condition is met. It is a programming technique used to solve problems that can be broken down into smaller, simpler versions of the original problem.

What does "returning nan" mean in a simple recursive function?

"Returning nan" means that the function is returning a value of "not a number". This can happen when the function is trying to perform a mathematical operation that is not possible, such as dividing by 0, or when there is an error in the code.

Why does a simple recursive function sometimes return nan?

A simple recursive function may return nan if there is an error in the code, such as a missing or incorrect base case, or if the function is trying to perform an impossible mathematical operation. It is important to check for these potential errors when writing a recursive function.

How can I fix a simple recursive function that is returning nan?

If your simple recursive function is returning nan, it is likely due to an error in the code. You can fix it by carefully checking your code for any mistakes, making sure you have a correct base case, and checking that your mathematical operations are valid. Debugging tools and stepping through the code can also help identify the source of the error.

What are some common mistakes when writing a simple recursive function?

Some common mistakes when writing a simple recursive function include not having a correct base case, not properly breaking down the problem into smaller versions, and forgetting to return a value. It is also important to make sure the function is not running infinitely, as this can lead to a stack overflow error. Using proper debugging techniques and careful testing can help identify and fix these mistakes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
747
  • Engineering and Comp Sci Homework Help
Replies
8
Views
833
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
933
Back
Top