views:

144

answers:

2

Hi Guys,

Ok, the problem is that when my app runs on the simulator it seems to work fine. On the device, however it does not update all the labels. I think the error is somewhere here:

- (IBAction)buttonclick1 {  

     self.startDate = [NSDate date];  
     double value = [self Level];  
     double value2 = [self Level2];  


if ((value2 - value) >= 3.0) {

 ifWork.text = @"DONE!";
 ifWork.textColor = [UIColor yellowColor];

 float noSeconds = (float) [self.startDate timeIntervalSinceNow]; 
  }
}

I am new to this game. What I am looking for, is when the button is pressed, it times how long it takes for an event to happen. I then would like to use this time in calculations.

I know the IF statement is correct as it works on the simulator. However on the device, when value and value2 differ by 3 and then the button is pressed, nothing happens (the label ifWork doesn't change!).

Any help would be much appreciated,

Stu

+2  A: 

Did you run the code in the debugger to see what happens? Maybe if the values depend on anything like CPU or network performance you may experience notable differences between the simulator and the actual device, so you cannot be sure that what works on the simulator works on the device.

frenetisch applaudierend
Thanks for your response and ideas, the code is linked to a value which continually decreases. It doesn't depend on network or CPU. Is the timer correct? The label doesn't even change... something is up with that IF!Stu
Stumf
To calculate the time elapsed I would invert the second time stamp to [[NSDate date] timeIntervalSinceDate:self.startDate]; otherwise you're getting a negative value. If you put a breakpoint at the if(...) statement and run the program on the simulator, do you get the expected values?
frenetisch applaudierend
Sorry, I made a mistake in the previous post. The interesting question of course is, wether you are getting the correct values on the actual device.
frenetisch applaudierend
Tried on simulator with break point and got expected results, tried again on device, still no luck. That label will indicate whether or not the IF has worked...still no change to "DONE". If you get the chance have a look at the link I provided above. Thanks for all of your help thus far.
Stumf
Maybe something to do with isEqual:? I have read that that should be used. I find that people say isEqualToString: is a common solution... but I am unsure how I can use it.
Stumf
isEqual: is only used with objects, you are using double which is a scalar. You could wrap your values in NSNumber objects, but I'm not sure this would help at all. Another possibility is that your problem is related to floating point precision. Take a look at http://stackoverflow.com/questions/1614533/strange-problem-comparing-floats-in-objective-c maybe this helps your case (especially the answer containing macros to compare floats).
frenetisch applaudierend
Still not got this working. I need to try EPSILON, but don't know how to go about it for my problem. Any help on this one? Thanks
Stumf
foats or doubles are not really exact because you cannot express every possible floating point number with 32 or 64 bit. So instead of directly comparing two floats you instead compare their difference to a very small number which is called EPSILON and represents the smallest representable float.
frenetisch applaudierend
Many thanks to frenetisch applaudierend and Matt Gallagher for your help on this. It is now working. I look forward to when I am good enough to give a litle back to this fantastic community by answering questions. Thanks again,Stu
Stumf
Always glad to help. Could you post a comment with a description of what was wrong? I can add it then to the response, so anyone who is looking for an answer does not have to drill down in the comments (if this is ok).
frenetisch applaudierend
I believe there were two problems here. The first being that my method of comparison should have been the EPSILON one, rather than what I was attempting to use. The other thing was this:Value2 (the one that should not update) was set up so that the label it was in did not update, yet the value itself did. This meant that the two values were never 3 apart. Once again many thanks to all!
Stumf
+1  A: 

There could be a few reasons.

  • Level1 or Level2 results could be different on device (using uninitialized memory which is different on device
  • value2 - value1 could be very close to 3.0 but just less than (doubles are often a little imprecise)
  • ifWork could be improperly set on the device (i.e. nil) so nothing happens.

The best solution is to debug on the device (step over this code line by line) and check if any of these things are true.

Matt Gallagher
Would the fact that I have used >=3.0 not mean that precision is not as big a deal? It doesn't need to be exactly 3.0.
Stumf
If you've performed a calculation that you expect to give 3.0 but the result in the double is actually 2.99999999999998756 then that makes a difference. If you don't care about edge cases, then it doesn't matter. If you do care about edge cases, then you might need to use >= 2.995 or something similar.
Matt Gallagher