Skip to content

Commit a003e78

Browse files
committed
win: more reliable uv__hrtime precision
Eliminates floating-point operations that can cause precision loss. Thanks to @Arnavion for suggesting the fix: nodejs/node#1272 (comment)
1 parent 2eb1c18 commit a003e78

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/win/util.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static char *process_title;
5959
static CRITICAL_SECTION process_title_lock;
6060

6161
/* Interval (in seconds) of the high-resolution clock. */
62-
static double hrtime_interval_ = 0;
62+
static long hrtime_interval_ = 0;
6363

6464

6565
/*
@@ -75,9 +75,9 @@ void uv__util_init() {
7575
* and precompute its reciprocal.
7676
*/
7777
if (QueryPerformanceFrequency(&perf_frequency)) {
78-
hrtime_interval_ = 1.0 / perf_frequency.QuadPart;
78+
hrtime_interval_ = perf_frequency.QuadPart;
7979
} else {
80-
hrtime_interval_= 0;
80+
hrtime_interval_= 1;
8181
}
8282
}
8383

@@ -484,7 +484,7 @@ uint64_t uv__hrtime(double scale) {
484484
* performance counter interval, integer math could cause this computation
485485
* to overflow. Therefore we resort to floating point math.
486486
*/
487-
return (uint64_t) ((double) counter.QuadPart * hrtime_interval_ * scale);
487+
return (uint64_t) (counter.QuadPart * (long) scale / hrtime_interval_);
488488
}
489489

490490

0 commit comments

Comments
 (0)