MacRumors Forums weird float problem

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
 test.zip (19.1 KB, 14 views)
0
 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. 0
 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 0
 Dec 29, 2006, 05:00 PM #4 caveman_uk Guest     Join Date: Feb 2003 Location: Hitchin, Herts, UK Try using -stringValue and -setStringValue and you won't have the problem with the float inaccuracies 0
 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? 0
Dec 31, 2006, 05:45 AM   #6
caveman_uk
Guest

Join Date: Feb 2003
Location: Hitchin, Herts, UK
Quote:
 Originally Posted by caminoix 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).
0
 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 0

 MacRumors Forums