emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master 67475a5: Fix timer.el minor rounding error


From: Paul Eggert
Subject: [Emacs-diffs] master 67475a5: Fix timer.el minor rounding error
Date: Wed, 5 Sep 2018 19:21:47 -0400 (EDT)

branch: master
commit 67475a59e95919e2dbe25ae950450578afdfd0dc
Author: Paul Eggert <address@hidden>
Commit: Paul Eggert <address@hidden>

    Fix timer.el minor rounding error
    
    * lisp/emacs-lisp/timer.el (timer-next-integral-multiple-of-time):
    Fix rounding error by using integers rather than floats.
    * test/lisp/emacs-lisp/timer-tests.el (timer-test-multiple-of-time):
    New test.
---
 lisp/emacs-lisp/timer.el            | 10 +++++-----
 test/lisp/emacs-lisp/timer-tests.el |  5 +++++
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/lisp/emacs-lisp/timer.el b/lisp/emacs-lisp/timer.el
index 795554f..74d37b0 100644
--- a/lisp/emacs-lisp/timer.el
+++ b/lisp/emacs-lisp/timer.el
@@ -102,14 +102,14 @@ fire each time Emacs is idle for that many seconds."
   "Yield the next value after TIME that is an integral multiple of SECS.
 More precisely, the next value, after TIME, that is an integral multiple
 of SECS seconds since the epoch.  SECS may be a fraction."
-  (let* ((trillion 1e12)
+  (let* ((trillion 1000000000000)
         (time-sec (+ (nth 1 time)
-                     (* 65536.0 (nth 0 time))))
+                     (* 65536 (nth 0 time))))
         (delta-sec (mod (- time-sec) secs))
-        (next-sec (+ time-sec (ffloor delta-sec)))
-        (next-sec-psec (ffloor (* trillion (mod delta-sec 1))))
+        (next-sec (+ time-sec (floor delta-sec)))
+        (next-sec-psec (floor (* trillion (mod delta-sec 1))))
         (sub-time-psec (+ (or (nth 3 time) 0)
-                          (* 1e6 (nth 2 time))))
+                          (* 1000000 (nth 2 time))))
         (psec-diff (- sub-time-psec next-sec-psec)))
     (if (and (<= next-sec time-sec) (< 0 psec-diff))
        (setq next-sec-psec (+ sub-time-psec
diff --git a/test/lisp/emacs-lisp/timer-tests.el 
b/test/lisp/emacs-lisp/timer-tests.el
index 65e5dc9..fa92c1b 100644
--- a/test/lisp/emacs-lisp/timer-tests.el
+++ b/test/lisp/emacs-lisp/timer-tests.el
@@ -39,4 +39,9 @@
   (if (fboundp 'debug-timer-check)
       (should (debug-timer-check)) t))
 
+(ert-deftest timer-test-multiple-of-time ()
+  (should (equal
+           (timer-next-integral-multiple-of-time '(0 0 0 1) (1+ (ash 1 53)))
+           (list (ash 1 (- 53 16)) 1 0 0))))
+
 ;;; timer-tests.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]