PDA

View Full Version : weird float problem

caminoix
Dec 29, 2006, 06:51 AM
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:
[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?

MrFrankly
Dec 29, 2006, 07:20 AM
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 (http://docs.sun.com/source/806-3568/ncg_goldberg.html). 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. ;)

caminoix
Dec 29, 2006, 01:44 PM
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 :)

caveman_uk
Dec 29, 2006, 05:00 PM
Try using -stringValue and -setStringValue and you won't have the problem with the float inaccuracies

caminoix
Dec 31, 2006, 03:35 AM
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?

caveman_uk
Dec 31, 2006, 05:45 AM
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).

caminoix
Dec 31, 2006, 06:40 AM
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 :)