Ok, this is so impossible that I don’t really know how to ask. I’m developing an iPhone app, and at one point I needed to use NSOperation for some background task. The problem is that even the simplest float calculation it’s trimmed after the point (eg 1/2= 1.0). Initially I thought that’s a concurrency problem but then I tried:
NSLog(@"%f", 1/2);
And it logged 1.00000. How is this even possible? Surely I do something wrong!
Any ideas anyone?
You are dividing two integers, which then rounds up to 1.
If you want to divide two floating point variables, either first save the values into a float and divide it or write
.0fat the end of your value to tell the compiler that you are working with a float. Eg:1.0f / 2.0fJust writing
1.0 / 2.0like suggested in the comments will result in a division of two doubles, as the compiler will default the values to double.