Commit ad42e952

ad42e9525da9Merged

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

Description

Float ceil/floor/round/truncate: presence-based return type (closes #1063)

CRuby chooses Integer vs Float from the runtime sign of ndigits (Integer when <= 0, Float when > 0). Spinel is statically typed and can't key a result type on a runtime value: x.round(n) with a variable n, or x.round(*args), has no single statically-knowable return class, and boxing it to poly would discard static typing for everything the result flows into.

Decide the return type by argument presence instead of value: the no-arg form returns Integer, any ndigits argument returns Float. This is fully static, never boxes, and is always numerically correct (15.5.round(-1) is 20.0, which == 20). It removes the previous literal-0 special case (so x.round(0) is now Float too) and needs no runtime helpers. The only divergence from CRuby is the type label on the ndigits form when ndigits <= 0: spinel yields Float (10.0) where CRuby yields Integer (10); values are equal, only #class and the default string form differ. Documented in docs/FLOAT-ROUNDING.md.

Thanks to @neidiom (#1063) for surfacing the return-type gap and the reported cases; this lands a different, static-typing-first resolution than that PR's runtime-value-keyed approach.

Closes #1063

Co-Authored-By: neidiom [email protected] Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
ad42e9525da9
Parent
89ecfd6b3673

docs/FLOAT-ROUNDING.mdaddedView file

1+# Float#ceil / #floor / #round / #truncate return type
2+
3+In CRuby these four methods choose their return class from the **runtime
4+value** of the `ndigits` argument:
5+
6+| call | CRuby returns |
7+|-----------------|--------------------|
8+| `1.9.round` | `2` (Integer) |
9+| `1.9.round(0)` | `2` (Integer) |
10+| `1.9.round(-1)` | `0` (Integer) |
11+| `1.234.round(2)`| `1.23` (Float) |
12+
13+i.e. Integer when `ndigits <= 0`, Float when `ndigits > 0`.
14+
15+## Why spinel can't follow that exactly
16+
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:
20+
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.
27+
28+## The rule spinel uses
29+
30+The return type is decided by argument **presence**, not value:
31+
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)`, ...)
35+
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.
42+
43+Code that needs an Integer from a negative/zero ndigits can convert
44+explicitly: `x.round(-1).to_i`.
45+
46+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`.