3

I suppose to convert time_t value to double and then back: double to time_t.

In what cases I might lose data in converting?

unwind
  • 391,730
  • 64
  • 469
  • 606
BaruchLi
  • 1,021
  • 2
  • 11
  • 18

1 Answers1

5

... convert time_t value to double and then back ...
In what cases I might lose data in converting?

In some cases, nothing. time_t is some real type. C11 §7.27.1 3
Integer and real floating types are collectively called real types. C11 §6.2.5 17

So if time_t was the same as a double, no loss is expected in conversion.

time_t is usually represented as 32 or 64 bit integer. Typical double can encode all 53-bit unsigned values (or 54 bit signed integer values) exactly and so when time_t is a 32-bit integer, no conversion loss should occur. When time_t is a 64-bit integer, conversion to double begin to incurring rounding with values outside 253 in magnitude.

The 2nd conversion back step should never occur any loss if the double value was from an original time_t, but can incur undefined behavior if an arbitrary double was attempted to be changed to an integer.

Less common, time_t could be a float, long double, long long, etc. and similar issues apply.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256