Re: NSNumber stringValue
Re: NSNumber stringValue
- Subject: Re: NSNumber stringValue
- From: Andy Lee <email@hidden>
- Date: Sat, 12 Dec 2009 23:24:36 -0500
On Dec 12, 2009, at 7:38 PM, RedleX Support wrote:
> My original intent was to store a representation of the float in a string so I can load it back again. In the meanwhile I found that using the %a format along with NSScanner's scanHexDouble does the trick (since I don't need that string to be human readable a hex representation is ok for me)
>
>
> double a=pi;
> double b;
>
> [[NSScanner scannerWithString:[NSString stringWithFormat:@"%a",a]] scanHexDouble:&b];
>
> if (a==b)
> NSLog(@"Success");
> else
> NSLog(@"Failure");
I hadn't thought of that -- I was going to suggest using NSArchiver and NSUnarchiver, though that approach would produce binaries rather than strings. NSScanner will work if you know you're always going to have a normal value but doesn't handle the nan and inf cases Andrew mentioned. I'm guessing you can get around that by writing your own trivial parser class that detects "nan", "inf", and "-inf" and converts those special cases to NAN, INFINITY, and -INFINITY, and uses NSScanner for all other cases. I don't know enough about "non-normal" float values to know for sure, though it might be close enough for your purposes.
As a side note, I found that you don't get perfect precision if you store an NSNumber in a plist; it looks like it stores the stringValue.
Out of curiosity, I wonder how Core Data stores floats and doubles if you choose an XML data store. I would expect it to be precise, but I'm admittedly too lazy to check.
--Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden