user1199624
user1199624

Reputation:

Objective-C: int can´t be NAN?

It seems that a int in Objective-C can't be NAN. This comparision returns false.

int i = NAN;
NSLog(@"isNan: %@", (isnan(i)) ? @"YES" : @"NO");

Is there any way to set an int to NAN, or do i have to use a double?

double d = NAN;
NSLog(@"isNan: %@", (isnan(d)) ? @"YES" : @"NO");

With a double it works.

Upvotes: 0

Views: 1805

Answers (2)

artm
artm

Reputation: 3678

NAN is a floating point value from IEEE 754 standard (see http://en.wikipedia.org/wiki/NaN)

According to wikipedia:

Most fixed sized integer formats do not have any way of explicitly indicating invalid data.

Upvotes: 3

一二三
一二三

Reputation: 21289

NaN is a value that is specific to the way that floating point numbers (float and double in C) are represented internally (IEEE 754). Is is not available for integer data types, as they may be represented in a completely different manner.

As a workaround, you may see people use a separate boolean flag to indicate if a value is valid, or the extreme values INT_MIN/INT_MAX. Although, nether of these is as good as NaN for ensuring valid values.

Upvotes: 6

Related Questions