Commit 04605518

046055181245Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 9 files · +2 added · ~7 modified

Description

Make Float rounding return type value-based for literal ndigits

CRuby's Float#round/ceil/floor/truncate return an Integer when ndigits <= 0 (or absent) and a Float when > 0 -- keyed on the ndigits value. Spinel was presence-based (any arg => Float), a deliberate divergence because a runtime ndigits value isn't statically visible.

For a literal-integer ndigits -- the common case -- the sign is known at compile time, so match CRuby: classify Integer (ndigits <= 0) vs Float (> 0), and for the Integer case cast the float formula to mrb_int so both value and #class agree. A non-literal ndigits keeps the static Float type (the remaining divergence, documented in FLOAT-ROUNDING.md).

1.9.round(0) # => 2 (Integer; was 2.0 Float) 1234.5.round(-1) # => 1230 (Integer; was 1230.0 Float) 1.234.round(2) # => 1.23 (Float; unchanged)

Codegen is factored into compile_float_round_expr. The two tests that pinned the old presence-based labels are updated to the CRuby-matching values.

Fixes #1183

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
046055181245
Parent
b29baef903c2

docs/FLOAT-ROUNDING.mdmodifiedView file

@@ -12,37 +12,36 @@
1212
1313 i.e. Integer when `ndigits <= 0`, Float when `ndigits > 0`.
1414
15-## Why spinel can't follow that exactly
15+## The rule spinel uses
1616
17-Spinel is an ahead-of-time compiler with static result types. A rule
18-keyed on the *value* of `ndigits` is unrepresentable in the general
19-case:
17+The return type matches CRuby's value-based rule **whenever `ndigits`
18+is a literal integer** -- the common case:
2019
21-- `x.round(n)` with a variable `n` would need an Integer-or-Float
22- return type chosen at runtime. Forcing it into a boxed/poly value to
23- carry both would discard static typing for every expression the
24- result flows into, which runs against spinel's static-typing design.
25-- `x.round(*args)` / `x.round(*[])` can't be classified at all: whether
26- any argument is actually present is only known at runtime.
20+- **no argument** -> `Integer` (`x.round`, `x.ceil`, ...)
21+- **literal `ndigits <= 0`** -> `Integer` (`x.round(0)`, `x.round(-1)`)
22+- **literal `ndigits > 0`** -> `Float` (`x.round(2)`)
2723
28-## The rule spinel uses
24+For an `ndigits <= 0` result the float formula is cast to `mrb_int`, so
25+both the value and `#class` match CRuby (`1234.5.round(-1)` is the
26+Integer `1230`).
2927
30-The return type is decided by argument **presence**, not value:
28+## The residual divergence
3129
32-- **no argument** -> `Integer` (`x.round`, `x.ceil`, ...)
33-- **any argument present** -> `Float` (`x.round(0)`, `x.round(-1)`,
34- `x.round(2)`, `x.round(n)`, `x.round(*args)`, ...)
30+A **non-literal** `ndigits` keeps the static `Float` type:
3531
36-This is fully static, never boxes, and always produces the numerically
37-correct value (`15.5.round(-1)` is `20.0`, which `== 20`). The single
38-divergence from CRuby is the *type label* on the ndigits form when
39-`ndigits <= 0`: spinel returns a Float (`10.0`) where CRuby returns an
40-Integer (`10`). The values are numerically equal; only `#class` and the
41-default string form (`"10.0"` vs `"10"`) differ.
32+- `x.round(n)` with a variable `n` would need an Integer-or-Float
33+ return type chosen at runtime. Forcing it into a boxed/poly value to
34+ carry both would discard static typing for every expression the
35+ result flows into, which runs against spinel's static-typing design.
36+- `x.round(*args)` / `x.round(*[])` can't be classified statically:
37+ whether any argument is present, and its sign, are runtime facts.
4238
43-Code that needs an Integer from a negative/zero ndigits can convert
44-explicitly: `x.round(-1).to_i`.
39+So `x.round(n)` where `n` is a variable holding a non-positive value
40+returns a Float (`10.0`) where CRuby returns an Integer (`10`). The
41+values are numerically equal; only `#class` and the default string form
42+(`"10.0"` vs `"10"`) differ. Code that needs an Integer can convert
43+explicitly: `x.round(n).to_i`.
4544
4645 The implementation lives in `infer_method_name_type` (spinel_analyze.rb)
47-and `compile_float_method_expr` (spinel_codegen.rb); both branch only on
48-`@nd_arguments[nid] >= 0`.
46+and `compile_float_round_expr` (spinel_codegen.rb); both classify a
47+literal-integer `ndigits` by sign and fall back to Float otherwise.