diff --git a/internal/vm/numeric_numerator_internal_test.go b/internal/vm/numeric_numerator_internal_test.go new file mode 100644 index 00000000..29c841a2 --- /dev/null +++ b/internal/vm/numeric_numerator_internal_test.go @@ -0,0 +1,48 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestNumericNumeratorDenominator covers #numerator and #denominator on Integer, +// Float, and the Numeric base (via Rational): an Integer is its own numerator +// over denominator 1 (Bignum included), a finite Float uses its exact rational +// value, and a non-finite Float (Infinity/NaN) returns itself as the numerator +// and 1 as the denominator. Verified against ruby 4.0.6. +func TestNumericNumeratorDenominator(t *testing.T) { + cases := []struct{ src, want string }{ + // Integer. + {`p 3.numerator`, `3`}, + {`p 3.denominator`, `1`}, + {`p(-5.numerator)`, `-5`}, + {`p(-5.denominator)`, `1`}, + {`p (2**70).numerator`, `1180591620717411303424`}, + {`p (2**70).denominator`, `1`}, + // Float, finite. + {`p 1.5.numerator`, `3`}, + {`p 1.5.denominator`, `2`}, + {`p 0.75.numerator`, `3`}, + {`p 0.75.denominator`, `4`}, + {`p 2.0.numerator`, `2`}, + {`p 2.0.denominator`, `1`}, + // Float, non-finite: no rational form. + {`p (1.0/0).numerator`, `Infinity`}, + {`p (1.0/0).denominator`, `1`}, + {`p (-1.0/0).numerator`, `-Infinity`}, + {`p (0.0/0).numerator.nan?`, `true`}, + {`p (0.0/0).denominator`, `1`}, + // Numeric base delegates to #to_r (Rational has its own, so this exercises + // the inherited path through a Numeric subclass that defines only #to_r). + {`p (3r/4).numerator`, `3`}, + {`p (3r/4).denominator`, `4`}, + {`class N < Numeric; def to_r; Rational(5, 6); end; end; p N.new.numerator`, `5`}, + {`class N < Numeric; def to_r; Rational(5, 6); end; end; p N.new.denominator`, `6`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +} diff --git a/internal/vm/prelude.rb b/internal/vm/prelude.rb index b2c4f326..c7645134 100644 --- a/internal/vm/prelude.rb +++ b/internal/vm/prelude.rb @@ -885,6 +885,41 @@ def cycle(n = nil) # (Integer/Float/Rational/Complex inherit it); String mixes it in directly. class Numeric include Comparable + + # A Numeric is treated as its exact rational value: #numerator and + # #denominator delegate to that of #to_r. Integer and Float override these + # below with cheaper, non-finite-aware versions. + def numerator + to_r.numerator + end + + def denominator + to_r.denominator + end +end + +class Integer + # An Integer is its own numerator over a denominator of 1. + def numerator + self + end + + def denominator + 1 + end +end + +class Float + # A finite Float uses the numerator/denominator of its exact rational value; a + # non-finite one (Infinity or NaN) has no rational form, so #numerator returns + # the Float itself and #denominator returns 1, matching MRI. + def numerator + finite? ? to_r.numerator : self + end + + def denominator + finite? ? to_r.denominator : 1 + end end class String diff --git a/internal/vm/prelude_frozen_gen.go b/internal/vm/prelude_frozen_gen.go index 7abb90d7..b36fddcf 100644 --- a/internal/vm/prelude_frozen_gen.go +++ b/internal/vm/prelude_frozen_gen.go @@ -10,8 +10,8 @@ import ( func embeddedPrelude() *bytecode.ISeq { return &bytecode.ISeq{ Name: "
", - Insns: []bytecode.Instr{{Op: 43}, {Op: 9}, {Op: 43, A: 1, B: 1}, {Op: 9}, {Op: 42, A: 2, B: 2}, {Op: 9}, {Op: 42, A: 3, B: 3}, {Op: 9}, {Op: 42, A: 4, B: 4}, {Op: 9}, {Op: 42, A: 5, B: 5}, {Op: 9}, {Op: 42, A: 6, B: 6}, {Op: 9}, {Op: 42, A: 7, B: 7}, {Op: 9}, {Op: 42, A: 8, B: 8}, {Op: 9}, {Op: 43, A: 9, B: 9}, {Op: 9}, {Op: 42, A: 10, B: 10}, {Op: 9}, {Op: 43, A: 11, B: 11}, {Op: 9}, {Op: 43, A: 12, B: 12}, {Op: 9}, {Op: 42, A: 13, B: 13}, {Op: 9}, {Op: 42, A: 14, B: 14}, {Op: 9}, {Op: 44, A: 15, B: 15}, {Op: 9}, {Op: 42, A: 16, B: 16}, {Op: 9}, {Op: 43, A: 17, B: 17}, {Op: 9}, {Op: 42, A: 18, B: 18}, {Op: 9}, {Op: 43, A: 19, B: 19}, {Op: 9}, {Op: 43, A: 20, B: 20}, {Op: 9}, {Op: 42, A: 21, B: 21}, {Op: 9}, {Op: 33, A: 21}, {Op: 41, A: 22}, {Op: 9}, {Op: 42, A: 23, B: 22}, {Op: 53}}, - Names: []string{"Comparable", "Enumerable", "Numeric", "String", "Symbol", "Array", "Range", "Hash", "Set", "Gem", "OpenStruct", "Forwardable", "SingleForwardable", "Delegator", "SimpleDelegator", "DelegateClass", "Pathname", "Singleton", "ERB", "Slim", "Haml", "OptionParser", "OptParse", "Tempfile"}, + Insns: []bytecode.Instr{{Op: 43}, {Op: 9}, {Op: 43, A: 1, B: 1}, {Op: 9}, {Op: 42, A: 2, B: 2}, {Op: 9}, {Op: 42, A: 3, B: 3}, {Op: 9}, {Op: 42, A: 4, B: 4}, {Op: 9}, {Op: 42, A: 5, B: 5}, {Op: 9}, {Op: 42, A: 6, B: 6}, {Op: 9}, {Op: 42, A: 7, B: 7}, {Op: 9}, {Op: 42, A: 8, B: 8}, {Op: 9}, {Op: 42, A: 9, B: 9}, {Op: 9}, {Op: 42, A: 10, B: 10}, {Op: 9}, {Op: 43, A: 11, B: 11}, {Op: 9}, {Op: 42, A: 12, B: 12}, {Op: 9}, {Op: 43, A: 13, B: 13}, {Op: 9}, {Op: 43, A: 14, B: 14}, {Op: 9}, {Op: 42, A: 15, B: 15}, {Op: 9}, {Op: 42, A: 16, B: 16}, {Op: 9}, {Op: 44, A: 17, B: 17}, {Op: 9}, {Op: 42, A: 18, B: 18}, {Op: 9}, {Op: 43, A: 19, B: 19}, {Op: 9}, {Op: 42, A: 20, B: 20}, {Op: 9}, {Op: 43, A: 21, B: 21}, {Op: 9}, {Op: 43, A: 22, B: 22}, {Op: 9}, {Op: 42, A: 23, B: 23}, {Op: 9}, {Op: 33, A: 23}, {Op: 41, A: 24}, {Op: 9}, {Op: 42, A: 25, B: 24}, {Op: 53}}, + Names: []string{"Comparable", "Enumerable", "Numeric", "Integer", "Float", "String", "Symbol", "Array", "Range", "Hash", "Set", "Gem", "OpenStruct", "Forwardable", "SingleForwardable", "Delegator", "SimpleDelegator", "DelegateClass", "Pathname", "Singleton", "ERB", "Slim", "Haml", "OptionParser", "OptParse", "Tempfile"}, NumRequired: 0, SplatIndex: -1, KwRestSlot: -1, @@ -2216,14 +2216,112 @@ func embeddedPrelude() *bytecode.ISeq { }, &bytecode.ISeq{ Name: "", - Insns: []bytecode.Instr{{Op: 5}, {Op: 33}, {Op: 30, A: 1, B: 1}, {Op: 53}}, - Names: []string{"Comparable", "include"}, + Insns: []bytecode.Instr{{Op: 5}, {Op: 33}, {Op: 30, A: 1, B: 1}, {Op: 9}, {Op: 44, A: 2}, {Op: 9}, {Op: 44, A: 3, B: 1}, {Op: 53}}, + Names: []string{"Comparable", "include", "numerator", "denominator"}, NumRequired: 0, SplatIndex: -1, KwRestSlot: -1, BlockSlot: -1, NumLocals: 0, - Super: "", + Children: []*bytecode.ISeq{ + &bytecode.ISeq{ + Name: "numerator", + Insns: []bytecode.Instr{{Op: 5}, {Op: 30}, {Op: 30, A: 1, Flags: 1}, {Op: 53}}, + Names: []string{"to_r", "numerator"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Super: "", + }, + &bytecode.ISeq{ + Name: "denominator", + Insns: []bytecode.Instr{{Op: 5}, {Op: 30}, {Op: 30, A: 1, Flags: 1}, {Op: 53}}, + Names: []string{"to_r", "denominator"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Super: "", + }, + }, + Super: "", + }, + &bytecode.ISeq{ + Name: "", + Insns: []bytecode.Instr{{Op: 44}, {Op: 9}, {Op: 44, A: 1, B: 1}, {Op: 53}}, + Names: []string{"numerator", "denominator"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Children: []*bytecode.ISeq{ + &bytecode.ISeq{ + Name: "numerator", + Insns: []bytecode.Instr{{Op: 5}, {Op: 53}}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Super: "", + }, + &bytecode.ISeq{ + Name: "denominator", + Insns: []bytecode.Instr{{Op: 1}, {Op: 53}}, + Consts: []object.Value{ + object.IntValue(1), + }, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Super: "", + }, + }, + Super: "", + }, + &bytecode.ISeq{ + Name: "", + Insns: []bytecode.Instr{{Op: 44}, {Op: 9}, {Op: 44, A: 1, B: 1}, {Op: 53}}, + Names: []string{"numerator", "denominator"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Children: []*bytecode.ISeq{ + &bytecode.ISeq{ + Name: "numerator", + Insns: []bytecode.Instr{{Op: 5}, {Op: 30}, {Op: 28, A: 7}, {Op: 5}, {Op: 30, A: 1}, {Op: 30, A: 2, Flags: 1}, {Op: 26, A: 8}, {Op: 5}, {Op: 53}}, + Names: []string{"finite?", "to_r", "numerator"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Super: "", + }, + &bytecode.ISeq{ + Name: "denominator", + Insns: []bytecode.Instr{{Op: 5}, {Op: 30}, {Op: 28, A: 7}, {Op: 5}, {Op: 30, A: 1}, {Op: 30, A: 2, Flags: 1}, {Op: 26, A: 8}, {Op: 1}, {Op: 53}}, + Consts: []object.Value{ + object.IntValue(1), + }, + Names: []string{"finite?", "to_r", "denominator"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Super: "", + }, + }, + Super: "", }, &bytecode.ISeq{ Name: "",