#1
  1. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12

    what are NaN's all about?


    i've done one of shannon's equasions that finds out the information of a message source (information in shannon's thing means freedom / entropy / spectrum of choice basically)

    with the given info (the number[] array) the outcome should be zero. the result is NaN. what's that all about? i know slightly, but not really. and what can you do about it? just test for NaN in some way and if posative, answer = 0.0; ?

    Code:
    #include <stdio.h>
    #include <math.h>
    #define SIZE sizeof(number) / sizeof(number[0])
    
    main()
    {
    	int i;
    	double number[] = {1.0, 0.0}; // the message probabilities
    	double answer;
    	
    	answer = 0.0;
    	for(i = 0; i < SIZE; i++)
    		answer += number[i] * log(number[i]);
    	
    	answer *= -1;
    		
    	printf("%f\n", answer);
    }
    also how can you test if a double equals something properly? because a double can look like 1.0, but if you say if(doub == 1.0) {....} the .... never gets run because of the inaccuracies, so as far as the computer is concerned doub does not equal 1.0. how can you go about testing double equality reasonably?
    Last edited by balance; July 17th, 2003 at 09:27 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    No es importante
    Posts
    2,065
    Rep Power
    14
    NaN is either true or false. Either the value is a number or not.
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Code:
     float x = 9.234, y = 9.432;
    
            if(x == y)
                    printf("float comparison ==\n");
    
            if((int)x == (int)y)
                    printf("there we go\n");
    yields:
    <oo7:Wed Jul 16> ./a.out
    there we go
  6. #4
  7. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    thanks

    but what does NaN mean?

    the answer to the above code should be 0. should i, or is it possible to, test for nan and then set answer to 0 if it is nan?

    i've a got a feeling they're the product of some kind of sum that can't really be done, like a number divided by zero, and probably lots of other things that'll cause them?



    also on comparing floats, what if you want to compare 2 floats including the decimal points, rather than just compare the whole number part?
    Last edited by balance; July 17th, 2003 at 07:29 AM.
  8. #5
  9. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    NaN is short for "not a number", so yeah, it can be used to denote the result of a division-by-zero. Or what you get when you try to convert a string to a number, when the string doesn't contain a valid string representation of a number.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  10. #6
  11. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    not a number. right. thanks.

    how can i say? :

    if(answer == NaN)
    answer = 0;
  12. #7
  13. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    The test for NaN's in C and C++ results from the fact that a NaN must not be equal to any floating point value, including itself .....

    So, the test to use is if (x != x) ....

    You need to be careful with this because;

    a) Some compiler optimisers are aggressive, and assume the result of x != x is always false. This means you often need to turn off compiler optimisation in the code doing this test.

    b) NaN is actually defined as part of IEEE floating point formats. If your machine doesn't support IEEE floating point (and there are a lot of those) then the test won't work.

    Keep in mind that testing for NaN (like testing for other special values such as infinity) is closing the barn door after the horse has bolted. You will usually get more robust code by structuring your algorithms so they don't generate NaN's in the first place (eg not taking the sqrt of a negative value).
  14. #8
  15. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    great information. thanks.

    the test did work for me, but as you said about barn doors and horses, i have changed the code to make sure it doesn't happen in the first place.
  16. #9
  17. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: what are NaN's all about?


    Originally posted by balance
    also how can you test if a double equals something properly? because a double can look like 1.0, but if you say if(doub == 1.0) {....} the .... never gets run because of the inaccuracies, so as far as the computer is concerned doub does not equal 1.0. how can you go about testing double equality reasonably?
    You need to compare the difference between the two values, and see if the absolute value of this is less than some small number, usually called Epsilon. For example, if you want to see if a == b (both doubles), then do the following:

    Code:
    #define EPSILON 0.00001 // whatever accuracy you prefer
    
    if (fabs(a-b) < EPSILON) printf("A = B.\n");
    else printf("A != B.\n");
  18. #10
  19. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    Jason beat me to the punch. But I have to mention another thing.

    In the code:
    Code:
    double x,y;
    x=25;y=25;
    if((int)x==(int)y) cout << "same";
    It doesn't always equate to true. Sometimes x or y will == 24.99999999... and converting to an int changes it to 24.

    So whenever you convert to an int, it's best to round the value.
  20. #11
  21. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    ahh interesting solution jason i didnt think of that one :)
  22. #12
  23. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    yup thanks Jason, just what i was after :)


    So whenever you convert to an int, it's best to round the value.
    yeah i remember thinking that myself epecially as there was another thread around the same time that mentioned adding 0.5 onto the number before casting to int in order to round off.
  24. #13
  25. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    That's one way of rounding, but I'm not sure which is faster. I'm guessing that using round() would be the fastest.
  26. #14
  27. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by balance
    yup thanks Jason, just what i was after :)

    yeah i remember thinking that myself epecially as there was another thread around the same time that mentioned adding 0.5 onto the number before casting to int in order to round off.
    You're welcome.

    Be careful, since I do not think adding 0.5 works for negative numbers, although I have never tired it myself. I do not think I have ever let a compiler type cast a float into an int, before, now that I think about it... I must not have ever needed to do that.
    Last edited by Jason Doucette; July 18th, 2003 at 09:49 AM.

IMN logo majestic logo threadwatch logo seochat tools logo