• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSNumber stringValue
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSNumber stringValue


  • Subject: Re: NSNumber stringValue
  • From: Andy Lee <email@hidden>
  • Date: Sat, 12 Dec 2009 13:25:22 -0500

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 (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

  • Follow-Ups:
    • Re: NSNumber stringValue
      • From: Andy Lee <email@hidden>
References: 
 >NSNumber stringValue (From: RedleX Support <email@hidden>)
 >Re: NSNumber stringValue (From: Bryan Henry <email@hidden>)
 >Re: NSNumber stringValue (From: Andrew Farmer <email@hidden>)
 >Re: NSNumber stringValue (From: Ben Haller <email@hidden>)
 >Re: NSNumber stringValue (From: David Rowland <email@hidden>)
 >Re: NSNumber stringValue (From: Andy Lee <email@hidden>)

  • Prev by Date: Re: NSUserDefaults Croaks on Dictionary Containing NSNumber as Key
  • Next by Date: Re: NSNumber stringValue
  • Previous by thread: Re: NSNumber stringValue
  • Next by thread: Re: NSNumber stringValue
  • Index(es):
    • Date
    • Thread