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 undefined
Last Comment
Fernando Soto

8/22/2022 - Mon

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

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
Fernando Soto


NaN is "Not A Number".


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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Fernando Soto

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.
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question