为什么 difftime() return 是双倍的?
Why does difftime() return a double?
double difftime(time_t time1, time_t time0);
The difftime()
function returns the number of seconds elapsed between
time time1
and time time0
, represented as a double
.
既然'number of seconds'不需要浮点数,为什么这个函数return是double
?
这点documentation说的比较清楚:
On POSIX systems, time_t is measured in seconds, and difftime is equivalent to arithmetic subtraction, but C and C++ allow fractional units for time_t.
虽然 POSIX requires time_t
to be an integer type,但对于非 POSIX 系统,这可能会 return 小数秒。
C 允许使用各种标量数(整数、浮点数)来表示时间。它需要是“......能够代表时间的真实类型”C11§7.27.1 3,
The range and precision of times representable in clock_t and time_t
are
implementation-defined. C11dr §7.27.1 4
2 个 time_t
值之间的差异,因为 double
提供了广泛的范围和精度。
OP, "Since 'number of seconds' doesn't require floating-point numbers, why does this function return a double?
[编辑] Linux/posix 可能不会使用几分之一秒,但其他系统已经这样做了。定义 difftime()
的 C 标准选择 double
并容纳秒的整数累加以及其他 OS 实现。
double difftime(time_t time1, time_t time0);
The
difftime()
function returns the number of seconds elapsed between timetime1
and timetime0
, represented as adouble
.
既然'number of seconds'不需要浮点数,为什么这个函数return是double
?
这点documentation说的比较清楚:
On POSIX systems, time_t is measured in seconds, and difftime is equivalent to arithmetic subtraction, but C and C++ allow fractional units for time_t.
虽然 POSIX requires time_t
to be an integer type,但对于非 POSIX 系统,这可能会 return 小数秒。
C 允许使用各种标量数(整数、浮点数)来表示时间。它需要是“......能够代表时间的真实类型”C11§7.27.1 3,
The range and precision of times representable in
clock_t and time_t
are implementation-defined. C11dr §7.27.1 4
2 个 time_t
值之间的差异,因为 double
提供了广泛的范围和精度。
OP, "Since 'number of seconds' doesn't require floating-point numbers, why does this function return a double?
[编辑] Linux/posix 可能不会使用几分之一秒,但其他系统已经这样做了。定义 difftime()
的 C 标准选择 double
并容纳秒的整数累加以及其他 OS 实现。