Avatar of davidmakovoz
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?

Thanks,
David
C#

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
vo1d

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

ASKER
They are all floats and when I put
Single.IsNaN(costWithInsert)
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

@vo1d

NaN is "Not A Number".

@davidmakovoz

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

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

Fernando
davidmakovoz

ASKER
       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.
ASKER CERTIFIED SOLUTION
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
or
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