Why am i getting -nan(ind)

How can i fix that error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  float returnFallenDistance (float gravity, float height, float thrustForce, float mass, time_t elapsedTime)
{
	std::cout << "---------------" << std::endl;
	float gravityForce = gravity * mass;
	std::cout << gravityForce << std::endl;

	float resultingForce;
	resultingForce = thrustForce - gravityForce;
	std::cout << resultingForce << std::endl;

	float fallspeed;
	fallspeed = resultingForce / mass;
	std::cout << fallspeed << std::endl;

	float fallenDistance = (fallspeed / 2) * (float(elapsedTime) * float(elapsedTime));
	std::cout << fallenDistance << std::endl;

	return fallenDistance;
}


Then i did this:

1
2
height += returnFallenDistance(gravity, height, thrust, mass, moment - start);
std::cout << height << std::endl;


But im getting -nan(ind) in console.

it cant be out of float range because its like
400000 + fallendistance.
Can you provide an example of the parameter values when you get this output?

Also where and how is height initialized?
Hi,

If you have a recursive function, it needs to have a base case. That is, some condition which causes it to end. Otherwise it's just going to keep calling itself.

Why does it need to be recursive? These things are normally worked with simple equations.


Prefer double rather than float. The precision of float is easily exceeded.

Good Luck !!
Last edited on
There is no number assigned to anything. You can't divide nothing by two, etc. You need to give values to gravity, height, thrustforce, mass, and time_t elapsed. When you call this function are you putting a number for each of these? And are they floating point numbers (1.00, not just 1, etc.)?
5886
4114
6.85667
5763.03 //changing of course
-nan(ind)
@jlb
Last edited on
So if you're passing a -nan into the function what do you expect the result to be other than -nan?

For that last parameter you probably should be using difftime() to compute the time difference instead of the subtraction of two time_t.
http://www.cplusplus.com/reference/ctime/difftime/
Im sorry i posted the output ,the parameters are.

9.81
400000
10000
1000
time since startup in seconds (1,2,3...)

@connorhperkins all of these variables have a value calculated in a part i didnt post here and they are floats/time_t

Why should i use difftime()? Whats its advantage?
Last edited on
Using the input you provided I get the following output:
---------------
9810
190
0.19
2.375
Value Returned: 2.375


Why should i use difftime()? Whats its advantage?

One big advantage is that if you used difftime() you wouldn't need to be casting the time_t to a float. And look at what the documentation from this site says about using a time_t:
http://www.cplusplus.com/reference/ctime/time_t/

Portable programs should not use values of this type directly, but always rely on calls to elements of the standard library to translate them to portable types.

And understand that a time_t is probably some unsigned type.


Topic archived. No new replies allowed.