# [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:

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

[code]//======================================================================
// calculates the stem radius at a given offset
// offs; the offset from the stem base
//======================================================================
{
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 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

if (taper > 2 || Z3 < taper)
{
}

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

// TO DO: LOBES

}

}[/code]

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982130

• 24
• 9
• 9
• 13
• 11