You should not compare floating point numbers for equality in most cases. This is true of any language on any platform.

Indeed, some floating-point numbers (such as the one represented by the integer 0x7fc00000) will compare as not equal to themselves:

I think what the OP really wanted to know (and I'm interested in the answer too) is whether going out to the stringValue and back to the doubleValue is guaranteed to yield a float that is bitwise identical to the original float, or whether there is "drift" in the least significant bit or two due to the changes in representation. Anyway, even if that's not the OP meant, that's what I'd like to ask. :->


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");

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to