• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSNumber and NaN
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSNumber and NaN


  • Subject: Re: NSNumber and NaN
  • From: Chris Kane <email@hidden>
  • Date: Sat, 7 Dec 2002 16:47:37 -0800

I don't see that a useful response to this appeared in the mailing list, so here's one for the record:

The strange behavior isn't NSNumber -- NSNumber -stringValue seems to be returning results one would expect. It's NSNumberFormatter which is returning unusual results.

The abstract problem here is that NSNumberFormatter was originally written to handle money amounts for EOF, so it doesn't deal with unusual numbers well (or general purpose string formatting for that matter).

The more concrete problem is that when you ask an NSNumberFormatter for -stringForObjectValue:, if the object is not an NSDecimalNumber, it converts it to an NSDecimalNumber using
[NSDecimalNumber decimalNumberWithString:[objectValue stringValue]]
which is not going to handle unusual values well, in addition to potentially other issues. A further issue, though I don't know this is the case, is that I don't think there is an NSDecimal (the struct) representation of positive and negative infinity, as there is for NaN.

Chris Kane
Cocoa Frameworks, Apple


On Friday, October 18, 2002, at 06:22 AM, Girard Iglesias wrote:

I found a very strange behavior around NSNumber, it seems that the
formating of -1.0.0.0 give 0.00 in place of NaN ?

Regards

GI

PS : the code

{
NSNumberFormatter * theFormatter = [[[NSNumberFormatter alloc] init]
autorelease];
NSDecimalNumber * theNan = [NSDecimalNumber notANumber];
NSNumber * the0By0 = [NSNumber numberWithFloat:0.0/0.0];
NSNumber * the1By0 = [NSNumber numberWithFloat:1.0/0.0];
NSNumber * theM1By0 = [NSNumber numberWithFloat:-1.0/0.0];

printf("format notANumber : %s\n", [[theFormatter
stringForObjectValue:theNan] lossyCString]);

printf("format 0/0 : %s\n", [[theFormatter
stringForObjectValue:the0By0] lossyCString]);
printf("format 1/0 : %s\n", [[theFormatter
stringForObjectValue:the1By0] lossyCString]);
printf("format -1/0 : %s\n", [[theFormatter
stringForObjectValue:theM1By0] lossyCString]);

printf("stringValue for 0/0 : %s\n", [[the0By0 stringValue]
lossyCString]);
printf("stringValue for 1/0 : %s\n", [[the1By0 stringValue]
lossyCString]);
printf("stringValue for -1/0 : %s\n", [[theM1By0 stringValue]
lossyCString]);

}

And the output

format notANumber : NaN
format 0/0 : NaN
format 1/0 : NaN
format -1/0 : 0.00
stringValue for 0/0 : NaN
stringValue for 1/0 : Inf
stringValue for -1/0 : -Inf
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: key value coding sample code ?
  • Next by Date: Re: key value coding sample code
  • Previous by thread: [Off] Help a procrastinating student...
  • Next by thread: Re: key value coding sample code
  • Index(es):
    • Date
    • Thread