Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pershiay

macrumors newbie
Original poster
Apr 18, 2012
1
0
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:

//[9]
float x=1234.5678;
NSLog(@"Reserve a space of 10, and show 2 significant digits.");
NSLog(@"%10.2f", x);

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!
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,062
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
Google search terms:
floating-point precision

Example from the first page of results:
http://idlastro.gsfc.nasa.gov/idl_html_help/Accuracy_and_Floating_Point_Operations.html

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


There's also this man page for Mac OS X:
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man3/float.3.html

Found by limiting the search to developer.apple.com. Google search terms:
floating-point precision site:developer.apple.com

EDIT
In addition to the man page, try looking up the word float in Xcode's builtin documentation.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.