Re: NSKeyedArchiver and NSPoints
Re: NSKeyedArchiver and NSPoints
- Subject: Re: NSKeyedArchiver and NSPoints
- From: "Clark S. Cox III" <email@hidden>
- Date: Wed, 28 Apr 2010 14:09:20 -0700
On Apr 28, 2010, at 12:31 PM, Quincey Morris wrote:
> On Apr 28, 2010, at 11:42, Kyle Sluder wrote:
>
>> Er? Unless there's some documentation about the precision of
>> NSStringFromPoint being less than that of the members,
>> NSStringFromPoint could certainly include just as much precision as
>> the actual float.
>
> Assuming that the string contains a decimal floating point number, then both the conversion to and from binary floating point involve (in general) imprecision from rounding and/or truncation, since in both cases the result could theoretically require an infinite number of decimal or binary places.
>
> Keep in mind there are some binary floating point numbers with a finite number of bits that have no exact representation in any finite decimal string, and vice versa. It's certainly true that since both representations are finite, then with enough decimal digits you could get very close (no more error than 1 in the last binary digit), but very close is not the same as exact.
>
> Even if you could get back the original binary value by using enough decimal digits, you'd still need to be certain that the binary to string conversion in the frameworks was just that finicky (as opposed to, say, using a library routine that defaults to a smallish maximum number of significant digits).
>
> Plus, you'd have to expect that encoding doubles would require, in the worst case, at least 309 significant digits, which is a much longer string than I want in my archive.
>
> So my original point stands: I want my original bit pattern back again after unarchiving, and I don't believe that a finite decimal string will guarantee that.
Then I would recommend using hex-float strings, as they are guaranteed to represent the exact value of the given floating point value (as binary fp to hex fp is lossless, unlike binary to dec):
[ccox@ccox-macpro:~]% cat test.m
#import <Foundation/Foundation.h>
int main() {
NSPoint p = {3.141592653, 3.141592653};
NSString *s = [NSString stringWithFormat: @"{%A, %A}", p.x, p.y];
NSLog(@"%@", s);
NSPoint p2 = NSPointFromString(s);
if(memcmp(&p, &p2, sizeof p) == 0) {
NSLog(@"Bit-pattern was preserved");
} else {
NSLog(@"Bit-pattern was not preserved");
}
return 0;
}
[ccox@ccox-macpro:~]% cc test.m -framework Foundation -fobjc-gc -arch x86_64 && ./a.out
2010-04-28 14:06:19.396 a.out[90877:903] {0X1.921FB542FE938P+1, 0X1.921FB542FE938P+1}
2010-04-28 14:06:19.399 a.out[90877:903] Bit-pattern was preserved
[ccox@ccox-macpro:~]% cc test.m -framework Foundation -fobjc-gc -arch i386 && ./a.out
2010-04-28 14:06:22.478 a.out[90885:903] {0X1.921FB6P+1, 0X1.921FB6P+1}
2010-04-28 14:06:22.481 a.out[90885:903] Bit-pattern was preserved
[ccox@ccox-macpro:~]% cc test.m -framework Foundation -fobjc-gc -arch ppc && ./a.out
2010-04-28 14:06:26.167 a.out[90893:903] {0X1.921FB6P+1, 0X1.921FB6P+1}
2010-04-28 14:06:26.204 a.out[90893:903] Bit-pattern was preserved
Since NSPointFromString isn't documented to work with hex-floats you may want to do the parsing yourself with NSScanner, but other than that, the principle is the same.
_______________________________________________
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