PDA

View Full Version : float == 0?




ArtOfWarfare
Aug 21, 2008, 02:25 PM
- (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.



TEG
Aug 21, 2008, 02:33 PM
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

ArtOfWarfare
Aug 21, 2008, 02:48 PM
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
Aug 21, 2008, 03:24 PM
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
Aug 21, 2008, 06:57 PM
...
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 if ((A-B)>epsilon) {
... and to tell if A "equals" B you could use 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):

- (void) updateTimeRemaining: (NSTimer *) timer
{
if (blueButtonState == 2) // The timer should only be running if the game is still running.
{
timeRemainingINT = timeRemainingINT--;
float floatTime = 0.01*timeRemainingINT;
timeRemainingLabel.text = [NSString stringWithFormat: @"Time Remaining: %3.2f Seconds", floatTime];
timeRemainingBar.progress = floatTime / [roundTime.text floatValue];

if (timeRemainingINT == 0)
{
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.)