On Dec 12, 2009, at 1:07 PM, Andy Lee wrote:
> 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.

P.S.  If the answer is no, it seems to me there should be an easy 
counterexample, but my math isn't good enough to figure one out.  I've tried 
the following code with f = 0.1, 0.100001, and 0.12345, but I always get back a 
bitwise match for the original float, even when stringValue returns a rounded 
string.

- (void)testFloat:(float)f
{
    NSString * s = [[NSNumber numberWithFloat:f] stringValue];
    float f2 = [s floatValue];
    NSLog(@"%.20f, %@, %.20f", f, s, f2);
    char *cp1 = (char *)(&f);
    char *cp2 = (char *)(&f2);
    int i;
    for (i = 0; i < sizeof(float); i++)
    {
        NSLog(@"byte %d %@", i, (cp1[i] == cp2[i] ? @"MATCHES" : @"DOES NOT 
match"));
    }
}

--Andy

_______________________________________________

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