float == 0?

Discussion in 'iPhone/iPad Programming' started by ArtOfWarfare, Aug 21, 2008.

  1. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    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.
     
  2. TEG
    macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #2
    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
     
  3. thread starter macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    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.)
     
  4. macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    As a general rule you never want to compare floats or doubles for equality since floating point numbers are not represented with perfect precision.
     
  5. macrumors member

    Joined:
    Jul 18, 2008
    Location:
    Chicago-area
    #5
    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.)
     

Share This Page