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
# Float#ceil / #floor / #round / #truncate return typeIn CRuby these four methods choose their return class from the **runtimevalue** of the `ndigits` argument:| call | CRuby returns ||-----------------|--------------------|| `1.9.round` | `2` (Integer) || `1.9.round(0)` | `2` (Integer) || `1.9.round(-1)` | `0` (Integer) || `1.234.round(2)`| `1.23` (Float) |i.e. Integer when `ndigits <= 0`, Float when `ndigits > 0`.## Why spinel can't follow that exactlySpinel is an ahead-of-time compiler with static result types. A rulekeyed on the *value* of `ndigits` is unrepresentable in the generalcase:- `x.round(n)` with a variable `n` would need an Integer-or-Float return type chosen at runtime. Forcing it into a boxed/poly value to carry both would discard static typing for every expression the result flows into, which runs against spinel's static-typing design.- `x.round(*args)` / `x.round(*[])` can't be classified at all: whether any argument is actually present is only known at runtime.## The rule spinel usesThe return type is decided by argument **presence**, not value:- **no argument** -> `Integer` (`x.round`, `x.ceil`, ...)- **any argument present** -> `Float` (`x.round(0)`, `x.round(-1)`, `x.round(2)`, `x.round(n)`, `x.round(*args)`, ...)This is fully static, never boxes, and always produces the numericallycorrect value (`15.5.round(-1)` is `20.0`, which `== 20`). The singledivergence from CRuby is the *type label* on the ndigits form when`ndigits <= 0`: spinel returns a Float (`10.0`) where CRuby returns anInteger (`10`). The values are numerically equal; only `#class` and thedefault string form (`"10.0"` vs `"10"`) differ.Code that needs an Integer from a negative/zero ndigits can convertexplicitly: `x.round(-1).to_i`.The implementation lives in `infer_method_name_type` (spinel_analyze.rb)and `compile_float_method_expr` (spinel_codegen.rb); both branch only on`@nd_arguments[nid] >= 0`.spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/float_round_ndigits_return_type.rbaddedView file
Loading diff…
test/float_round_ndigits_return_type.rb.expectedaddedView file
Loading diff…
test/float_round_zero.rbmodifiedView file
Loading diff…
test/float_round_zero.rb.expectedmodifiedView file
Loading diff…