diff options
author | Paul Eggert <eggert@Penguin.CS.UCLA.EDU> | 2018-10-22 19:31:15 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2018-10-22 19:34:49 -0700 |
commit | a38128561757c82fbd088cba379b7a253558c7f1 (patch) | |
tree | 3583478b760707dca52441fef3a19dfc1e954baf /lisp/emacs-lisp | |
parent | 8602bd855904acc1966f1a94a008f91bb3f88c18 (diff) | |
download | emacs-a38128561757c82fbd088cba379b7a253558c7f1.tar.gz emacs-a38128561757c82fbd088cba379b7a253558c7f1.tar.bz2 emacs-a38128561757c82fbd088cba379b7a253558c7f1.zip |
Improve rounding in recent timer fix
* lisp/emacs-lisp/timer.el (timer-next-integral-multiple-of-time):
Use more-precise arithmetic to handle some boundary cases better
when rounding errors occur (Bug#33071).
* test/lisp/emacs-lisp/timer-tests.el:
(timer-next-integral-multiple-of-time-3):
New test, to test one of the boundary cases.
(timer-next-integral-multiple-of-time-2):
Redo so as to not assume a particular way of rounding 0.01.
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r-- | lisp/emacs-lisp/timer.el | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/lisp/emacs-lisp/timer.el b/lisp/emacs-lisp/timer.el index e140738d9f3..56323c85c2c 100644 --- a/lisp/emacs-lisp/timer.el +++ b/lisp/emacs-lisp/timer.el @@ -100,10 +100,16 @@ of SECS seconds since the epoch. SECS may be a fraction." (integerp (cdr time)) (< 0 (cdr time))) time (encode-time time 1000000000000))) + (ticks (car ticks-hz)) (hz (cdr ticks-hz)) - (s-ticks (round (* secs hz))) - (more-ticks (+ (car ticks-hz) s-ticks))) - (encode-time (cons (- more-ticks (% more-ticks s-ticks)) hz)))) + trunc-s-ticks) + (while (let ((s-ticks (* secs hz))) + (setq trunc-s-ticks (truncate s-ticks)) + (/= s-ticks trunc-s-ticks)) + (setq ticks (ash ticks 1)) + (setq hz (ash hz 1))) + (let ((more-ticks (+ ticks trunc-s-ticks))) + (encode-time (cons (- more-ticks (% more-ticks trunc-s-ticks)) hz))))) (defun timer-relative-time (time secs &optional usecs psecs) "Advance TIME by SECS seconds and optionally USECS microseconds |