diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index f620f7a6..18b2df0a 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -4866,10 +4866,10 @@ func (vm *VM) bootstrap() { return self }) vm.cFloat.define("to_i", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - return object.IntValue(int64(floatOf(self))) + return floatToInt(floatOf(self)) }) vm.cFloat.define("to_int", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - return object.IntValue(int64(floatOf(self))) + return floatToInt(floatOf(self)) }) vm.cFloat.define("ceil", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return floatRound(floatOf(self), args, math.Ceil) @@ -5253,7 +5253,29 @@ func floatRound(f float64, args []object.Value, fn func(float64) float64) object if ndigits > 0 { return object.Float(r) } - return object.IntValue(int64(r)) + return floatToInt(r) +} + +// floatToInt converts a Float to an Integer as MRI's flo2int does: a non-finite +// value (NaN or ±Infinity) has no integer and raises FloatDomainError; a finite +// value truncates toward zero into an Integer/Bignum. It backs the Integer-valued +// Float conversions (#to_i, #to_int, and #ceil/#floor/#round/#truncate without a +// positive ndigits), which previously returned 0 for NaN or crashed. +func floatToInt(f float64) object.Value { + if math.IsNaN(f) { + raise("FloatDomainError", "NaN") + } + if math.IsInf(f, 1) { + raise("FloatDomainError", "Infinity") + } + if math.IsInf(f, -1) { + raise("FloatDomainError", "-Infinity") + } + if f >= math.MinInt64 && f < math.MaxInt64 { + return object.IntValue(int64(f)) + } + bi, _ := big.NewFloat(math.Trunc(f)).Int(nil) + return object.NormInt(bi) } // clampCount validates a `first(n)`/`last(n)` count: it must be non-negative diff --git a/internal/vm/float_domain_test.go b/internal/vm/float_domain_test.go new file mode 100644 index 00000000..7115757b --- /dev/null +++ b/internal/vm/float_domain_test.go @@ -0,0 +1,55 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// TestFloatToIntegerDomain covers converting a Float to an Integer: a non-finite +// value (NaN / ±Infinity) raises FloatDomainError rather than returning 0 or +// crashing, and a finite value converts (as a Bignum when it exceeds a machine +// word). Verified against MRI Ruby 4.0.6. +func TestFloatToIntegerDomain(t *testing.T) { + cases := []struct{ src, want string }{ + // Finite conversions are unchanged; a positive ndigits keeps a Float NaN. + {`p 3.7.to_i`, "3\n"}, + {`p 3.7.round`, "4\n"}, + {`p 2.5.round`, "3\n"}, + {`p(-3.7.floor)`, "-4\n"}, + {`p 3.15.ceil`, "4\n"}, + {`p 3.99.truncate`, "3\n"}, + {`p 1234.5678.round(-2)`, "1200\n"}, + {`p (0.0/0.0).round(2)`, "NaN\n"}, + // A Float too large for a machine word becomes a Bignum. + {`p 1e20.to_i`, "100000000000000000000\n"}, + {`p (-1e20).to_i`, "-100000000000000000000\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } + + // A non-finite Float has no Integer value: FloatDomainError, whose message is + // the value's name (MRI). Previously NaN#round crashed and NaN#to_i/#ceil/ + // #floor silently returned 0. + errCases := []struct{ src, substr string }{ + {`Float::NAN.round`, "NaN"}, + {`Float::NAN.ceil`, "NaN"}, + {`Float::NAN.floor`, "NaN"}, + {`Float::NAN.to_i`, "NaN"}, + {`Float::NAN.to_int`, "NaN"}, + {`Float::NAN.truncate`, "NaN"}, + {`(1.0/0.0).round`, "Infinity"}, + {`(1.0/0.0).to_i`, "Infinity"}, + {`Float::INFINITY.floor`, "Infinity"}, + {`(-1.0/0.0).ceil`, "-Infinity"}, + {`(-1.0/0.0).to_i`, "-Infinity"}, + } + for _, c := range errCases { + err := runErr(t, c.src) + if err == nil || !strings.Contains(err.Error(), "FloatDomainError") || !strings.Contains(err.Error(), c.substr) { + t.Errorf("src=%q err=%v, want FloatDomainError containing %q", c.src, err, c.substr) + } + } +} diff --git a/internal/vm/numeric_edges.go b/internal/vm/numeric_edges.go index c04baeec..a8734cef 100644 --- a/internal/vm/numeric_edges.go +++ b/internal/vm/numeric_edges.go @@ -157,6 +157,14 @@ func dbl2ival(x float64) object.Value { // floRound is the full CRuby flo_round contract: it returns an Integer for // ndigits <= 0 and a Float for ndigits > 0, applying the selected half mode. func floRound(number float64, ndigits int, mode roundMode) object.Value { + if math.IsNaN(number) || math.IsInf(number, 0) { + // With a positive ndigits the result is a Float and keeps NaN/±Infinity; + // an Integer result has no value for a non-finite Float (FloatDomainError). + if ndigits > 0 { + return object.Float(number) + } + return floatToInt(number) + } if number == 0.0 { if ndigits > 0 { return object.Float(number) @@ -171,21 +179,20 @@ func floRound(number float64, ndigits int, mode roundMode) object.Value { if ndigits == 0 { return dbl2ival(applyRoundMode(mode, number, 1.0)) } - if !math.IsInf(number, 0) && !math.IsNaN(number) { - _, binexp := math.Frexp(number) - if floatRoundOverflow(ndigits, binexp) { - return object.Float(number) - } - if floatRoundUnderflow(ndigits, binexp) { - return object.Float(0) - } - if ndigits >= dblDig { // !ACCURATE_POW10 — fall back to exact rational - return floRoundByRational(number, ndigits, mode) - } - f := math.Pow(10, float64(ndigits)) - return object.Float(applyRoundMode(mode, number, f) / f) + // number is finite and non-zero here (NaN/±Infinity returned above), so + // ndigits > 0 always yields a Float. + _, binexp := math.Frexp(number) + if floatRoundOverflow(ndigits, binexp) { + return object.Float(number) + } + if floatRoundUnderflow(ndigits, binexp) { + return object.Float(0) + } + if ndigits >= dblDig { // !ACCURATE_POW10 — fall back to exact rational + return floRoundByRational(number, ndigits, mode) } - return object.Float(number) // NaN / ±Infinity + f := math.Pow(10, float64(ndigits)) + return object.Float(applyRoundMode(mode, number, f) / f) } // floRoundByRational rounds with ndigits beyond a double's accurate power of ten