Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 15th, 2003, 11:03 PM
 balance
.

Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation Power: 12

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 10:27 AM.

#2
July 15th, 2003, 11:28 PM
 victorpendleton
Contributing User

Join Date: Jan 2003
Location: No es importante
Posts: 2,065
Time spent in forums: 6 h 50 m 52 sec
Reputation Power: 13
NaN is either true or false. Either the value is a number or not.

#3
July 16th, 2003, 12:11 AM
 infamous41md
not a fan of fascism (n00b)

Join Date: Feb 2003
Location: ct
Posts: 2,756
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
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
July 17th, 2003, 08:17 AM
 balance
.

Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation 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 08:29 AM.

#5
July 17th, 2003, 08:30 AM
 Analyser
*bounce*

Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513
Time spent in forums: 2 Days 22 h 48 m 47 sec
Reputation Power: 41
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/

#6
July 17th, 2003, 09:05 AM
 balance
.

Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation Power: 12
not a number. right. thanks.

how can i say? :

#7
July 17th, 2003, 09:39 AM
 grumpy

Join Date: Jun 2003
Posts: 1,044
Time spent in forums: 2 Days 53 m 47 sec
Reputation Power: 13
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
July 17th, 2003, 10:24 AM
 balance
.

Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation 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.

#9
July 17th, 2003, 10:45 AM
 Jason Doucette
jasondoucette.com

Join Date: Feb 2003
Posts: 378
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Re: what are NaN's all about?

Quote:
 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
July 17th, 2003, 12:29 PM
 dog135
Doggie

Join Date: Jul 2003
Location: Seattle, WA
Posts: 751
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 12
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
July 17th, 2003, 01:32 PM
 infamous41md
not a fan of fascism (n00b)

Join Date: Feb 2003
Location: ct
Posts: 2,756
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
ahh interesting solution jason i didnt think of that one

#12
July 17th, 2003, 01:46 PM
 balance
.

Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation Power: 12
yup thanks Jason, just what i was after

Quote:
 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
July 17th, 2003, 07:02 PM
 dog135
Doggie

Join Date: Jul 2003
Location: Seattle, WA
Posts: 751
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 12
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
July 18th, 2003, 10:40 AM
 Jason Doucette
jasondoucette.com

Join Date: Feb 2003
Posts: 378
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Quote:
 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 10:49 AM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > what are NaN's all about?