Re: NSNumber stringValue
Re: NSNumber stringValue
- Subject: Re: NSNumber stringValue
- From: "Lane Roathe" <email@hidden>
- Date: Sat, 12 Dec 2009 16:12:49 -0600
on Sat, Dec 12, 2009 Andy Lee may have said:
>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.
A float is 32 bits, so it can not represent in exact bits every integer
value, not to mention decimal places. Thus while a string can be
converted back to a bitwise equivalent of the original float, there is
no guarantee that it will be.
Also, keep in mind that internally Apple represents many values as
doubles. Thus some of the examples in the discussion, such as:
>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
are giving up 32 bits of precision for no reason. If you start with a
NSNumber, write that out as a string, then read it back in as a string,
convert back to an NSNumber, and then get that value as a float you are
not only converting the value back and forth between binary and string
representations, but also from 64bit to 32bit representations. The
binary conversion is likely to loose more precision than the string
conversions.
If you are starting with a value that can be represented as a double,
keep it in that format. Unless you are on a mobile device, GPU code or
other specialty code the use of doubles is not costly enough to worry
about and it gives you much better precision during calculations as well
as better retention when converting back and forth between string and
binary representations.
Lane Roathe, CEO
Ideas From the Deep, llc <http://www.ideasfromthedeep.com>
___________________________________________________________________
Life is cheap, but the accessories will break you.
_______________________________________________
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