Commit c6314783
c63147834a18Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
feat(codegen): Float#ceil(n)/floor(n)/round(n)/truncate(n) precision args
Bundle the four mirror-image precision-arg methods. The zero-arg
variants already shipped (f.round returns Integer); the precision
argument is the standard shift-by-pow(10,n)-then-back, lowering to
the same C math primitive as the zero-arg form except scaled.
For each of ceil / floor / round / truncate, in compile_float_method_expr, when an argument is present, lower to:
<C-fn>((rc) * pow(10, n)) / pow(10, n)
where is ceil / floor / round / trunc from <math.h>
respectively. The result keeps the divided-back form so the value is
a Float in the original scale (e.g. 3.14159.round(2) == 3.14).
infer_method_name_type now branches on argument presence: with an
arg, the result type is float so the local declaration uses
mrb_float; without an arg, the existing int rule still produces
the Integer-typed zero-arg form.
Test exercises the four methods at positive precision over typical floats, including cases where the underlying C primitive's behaviour matters (banker's-rounding cases like 1.5.round(1), negative input truncate, exact-precision boundaries like 1.001.ceil(2)).
Out of scope: n <= 0 cases that Ruby spec types as Integer.
The current bundle always returns Float when an argument is given,
so f.round(0) prints "3.0" instead of "3"; that's consistent with
the shift-by-pow(10,0) reduction (1.0 multiplier, divides back to
itself) but breaks parity with CRuby's type-switch. A separate
commit can teach the codegen to recognize literal-int-zero-or-
negative arguments and route to the existing zero-arg path.
Bootstrap: gen2.c == gen3.c. Tests: 210 pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- c63147834a18
- Parent
- 02005f26deba
spinel_codegen.rbmodifiedView file
if mname == "to_f" return "float" end if mname == "ceil" return "int" end if mname == "floor" return "int" end if mname == "round" # Float#ceil(n)/floor(n)/round(n)/truncate(n) with n given return # Float; zero-arg / Integer#ceil etc. return Integer. (truncate's arm # used to live next to nan?/infinite? — folded in here for one place # to update.) if mname == "ceil" || mname == "floor" || mname == "round" || mname == "truncate" if @nd_arguments[nid] >= 0 return "float" end return "int" end if mname == "upcase" if mname == "infinite?" return "int" end if mname == "truncate" return "int" end if mname == "tally" return "str_int_hash" end if mname == "to_i" return "(mrb_int)(" + rc + ")" end # ceil/floor/round/truncate with precision arg use a GCC stmt-expr so # the argument expression is compiled-and-emitted once on the Ruby # side and pow(10, n) is evaluated once at runtime — the original # form double-evaluated both, which broke any side-effecting arg. if mname == "ceil" if @nd_arguments[nid] >= 0 arg = compile_arg0(nid) return "({ double _f = pow(10, " + arg + "); ceil((" + rc + ") * _f) / _f; })" end return "(mrb_int)ceil(" + rc + ")" end if mname == "floor" if @nd_arguments[nid] >= 0 arg = compile_arg0(nid) return "({ double _f = pow(10, " + arg + "); floor((" + rc + ") * _f) / _f; })" end return "(mrb_int)floor(" + rc + ")" end if mname == "round" if @nd_arguments[nid] >= 0 arg = compile_arg0(nid) return "({ double _f = pow(10, " + arg + "); round((" + rc + ") * _f) / _f; })" end return "(mrb_int)round(" + rc + ")" end if mname == "abs" return "(isinf(" + rc + ") ? (" + rc + " < 0 ? -1 : 1) : 0)" end if mname == "truncate" if @nd_arguments[nid] >= 0 arg = compile_arg0(nid) return "({ double _f = pow(10, " + arg + "); trunc((" + rc + ") * _f) / _f; })" end return "(mrb_int)trunc(" + rc + ")" end if mname == "fdiv"test/float_precision_args.rbaddedView file
Loading diff…