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

Started by
4 comments, last by kdmiller3 12 years, 10 months ago
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!
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Advertisement
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.
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;
else
depth = taper - 2;

float Z3;
if (taper < 2)
Z3 = Z2;
else
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;
}

// TO DO: LOBES

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

return radius;
}

"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
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..

</idiot>
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Good stuff. I was going to ask you where you put your break point because there was other stuff in there where stuff could have gone wrong.
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);
[/source]
This will let you catch zero-division and invalid operations where they happen instead of having to litter your code with assertions.

This topic is closed to new replies.

Advertisement