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

@@ -2165,13 +2165,14 @@
21652165 if mname == "to_f"
21662166 return "float"
21672167 end
2168- if mname == "ceil"
2169- return "int"
2170- end
2171- if mname == "floor"
2172- return "int"
2173- end
2174- if mname == "round"
2168+ # Float#ceil(n)/floor(n)/round(n)/truncate(n) with n given return
2169+ # Float; zero-arg / Integer#ceil etc. return Integer. (truncate's arm
2170+ # used to live next to nan?/infinite? — folded in here for one place
2171+ # to update.)
2172+ if mname == "ceil" || mname == "floor" || mname == "round" || mname == "truncate"
2173+ if @nd_arguments[nid] >= 0
2174+ return "float"
2175+ end
21752176 return "int"
21762177 end
21772178 if mname == "upcase"
@@ -2580,9 +2581,6 @@
25802581 if mname == "infinite?"
25812582 return "int"
25822583 end
2583- if mname == "truncate"
2584- return "int"
2585- end
25862584 if mname == "tally"
25872585 return "str_int_hash"
25882586 end
@@ -16603,13 +16601,29 @@
1660316601 if mname == "to_i"
1660416602 return "(mrb_int)(" + rc + ")"
1660516603 end
16604+ # ceil/floor/round/truncate with precision arg use a GCC stmt-expr so
16605+ # the argument expression is compiled-and-emitted once on the Ruby
16606+ # side and pow(10, n) is evaluated once at runtime — the original
16607+ # form double-evaluated both, which broke any side-effecting arg.
1660616608 if mname == "ceil"
16609+ if @nd_arguments[nid] >= 0
16610+ arg = compile_arg0(nid)
16611+ return "({ double _f = pow(10, " + arg + "); ceil((" + rc + ") * _f) / _f; })"
16612+ end
1660716613 return "(mrb_int)ceil(" + rc + ")"
1660816614 end
1660916615 if mname == "floor"
16616+ if @nd_arguments[nid] >= 0
16617+ arg = compile_arg0(nid)
16618+ return "({ double _f = pow(10, " + arg + "); floor((" + rc + ") * _f) / _f; })"
16619+ end
1661016620 return "(mrb_int)floor(" + rc + ")"
1661116621 end
1661216622 if mname == "round"
16623+ if @nd_arguments[nid] >= 0
16624+ arg = compile_arg0(nid)
16625+ return "({ double _f = pow(10, " + arg + "); round((" + rc + ") * _f) / _f; })"
16626+ end
1661316627 return "(mrb_int)round(" + rc + ")"
1661416628 end
1661516629 if mname == "abs"
@@ -16625,6 +16639,10 @@
1662516639 return "(isinf(" + rc + ") ? (" + rc + " < 0 ? -1 : 1) : 0)"
1662616640 end
1662716641 if mname == "truncate"
16642+ if @nd_arguments[nid] >= 0
16643+ arg = compile_arg0(nid)
16644+ return "({ double _f = pow(10, " + arg + "); trunc((" + rc + ") * _f) / _f; })"
16645+ end
1662816646 return "(mrb_int)trunc(" + rc + ")"
1662916647 end
1663016648 if mname == "fdiv"