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

Join Date
Dec 2002
Posts
296
Rep Power
16

#### 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

for(i = 0; i < SIZE; i++)

}```
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. No Profile Picture
Contributing User
Devshed Regular (2000 - 2499 posts)

Join Date
Jan 2003
Location
No es importante
Posts
2,065
Rep Power
18
NaN is either true or false. Either the value is a number or not.
3. 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
4. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
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.
5. 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.
6. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

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

how can i say? :

7. 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).
8. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
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.
9. #### 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");```
10. 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.
11. ahh interesting solution jason i didnt think of that one :)
12. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
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.
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.
14. 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.