Re: NSNumber stringValue
Re: NSNumber stringValue
- Subject: Re: NSNumber stringValue
- From: Andy Lee <email@hidden>
- Date: Sat, 12 Dec 2009 13:07:40 -0500
On Dec 12, 2009, at 12:51 PM, David Rowland wrote:
> On Dec 12, 2009, at 9:32 AM, Ben Haller 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. :->
>>
>
> The problem with floating point is that the binary form almost always uses binary arithmetic. That means that a number like 0.1 has an infinitely repeating binary form. It is likely that converting the string "0.1" to a float and then back will yield "0.1" because it will be rounded, but there are marginal cases where you won't get what you put in.
>
> Also, the string can carry more information than the float. "8.336" is different from "8.336000". The latter implies that you know the precision to six decimal places, the former does not.
If I understand the question, it's not about converting an arbitrary decimal string to a float, but specifically a string that was generated from a float in the first place.
As glenn pointed out, that string most certainly *can* be a string that can be converted back to a bitwise equivalent of the original float. Ben's question was whether in practice it is guaranteed to be so, or whether stringValue uses a maximum number of decimal places that would lead to rounding error. I can't tell from a quick look at the docs.
--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