Sign in to follow this  

[C++] Weird bug, float having value -1.#IND0?

Recommended Posts

I have a function that's called at least a thousand times, in that function i have this:

float radius = baseRadius * (1 - unit_taper * Z);

radius is calculated correctly except for one call, where it has the value -1.#IND0. (I'm guessing radius isn't being initialized for some reason). In that instance:

baseRadius 0.91499996 float

unit_taper 0.29999995 float

Z 0.83333331 float

i'd give more information, except i have no idea what's going on!

Share this post

Link to post
Share on other sites
That means that it could not determine what that number should be ( like dividing by zero for example ).

Can you paste a more complete code example, including where/how you print out these values. If it always faults in the same place each time you can also try to step through it in the debugger to get an exact idea of what is going wrong.

Share this post

Link to post
Share on other sites
Ah it's kind of a huge program, so that won't really work.. I'm already using a breakpoint to get the values.

I don't understand why this is n't working. According to the debugger, 1 - unit_taper * Z = 0.75000006. Why would multiplying that by 0.915 (baseRadius) cause a problem??

I tried splitting the calculation, didn't work.

Here's the whole function if you're interested

// calculates the stem radius at a given offset
// offs; the offset from the stem base
// return; the radius
float Stem::CalcRadiusAt(float offs)
float taper = lpar->nTaper;

float unit_taper = 0;
if (taper <= 1)
unit_taper = taper;
else if (taper <= 2)
unit_taper = 2 - taper;

float Z = min(offs / length, 1);
float radius = baseRadius * (1 - unit_taper * Z);

float depth = 0;
if (taper > 1)
float Z2 = (1 - Z) * length;

if (taper < 2 || Z2 < radius)
depth = 1;
depth = taper - 2;

float Z3;
if (taper < 2)
Z3 = Z2;
Z3 = abs(Z2 - 2*radius * (int)(Z2/(2*radius) + 0.5));

if (taper > 2 || Z3 < taper)
radius = (1 - depth) * radius + depth * sqrt(radius*radius - (Z3-radius)*(Z3-radius));

// only for the trunk
if (stemLevel == 0)
float y = max(0, 1 - 8 * Z);
float flare = 1 + par->flare * (pow(100, y) - 1) / 100.0f;
radius *= flare;


radius *= par->_0Scale; // what about _0ScaleV?

return radius;

Share this post

Link to post
Share on other sites
I'm such a moron. The problem isn't there, it's in the sqrt(). I'm probably giving it a negative value.. I'll check it out

Edit: Yep, i'm passing it a negative value. I replaced the if right before the sqrt by:

if (taper > 2 || Z3 < radius)

and it worked..


Share this post

Link to post
Share on other sites
Here's a little snippet that's saved me hours of debugging:
[source lang="C++"]
// enable floating-point exceptions
unsigned int prev = _controlfp(0, _EM_ZERODIVIDE|_EM_INVALID);
This will let you catch zero-division and invalid operations where they happen instead of having to litter your code with assertions.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this