Link to home
Start Free TrialLog in
Avatar of davidmakovoz

asked on

erratic NaN's

I am getting NaN's evaluating the following statement
                    costWithDelete = RowVectorTwo[i - 1] + m_DeleteCost;
The left hand side values are regular values and i-1 is not out of bounds.
The most strangest thing is that it happens completely randomly.
Any ideas what can cause this bevavior?

Avatar of vo1d
Flag of Germany image

what do you mean with NaN?
of what type is evaluating, RowVectorTwo, DeleteCost ?
Avatar of davidmakovoz


They are all floats and when I put
afterwards it catches it.
Moreover if I do the following
costWithDelete = RowVectorTwo[i - 1] + m_DeleteCost;
while (Single.IsNaN(costWithDelete))
 Console.WriteLine("{0}  {1}  {2}", costWithDelete, RowVectorTwo[i - 1], m_DeleteCost);
costWithDelete = RowVectorTwo[i - 1] + m_DeleteCost;
the program prints out a NaN once or twice and then proceeds happily

NaN is "Not A Number".


Can you show us some code and how costWithDelete, m_DeleteCost and RowVectorTwo are declared?

Hi davidmakovoz;

Microsoft states that: "Floating-point operations return NaN to signal that that result of the operation is undefined. For example, dividing 0.0 by 0.0 results in NaN."

So look for a operation that may cause this to happen.

       private float m_EditCost, m_DeleteCost, m_InsertCost, m_RecallPrecisionRelativeWeight;
            float[] RowVectorOne = new float[rows];
            float[] RowVectorTwo = new float[rows];
            float costWithInsert;
            float costWithDelete;
            float costWithEdit;

The problem is that this line is a piece of a fairly big project.
I have been trying to simplify it to locate the cause
but so far unsuccesfully.
In my experience this kind of problem is caused
by the memory being messed up.
And it can be done by any part of the program
seemingly unrelated to the one where the NaN shows up.
I wonder if there is a way in VS debugger to set up
memory access checking. I know such functionality
exists in Sun's debugger "workshop".
Also I was under the impression that C# does
very rigorous array bounds checking and such
would ocurr the whole program would crash.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial