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

caminoix

macrumors newbie
Original poster
Apr 13, 2005
6
0
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?
 

Attachments

  • test.zip
    19.1 KB · Views: 89

MrFrankly

macrumors regular
Jan 11, 2006
112
0
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. ;)
 

caminoix

macrumors newbie
Original poster
Apr 13, 2005
6
0
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

macrumors newbie
Original poster
Apr 13, 2005
6
0
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

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
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:mad:"%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

macrumors newbie
Original poster
Apr 13, 2005
6
0
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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.