Public Group

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

This topic is 2567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

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:

unit_taper 0.29999995 float

Z 0.83333331 float

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

##### 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 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; 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; }

##### 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..

</idiot>

##### Share on other sites
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.

##### 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);
[/source]
This will let you catch zero-division and invalid operations where they happen instead of having to litter your code with assertions.

• 16
• 11
• 10
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999748
×