Question

I'm using C# with the XNA library and I'm getting NaNs cropping up in my Vector3 objects. Is there a way to break into the debugger when the offending calculation happens (e.g. a divide by zero)? Currently the program just continues running. I'm using VS2008 Professional. All the exceptions in the Exceptions dialog are selected in the "user-unhandled" column.

Edit: To clarify, I can't work out where the bad calculation is. This is why I want the debugger to break automatically. Setting breakpoints is not a solution.

Was it helpful?

Solution

Firstly dividing a double/float by zero gives Infinity/-Infinity depending upon whether the double is positive or negative. Only a zero double/float divided by zero gives NaN. In either case, no exception will be thrown.

You should be able to use conditional breakpoints to detect when a particular variable gets set to one of these values. Be careful when checking for NaN though, as NaN != NaN.

double a = double.NaN;
Console.Out.WriteLine(a == double.NaN); // false
Console.Out.WriteLine(a == a); // false
Console.Out.WriteLine(double.IsNaN(a)); // true

OTHER TIPS

Sounds like you're handling the exception somehow (like a catching a generic Exception) What you can do is press Ctrl+alt+E to bring up the exceptions dialog -- make sure you check the "when thrown" checkbox for the exception(s) you're interested in

You could set a conditional breakpoint that only breaks when the divisor of an expression is 0.

I know this is an old post but.....

from experience its nearly always Vector3.Normalize that i use by mistake.

When I'm not sure whether it will be zero length I now always do

float L = V.Length(); if(L != 0.0) V /= L;

divide by zero in Normalize should give an exception but it doesn't. Caused me a lot of head scratching.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top