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
i.e. Integer when `ndigits <= 0`, Float when `ndigits > 0`.## Why spinel can't follow that exactly## The rule spinel usesSpinel is an ahead-of-time compiler with static result types. A rulekeyed on the *value* of `ndigits` is unrepresentable in the generalcase:The return type matches CRuby's value-based rule **whenever `ndigits`is a literal integer** -- the common case:- `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.- **no argument** -> `Integer` (`x.round`, `x.ceil`, ...)- **literal `ndigits <= 0`** -> `Integer` (`x.round(0)`, `x.round(-1)`)- **literal `ndigits > 0`** -> `Float` (`x.round(2)`)## The rule spinel usesFor an `ndigits <= 0` result the float formula is cast to `mrb_int`, soboth the value and `#class` match CRuby (`1234.5.round(-1)` is theInteger `1230`).The return type is decided by argument **presence**, not value:## The residual divergence- **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)`, ...)A **non-literal** `ndigits` keeps the static `Float` type: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.- `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 statically: whether any argument is present, and its sign, are runtime facts.Code that needs an Integer from a negative/zero ndigits can convertexplicitly: `x.round(-1).to_i`.So `x.round(n)` where `n` is a variable holding a non-positive valuereturns a Float (`10.0`) where CRuby returns an Integer (`10`). Thevalues are numerically equal; only `#class` and the default string form(`"10.0"` vs `"10"`) differ. Code that needs an Integer can convertexplicitly: `x.round(n).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`.and `compile_float_round_expr` (spinel_codegen.rb); both classify aliteral-integer `ndigits` by sign and fall back to Float otherwise.spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/float_round_ndigits_return_type.rbmodifiedView file
Loading diff…
test/float_round_ndigits_return_type.rb.expectedmodifiedView file
Loading diff…
test/float_round_value_based_type.rbaddedView file
Loading diff…
test/float_round_value_based_type.rb.expectedaddedView file
Loading diff…
test/float_round_zero.rbmodifiedView file
Loading diff…
test/float_round_zero.rb.expectedmodifiedView file
Loading diff…