Hi,

I just stumbled across a bug in NSNumber that I can't fix as I don't understand 
the NSNumber code. The problem is easy to see, though:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        NSNumber *n   = [NSNumber numberWithInt:2];
        NSNumber *nan = [NSDecimalNumber notANumber];

        NSLog(@"%@ (class:%@ double:%g) == %@ -> %s",
                  n, NSStringFromClass([n class]), [n doubleValue],
                  nan, [n isEqualToNumber:nan] ? "YES" : "NO");

        [pool release];
        return 0;
}

On FreeBSD-8.2rc3 (amd64) this produces the following output:
2011-02-17 16:02:15.316 NumberTest[59574] 2 (class:NSIntNumber double:2) == NaN 
-> YES
Which is clearly wrong.

I came across this issue via NSNumberFormatter, which instead of formatting any 
integer numbers always printed NaN. In my project  I tried to debug into the 
issue and found this:
769       if ([anObject isEqual: [NSDecimalNumber notANumber]])
(gdb) s
-[NSNumber isEqual:] (self=0x803e8d1d0, _cmd=0x801410980, anObject=0x805e625c0)
    at NSNumber.m:376
376       if ([anObject isKindOfClass: NSNumberClass])
(gdb) n
378           return [self isEqualToNumber: anObject];
(gdb) po anObject
NaN
(gdb) po [anObject class]
NSDecimalNumber
(gdb) s
-[NSNumber isEqualToNumber:] (self=0x803e8d1d0, _cmd=0x80140d300, 
    aNumber=0x805e625c0) at NSNumber.m:371
371       return ([self compare: aNumber] == NSOrderedSame) ? YES : NO;
(gdb) s
-[NSSignedIntegerNumber compare:] (self=0x803e8d1d0, _cmd=0x80140d2e0, 
    aNumber=0x805e625c0) at NSNumber.m:107
107       if (aNumber == self)
[…]
(gdb) n
165               double value = [self doubleValue];
(gdb) p other
$3 = nan(0x8000000000000)
(gdb) p value
$4 = 6.9533556002420585e-310

As you can see above, asking an NSIntNumber instance for its doubleValue works 
properly, however, it fails at NSNumber.m:165. I don't know the reason, but I 
also don't understand the implications of the macros used. Can somebody 
elaborate? Please note that I'm on a 64-bit (amd64) system.

Thanks,

  Marcus

-- 
Marcus Mueller  .  .  .  crack-admin/coder ;-)
Mulle kybernetiK  .  http://www.mulle-kybernetik.com
Current projects: http://www.mulle-kybernetik.com/znek/


Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
Gnustep-dev mailing list
Gnustep-dev@gnu.org
http://lists.gnu.org/mailman/listinfo/gnustep-dev

Reply via email to