Click to See Complete Forum and Search --> : NaN error in c++


skulkarni
July 21st, 2002, 03:50 PM
Hi, I am working with c++ on linux and i got an error while running the program and the below code is a part of it.


float last_dist = matrix_distance(at_place_id, item.end_place);
float value = (item.score / ( item.time_spent + (last_dist / item.mph) + item.time_from_end_to_mth + item.total_distance + last_dist + item.dist_from_end_to_mth));

when i executed the code, i got the NaN error. and when i changed the code to

float last_dist = matrix_distance(at_place_id, item.end_place);
float fmph = (float)item.mph;
float tot_time = item.time_spent + (last_dist / fmph) + item.time_from_end_to_mth;
float tot_dist = item.total_distance + last_dist + item.dist_from_end_to_mth;
float fscore = (float)item.score;
float den = tot_dist + tot_time;
float value = fscore / den;

the error is gone.

What may be the reason for it. why do we get Nan errors and how to remove them.

Thanks.
Srinivas.

JMS
July 21st, 2002, 08:12 PM
NaN stands for Not a Number. Often this is caused by divide by zero. Since this is a run time error and not a compile error the limited snippet of code you provided is of only limited help. You'd need to check out the values of the variables to really investigate this..

The Majic bullit would be to verify what you're setting "item.mph" and verify that that is a float and not an int variables...

The 100% sure fire but cumbersome way to verify what's going on would be to dump the values of all the numbers to a file before you execute the effending code snippet. That way you can see what is unexpected.

Graham
July 22nd, 2002, 03:38 AM
As JNS says, it's not easy to determine what's happening from the code snippet you gave.

As for NaN - that comes about because not every bit pattern forms a legal floating point number. It's in the nature of the way that floats are encoded that some patterns of the 32 (or whatever) bits can't be interpreted as a floating number, so we get the concept of NaN. (Which means, intriguingly, that, for computing, the set of integers is larger than the set of real numbers.) Under normal circumstances, you shouldn't be able to set a float to such a bit pattern so you don't normally see them. One way to do it would be to make a union of an unsigned int and a float, assign incrementally to the int, and access the float and see where you get problems. (By the way, the standard forbids you to assign to one member of a union and access another.)

Pete Bourner
July 22nd, 2002, 03:53 AM
Hi skulkarni,

On a different note, there is something logically wrong with the code snippet you presented. Going from the variable names, you appear to be adding a time and a distance, for example from the original code:

float value = (item.score / ( item.time_spent + (last_dist / item.mph) + item.time_from_end_to_mth + item.total_distance + last_dist + item.dist_from_end_to_mth));

where you are adding item.time_spent and item.total_distance, or
the expanded version of the code:

float den = tot_dist + tot_time;

where presumably 'tot_dist' represents a distance and 'tot_time' represents a time.

You cannot add together quantities which are in different units, as the result will not make any sense. Whether the NaN problem gets fixed or not, this code will not be producing meaningful answers because of this other problem.

Pete.