Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSString floatValue help

NSString floatValue help
Thread Tools
Fresh-Faced Recruit
Join Date: Jan 2010
Status: Offline
Reply With Quote
Jan 23, 2010, 01:32 PM
 
Hello everyone, I'm relatively new to obj-c and I'm having a strange problem with string to float conversion:

NSString *str=@"1.1";
float f=[str floatValue];

This sets the value of f to: 1.10000002. what's up with the 2 and how do I make f equal to str exactly?
Thanks in advance
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 25, 2010, 12:37 PM
 
Binary floating-point cannot precisely represent 1.1 — it's an infinitely repeating sequence of digits — so it uses a number very close to 1.1 that rounds to 1.10000002 in decimal. You should never, ever count on floating-point numbers to be accurate, and you shouldn't test for equality either — test whether a number is within a tiny range around the number you're interested in (e.g. x - 0.0000001 < n && x + 0.0000001 > n).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:54 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2