0

Hi I am doing mathematical operations with NSTimeInterval(which is double type) with one long variable

NSTimeInterval  timeDifference =[endDateTime timeIntervalSinceDate:startDateTime];

  long duration= timeDifference-[dauseDuration longValue];
  NSString *eventDuration=[NSString stringWithFormat:@"%ld",duration];
  NSLog(@"Duration  :     %@",eventDuration);

where I am getting 1 second difference in result,can any one know's thanks in advance !!!

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
Sandeep Khade
  • 2,832
  • 3
  • 21
  • 37
  • 3
    Likely a rounding issue; standard double-to-int conversion uses the floor value. Try rounding before the conversion. – Dave Dec 20 '13 at 07:09

1 Answers1

0

NSTimeInterval can hold only NSUInteger values. When you subtract a 'long' from a 'integer' value, the result will always be a 'integer'. The long will loose its precision. Try using,

long duration= (long)timeDifference-[dauseDuration longValue];

(But not sure, if you can make up the 1 second difference).

Karthik
  • 88
  • 5
  • thanks @karthik for reply I know solution just I wanted to know reason behind it, my assumption is due to rounding it is happening just wanted to verify. – Sandeep Khade Dec 20 '13 at 12:58