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

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,616
6,145
Code:
- (void) updateTimeRemaining: (NSTimer *) timer
{
	if (blueButtonState == 2) // The timer should only be running if the game is still running.
	{
		timeRemaining = timeRemaining - 0.01;
		timeRemainingLabel.text = [NSString stringWithFormat: @"Time Remaining: %3.2f Seconds", timeRemaining];
		timeRemainingBar.progress = timeRemaining / [roundTime.text floatValue];
		
		if (timeRemaining == 0)
		{
			NSLog(@"Sees that the time limit is up.");
			
			if (numberOfTeams.selectedSegmentIndex == 0)
			{
				NSLog(@"Sees that it's time to restart.");
			}
		}
	}	
}

timeRemaining becomes 0 but it just keeps going without ever posting the NSLog message. timeRemaining is a float.
 

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,616
6,145
Because it is a float, you should compare it to 0.0. Also, I would suggest using <= 0.0, just in case it drops below 0.

TEG

Yeah, originally I was comparing it to 0.00 but that didn't work so I changed it to 0.

<= worked perfectly though (or close enough.)
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
As a general rule you never want to compare floats or doubles for equality since floating point numbers are not represented with perfect precision.
 

Ron C

macrumors member
Jul 18, 2008
61
0
Chicago-area
Code:
...
		timeRemaining = timeRemaining - 0.01;
...

timeRemaining becomes 0 but it just keeps going without ever posting the NSLog message. timeRemaining is a float.

The recommendation for <= is in the "rules of the road" for comparing floating-point types. When comparing two floating-point values, you can use an epsilon comparison (e.g., to tell if A is greater than B, you could use
Code:
if ((A-B)>epsilon) {
...
and to tell if A "equals" B you could use
Code:
if ((epsilon <= (A-B)) && ((A-B) <= epsilon)) {
...
where epsilon is your degree of precision. (For the A==B case, you can store A-B in a temporary but you still have to do the comparison twice.)

But I have a different question: is there a good reason that your counter is a floating-point and not an integer representation of that floating-point value. For example, reading the code you provided, I could use an int to perform the same thing (changed code highlighted in bold):

Code:
- (void) updateTimeRemaining: (NSTimer *) timer
{
	if (blueButtonState == 2) // The timer should only be running if the game is still running.
	{
		[B]timeRemainingINT[/B] = [B]timeRemainingINT--[/B];
		[B]float floatTime = 0.01*timeRemainingINT[/B];
		timeRemainingLabel.text = [NSString stringWithFormat: @"Time Remaining: %3.2f Seconds", [B]floatTime][/B];
		timeRemainingBar.progress = [B]floatTime[/B] / [roundTime.text floatValue];
		
		if ([B]timeRemainingINT == 0[/B])
		{
			NSLog(@"Sees that the time limit is up.");
			
			if (numberOfTeams.selectedSegmentIndex == 0)
			{
				NSLog(@"Sees that it's time to restart.");
			}
		}
	}	
}

I used a temporary for the float version of the timer because of too many years of paying attention to these kinds of things. Had it not been hundredth's of seconds as the unit of time, the multiplication factor would be that value (thousandth's or seventy-thirds or whatever).

RonC

(NOTE: I write Obj-C code like a C/C++ programmer because I'm a novice Obj-C programmer, but have been using C/C++ since 1984 when I was a newbie at Bell Labs. Yes, that means I'm an old fart.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.