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

aukemid

macrumors newbie
Original poster
Mar 26, 2010
24
0
I have the following code to start a timer:
Code:
self.timer = [NSTimer scheduledTimerWithTimeInterval: (60 / 118)
			target: self
			selector: @selector(timerFiredMethod)
			userInfo: nil
			repeats: YES];
This does not work!
The result of 60 / 118 = 0.5084745763. If I enter this result as interval it does work. Can someone tell me why this is?
 
60 and 118 are integer constants. Therefore, their type is int, and all arithmetic will be performed with integer quantities. So calculated as an integer, 60/118 is 0.

If you want fractional quantities, you must use numbers that have fractional parts, even if the fractional part is 0. A fractional constant is one containing a decimal point. Example: 60.0 / 118.0.

The fractional types are 'double' and 'float'. Here, I use the word "fractional" to mean "capable of representing a fraction". Clearly, the integer types (char, int, long) do not have this capability, or they wouldn't be integer types.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.