Reputation: 3317
I have come across the nan
value in some of my code and I know how to handle it, but when trying to explain what exactly nan means, I realized I'm not sure myself. There are also some similar odd values for things that I would like some clarification on.
inf
this clearly means infinity, but will this show up when you reach the maximum float value?
nan
what exactly does this mean and when does this value pop up?
nil
I think I do know this one. An instance of an object that has not been allocated. I thought it belongs in this list anyway.
null
I'm not sure about this one but I think it's similar to nil?
Upvotes: 3
Views: 4082
Reputation: 366073
inf
this clearly means infinity, but will this show up when you reach the maximum float value?
This isn't really part of ObjC, except that NSNumber
uses this as the -description
when holding an infinite value.
The actual value inside an NSNumber
is a double
, which is defined by C—and, actually, the definition is largely platform-dependent. However, on most platforms, it's an IEEE-754 binary64
. The IEEE 754 standard defines an affinely extended real number line, with values for positive and negative infinity as well as finite values. It also precisely defines the different ways a finite calculation can overflow to infinity, and how your code can control that. But, as a general rule, yes, if a calculation surpasses the maximum finite value for a double
, you get positive infinity.
nan
what exactly does this mean and when does this value pop up?
This also is mainly about C and IEEE-754, not ObjC (again, except for the -description
of an NSNumber
). Wikipedia has a good page on NaN
, but, briefly it means "Not a Number", and it's used for cases where there is no sensible value, not even infinity (or where it's ambiguous between too many different values), like 0/0
.
nil
I think I do know this one. An instance of an object that has not been allocated. I thought it belongs in this list anyway.
nil
is not an instance of an object. nil
is an empty pointer, used when there is no instance to point to. It's basically equivalent to (id)0
or (NSObject *)0
.
But "an object that has not been allocated" is not an object, and doesn't exist, so… to the extent that it makes any sense to talk about such a thing in the first place, I guess you could say nil
is the pointer to it.
null
I'm not sure about this one but I think it's similar tonil
?
+[NSNull null]
values are used as a place-holder object that actually exists, but represents nothing, in a place where you can't actually not have anything (e.g., in an NSArray
).
You may also see null
if you try to print out an empty pointer as a C string (e.g., NSLog(@"%s", 0)
). But any code where you see this is not actually legal C or ObjC code.
Also, people often confuse null
with NULL
, which means any empty pointer.
So, when you have an id
, NSObject *
, NSSomeOtherClass *
, etc., and you don't have anything for it to point to, use nil
. When you have a pointer to a non-ObjC value, like an int *
, and you don't have anything for it to point to, use NULL
. When you want to put an empty pointer into a collection, use null
.
Upvotes: 12
Reputation: 224092
NaN means “Not a Number”.
Floating-point operations produce NaNs when there is not a number that would be a sensible result. E.g., there is no floating-point number that sqrt(–1)
could return that would be a good approximation to the square root of –1.
NaNs can be used for a variety of things. Software could initialize floating-point objects to signaling NaNs so that using one without assigning it a value first would cause a trap.
Quiet NaNs can be silently ignored to allow a large computation to complete, with the NaNs to be fixed up or removed later. (Most operations propagate NaNs: If you add a NaN and a number, the result is a NaN.)
Upvotes: 2