New to Objective-C Question

Discussion in 'iOS Programming' started by pershiay, Apr 18, 2012.

  1. pershiay macrumors newbie

    Apr 18, 2012
    This is probably a super dumb question, but I'm completely new to Objective-C and XCode and the most complicated programming I've ever done was using AS3.

    Anyway, I was following along with Cocoa Lab's "Become An XCoder" eBook and was working with Chapter 4: Printing On Screen and decided to plug in this bit of code into XCode 3.6 to test it out:

    It worked fine, of course so I decided to play around a bit and see what it did if I added characters to x to make it greater than 10 characters and ran the program. It came up truncated, but rounded it oddly.

    For example, I put in: 1234567.5678 and got something like: 1234567.62

    There must be a reason why it's rounding .5678 to .62, but I couldn't understand it. When I changed the numbers, I also got some odd rounding. This is probably something to do with how Objective-C works and I'll likely figure it out as I continue to learn more. But it's a curiosity that's bugged me since I can't figure it out and would love an explanation. Keeping in mind that I'm a total newbie. Thanks!
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    I'd say this has more to do with C than obj-C.

    My only guess is that the "float" type has inadequate bits in it to store the exact value. Maybe changing it to a "double" would work better?
  3. chown33, Apr 18, 2012
    Last edited: Apr 18, 2012

    chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Google search terms:
    floating-point precision

    Example from the first page of results:

    A lot of the other results have longer explanations. Some, much longer.

    There's also this man page for Mac OS X:

    Found by limiting the search to Google search terms:
    floating-point precision

    In addition to the man page, try looking up the word float in Xcode's builtin documentation.

Share This Page