Question

I have been tinkering with NSNumber for a little while when searching for infinity value and I have come across one interesting fact. When I do this:

long infinity = [((NSNumber *)kCFNumberPositiveInfinity) longValue];

32bit simulator interprets it as 0, while 64bit simulator interprets it as -9223372036854775808.

Now I remember my Computer Architecture 101 and they used to tell us - 32bit = smaller range, 64bit = wider range. But obviously, on 64bit simulator's interpretation of positive infinity there is an overflow(since my research tells me that positive infinity will most likely be a positive number).

The question is - is this a feature(if so, then why?), or a bug?

Was it helpful?

Solution

With the 32 bit simulator,

If you make an object from (NSNumber *)kCFNumberPositiveInfinity like this:

NSObject *dummy = (NSNumber *)kCFNumberPositiveInfinity;

and inspect it, you will see it is this:

dummy = (__NSCFNumber *)(double)inf

and when you convert it to an long value like this:

long longDummy = [(NSNumber *)dummy longValue];

it gets set to 0

if you just set a long to that value like this:

long testDummy = kCFNumberPositiveInfinity;

it gets set to 53994368

The problem (whether it be a bug, or just an unexpected implementation) is in the format of this call

[((NSNumber *)kCFNumberPositiveInfinity) longValue];

where you force it to an NSNumber, which defaults to a double, because it doesn't know better, and then get a longValue from a double.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top