Re: NSNumber stringValue
Re: NSNumber stringValue
- Subject: Re: NSNumber stringValue
- From: glenn andreas <email@hidden>
- Date: Sat, 12 Dec 2009 11:47:01 -0600
On Saturday, December 12, 2009, at 11:32AM, "Ben Haller" <email@hidden> wrote:
>>> 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. :->
>
It is absolutely not guaranteed to work - see <http://docs.sun.com/source/806-3568/ncg_goldberg.html#1251>.
To be able to convert base 2 (the binary representation) to base 10 (the textual representation) without a loss of precision is possible (since 2 is a factor of 10), though it potentially requires at least 17 digits (quick back of the envelope calculation of how many digits are in 1/2**24, given that a float has a 24 bit mantissa), and stringValue isn't documented as providing any specific number of digits of precision.
Going the other way, 0.1 (base 10) isn't possible to be represented exactly as a floating point number.
_______________________________________________
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