Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions internal/vm/numeric_numerator_internal_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
35 changes: 35 additions & 0 deletions internal/vm/prelude.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
108 changes: 103 additions & 5 deletions internal/vm/prelude_frozen_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.