CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2002
    Location
    USU, Logan, UT
    Posts
    5

    NaN error in c++

    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.

  2. #2
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    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.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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.)
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Jun 2002
    Location
    Dover, England
    Posts
    28
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured