Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 29, 2006, 06:51 AM   #1
caminoix
macrumors newbie
 
Join Date: Apr 2005
weird float problem

hello

please excuse my stupid question, i'm only beginning to learn objc and cocoa...

i ran recently into a strange problem. trying to solve it, i eventually ended up writing a tiny app with two text fields: in and out, and a button. when i enter some value in the in field and press the button, it's supposed to copy the value into the out field. it works ok with integers but with floats the values are not the same!
0,1 -> 0,1000000014901161
1,2 -> 1,200000047683716
2,3 -> 2,299999952316284

the code is as simple as:
Code:
[outField setFloatValue:[inField floatValue]];
just this single line, not counting the automatically generated parts.
just in case i attach the xcode project.

could you please explain me what i'm doing wrong?
Attached Files
File Type: zip test.zip (19.1 KB, 10 views)
caminoix is offline   0 Reply With Quote
Old Dec 29, 2006, 07:20 AM   #2
MrFrankly
macrumors regular
 
Join Date: Jan 2006
You're not doing anything wrong. Potentially a float should be able to represent an infinite amount of numbers. After all there are an infinite amount of possible numbers between 1 and 2 for example (1.1 , 1.12, 1.234, etc.). But computers have to use the discrete values 0 and 1 to make a representation of the infinite set of real numbers. So when you try to store 0.1 into a float it will try to store the best approximation. Which in your case will probably be 0.1000000014901161. This is quite a classic computer science problem.

It's also the reason why you should never try to compare a float directly with an other float. Because they might not be what you expect them to be. Always use a certain margin of error.

Try reading this paper about it. What Every Computer Scientist Should Know About Floating-Point Arithmetic. It's quite extensive and maybe even quite complex but it contains some very useful information when you're working with floating-point numbers. There are some other good documents about floating point numbers as well but try some search engine to find them. Knowing about them makes you a better programmer.
MrFrankly is offline   0 Reply With Quote
Old Dec 29, 2006, 01:44 PM   #3
caminoix
Thread Starter
macrumors newbie
 
Join Date: Apr 2005
i see. thanks a lot
it seems that i got led astray by using nstextfield. when i was writing anything in qt or gtk, i always used spinboxes to get numeric values from the user. but when i found the nstextfield in the apple's official xcode tutorial - the currency converter - i just thought this was the way to do it in cocoa.
thanks for a very fast reply
caminoix is offline   0 Reply With Quote
Old Dec 29, 2006, 05:00 PM   #4
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Try using -stringValue and -setStringValue and you won't have the problem with the float inaccuracies
caveman_uk is offline   0 Reply With Quote
Old Dec 31, 2006, 03:35 AM   #5
caminoix
Thread Starter
macrumors newbie
 
Join Date: Apr 2005
unfortunately, i don't have a mac at home and can't check it till tuesday but it seems to me that it should be quite impossible to use stringValue with float variables - so that i could perform arithmetic operations on them later on, or am i wrong?
caminoix is offline   0 Reply With Quote
Old Dec 31, 2006, 05:45 AM   #6
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Quote:
Originally Posted by caminoix View Post
unfortunately, i don't have a mac at home and can't check it till tuesday but it seems to me that it should be quite impossible to use stringValue with float variables - so that i could perform arithmetic operations on them later on, or am i wrong?
You can easily convert between the two. To get the float from a string use the -floatValue method. and you can convert back using [NSString stringWithFormat:@"%f", float] (you could also specify the number of decimal places in the string using the @"%.xf" notation). The point is that you keep the values as they were entered by the user until such time as your program needs to do something with them (like comparisons or arithmetic).
caveman_uk is offline   0 Reply With Quote
Old Dec 31, 2006, 06:40 AM   #7
caminoix
Thread Starter
macrumors newbie
 
Join Date: Apr 2005
ah, so that's the trick! i didn't know there were such fine conversion methods in cocoa. what you say sounds pleasantly easy. i'm new to mac, and i just can't stop being amazed by how easily things can get done.

thanks a lot for all your help
caminoix is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Weird problem EricandSuebee Mac mini 2 Apr 12, 2014 02:43 PM
iPad: My new Air didn't float, just drooped it. double329 iPad 15 Nov 5, 2013 10:40 PM
Are meeting reminders supposed to float? Steviejobz iOS 7 1 Sep 18, 2013 03:04 PM
using a float to display height animefx iPhone/iPad Programming 5 Jul 30, 2012 12:09 PM
stdarg.h and float.h not found in XCode 3.x maceatsapple iPhone/iPad Programming 0 Jul 30, 2012 09:08 AM

Forum Jump

All times are GMT -5. The time now is 06:23 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC