[OCaml] High Intensity Training Online
fix compute the workout timer in floats
The elapsed timer read the workout start epoch and the current time as OCaml ints. js_of_ocaml ints are 31-bit, so epoch seconds (about 1.75e9) and getTime milliseconds overflow max_int and the subtraction produced a wrong count. Keep both times as floats and narrow only the small elapsed difference to an int, so the timer counts up correctly. Presentation only; no doctrine bearing.
lib/web/workout_client.ml
@@ -74,11 +74,16 @@
74
74
match Js.Opt.to_option (bar##getAttribute (Js.string "data-started")) with
75
75
| None -> ()
76
76
| Some started -> (
77
Added:
(* Epoch seconds exceed js_of_ocaml's 31-bit [int] range, so the
78
Added:
start and the current time are kept as floats; only the small
79
Added:
elapsed difference is narrowed to an [int]. Doing the subtraction
80
Added:
in [int] would overflow and yield a wrong count. *)
77
81
let started =
78
Removed:
try int_of_string (String.trim (Js.to_string started)) with _ -> 0
82
Added:
try float_of_string (String.trim (Js.to_string started))
83
Added:
with _ -> 0.
79
84
in
80
Removed:
let now = int_of_float (Js.to_float (new%js Js.date_now)##getTime) in
81
Removed:
let elapsed = (now / 1000) - started in
85
Added:
let now = Js.to_float (new%js Js.date_now)##getTime /. 1000. in
86
Added:
let elapsed = int_of_float (now -. started) in
82
87
match query_one document "[data-hito-workout-timer-value]" with
83
88
| None -> ()
84
89
| Some value ->