Here is the code
NSDate* d = [NSDate dateWithTimeIntervalSince1970:32.4560];
double ti = [d timeIntervalSince1970];
NSLog(@"Interval: %f %f %f %f",ti,32.4560,ti*1000.0,32.4560*1000.0);
the output is
Interval: 32.456000 32.456000 32455.999970 32456.000000
Why NSDate return the value which lose some precisions?
That’s not the problem of
NSDateitself. It’s in the nature of the floating point numbers themselves. I believeNSDatekeeps its date from the OS X epoch(2001), not the UNIX epoch (1970). Let the difference in the two epochs be x.Then what happens is this:
However, the floating point doesn’t have infinite precision. So,
+xand then-xcan introduce slight error in the calculation.For more, read e.g. this Wikipedia article.
If you use the OS X epoch, you get what you naively expect: