0

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?

Michal
  • 15,429
  • 10
  • 73
  • 104

1 Answers1

0

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.

HalR
  • 11,411
  • 5
  • 48
  • 80
  • Right, but then why does the 64bit simulator makes it such a nonsense number and not 0 as well? – Michal Apr 09 '14 at 22:26
  • interesting. So [NSNumber numberWithLong: kCFNumberPositiveInfinity] yields a different result then? (also numberWithLongLong:...?) – RobP Apr 09 '14 at 22:27
  • Those two actually produce reasonable numbers -> 26530688 on 32bit and 4322992432 on 64bit. But that's because your example is retyping kCFNumberPositiveInfinity to long, whereas I am retyping it to NSNumber. – Michal Apr 09 '14 at 22:46
  • It seems like kind of a blind retyping. NSNumber can me one of several types of numbers. kCFNumberPositiveInfinity seems to depend on context. So its not too surprising that a variable input combined with a variable output produces spurious results. – HalR Apr 09 '14 at 22:57
  • I actually found a post from 2006 doing the same thing, but with int - http://lists.apple.com/archives/cocoa-dev/2006/May/msg01766.html and with Intel vs PowerPC :-) – Michal Apr 09 '14 at 23:06