Simple Recursive Function- returning nan

Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to a recursive function intended to simulate population growth. Participants explore the reasons for the function returning "nan" (not a number) and suggest various modifications to the code to address the problem. The focus includes debugging, variable passing methods, and the implications of using pointers versus references.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the function returns "nan" and questions why this occurs, suggesting that the details of the program may not be crucial.
  • Another participant raises a concern about the handling of the variable X, specifically when it is between 0 and 1, indicating that the current checks might be insufficient.
  • A participant points out that only the last instance of the recursive function returns a value, implying that earlier calls do not return anything, which could lead to "nan".
  • There is a suggestion to pass X by reference instead of by value to ensure that changes persist across recursive calls.
  • A participant expresses confusion about the need to pass timeSteps as a pointer and seeks clarification on "catching the return value of growth".
  • After modifying the function to pass X by reference, a participant reports no longer receiving "nan" but instead getting a different output, prompting further investigation into the results.
  • Another participant clarifies that the time value is not being passed correctly as a pointer and suggests that passing it as a pointer or reference may not be necessary.
  • One participant expresses gratitude for achieving the desired results after making the suggested changes.
  • A later post indicates a lack of familiarity with passing by reference and notes the timing of their course completion as a limitation.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method for passing variables in the recursive function, with differing opinions on the necessity of pointers versus references. The discussion remains unresolved regarding the optimal approach to handle the timeSteps variable.

Contextual Notes

There are limitations in understanding the implications of passing variables by value versus by reference, as well as the handling of recursive function returns. Some participants express uncertainty about specific programming concepts, which may affect their implementation.

sandy.bridge
Messages
797
Reaction score
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
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.
 
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.
 
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:
Try it with:
Code:
double growth(int timeSteps, double r, double& X)
It means that X is passed by reference instead of by value.
 
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:
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.
 
sandy.bridge said:
EDIT: I got the desired results! THANKS A BUNCH!

Good! :smile:
 
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 16 ·
Replies
16
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K