Commit e992d02e
e992d02efb02Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): Time#- subtracts int fields before float conversion
Follow-up to #237 — Gemini's medium-priority review feedback that
the float conversion should happen after the integer subtraction,
not before. With the original (mrb_float)a.tv_sec - (mrb_float)b.tv_sec
shape, both sides round to the nearest representable double before
the subtraction, so when tv_sec is past 2^53 (year 9876 onward,
or any synthetic large epoch) the low-order bits of the difference
are lost even though int64_t would represent them exactly.
Reordered to subtract first, convert second:
(mrb_float)(a.tv_sec - b.tv_sec) + (mrb_float)(a.tv_nsec - b.tv_nsec) / 1e9
Same shape applies to the nsec term. Practical impact today is minor (current epoch is ~1.7e9, well inside the 2^53 ≈ 9e15 window), but the int-first form is unconditionally safer at zero cost.
290 tests pass.
Refs #237
Co-Authored-By: Tomoyuki INOUE [email protected] Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- e992d02efb02
- Parent
- f809e55cf47f
spinel_codegen.rbmodifiedView file
return "sp_poly_sub(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(args_id)[0]) + ")" end # Hoist both sides into temps: each may be a fresh sp_time_now() # call, and tv_sec / tv_nsec are read twice each. # call, and tv_sec / tv_nsec are read twice each. Subtract the # integer fields *before* converting to float so we don't lose # bits when both timestamps are large (`tv_sec` past 2^53 seconds # falls outside double's exact-integer range, where the difference # `(double)a - (double)b` could miss the low-order bits even # though `(int64_t)(a - b)` is exact). if lt == "time" rhs_id = get_args(args_id)[0] if infer_type(rhs_id) == "time" return "({ sp_Time _a = " + compile_expr(recv) + "; sp_Time _b = " + compile_expr(rhs_id) + "; ((mrb_float)_a.tv_sec - (mrb_float)_b.tv_sec) + ((mrb_float)_a.tv_nsec - (mrb_float)_b.tv_nsec) / 1e9; })" return "({ sp_Time _a = " + compile_expr(recv) + "; sp_Time _b = " + compile_expr(rhs_id) + "; (mrb_float)(_a.tv_sec - _b.tv_sec) + (mrb_float)(_a.tv_nsec - _b.tv_nsec) / 1e9; })" end end r = compile_array_setop_expr(nid, recv, "difference", lt)