8 ms·
Floating-point time loses the ability to represent milliseconds, microseconds, and nanoseconds exactly. C already has (since the 2011 standard) `struct timespe
by _kst_ 3y ago
Floating-point time loses the ability to represent milliseconds, microseconds, and nanoseconds exactly.
C already has (since the 2011 standard) `struct timespec`, a structure consisting of a `time_t` representing seconds since the epoch (typically 64 bits signed) and a `long` representing nanoseconds. It's admittedly awkward for arithmetic, but it doesn't overflow for another 292 billion years. It gives about 200 times better precision than 64-bit floating-point (for the interval from 2004 to 2038).
32-bit signed time_t is still a problem, particularly for embedded systems that are difficult to update. (A lot of such systems probably don't need to know the current time.)
A 64-bit signed integer representing nanoseconds since the epoch doesn't overflow until 2554.
Floating-point time is likely to be good enough in a lot of contexts -- but the fact that it's non-standard, unlike `time_t` and `struct timespec`, is a big disadvantage.
- JdeBP 3y agoMeasuring time as a combination of time_t and a fraction of a second count goes back far longer than that. 4.3BSD had time_t plus microseconds, for example.