From: cremno@... Date: 2014-08-30T22:30:11+00:00 Subject: [ruby-core:64678] [ruby-trunk - Feature #7517] Fixnum::MIN,MAX Issue #7517 has been updated by cremno phobia. Even if the use-cases aren't really convincing, I think the informational purpose alone is a good enough argument to add it. If you search for ���ruby fixnum max��� or something similar, you'll find the interest is there, but the solution(s) usually only work in CRuby and are not as easy to understand as the proposed constants. ~~~ diff diff --git a/numeric.c b/numeric.c index 1e971f4..b57c686 100644 --- a/numeric.c +++ b/numeric.c @@ -4062,6 +4062,15 @@ Init_Numeric(void) rb_cFixnum = rb_define_class("Fixnum", rb_cInteger); + /* + * The minimum value of a Fixnum. + */ + rb_define_const(rb_cFixnum, "MIN", LONG2FIX(FIXNUM_MIN)); + /* + * The maximum value of a Fixnum. + */ + rb_define_const(rb_cFixnum, "MAX", LONG2FIX(FIXNUM_MAX)); + rb_define_method(rb_cFixnum, "to_s", fix_to_s, -1); rb_define_alias(rb_cFixnum, "inspect", "to_s"); diff --git a/test/ruby/test_fixnum.rb b/test/ruby/test_fixnum.rb index 8b2cf2e..8c896ec 100644 --- a/test/ruby/test_fixnum.rb +++ b/test/ruby/test_fixnum.rb @@ -312,4 +312,20 @@ class TestFixnum < Test::Unit::TestCase assert_equal(1, 5.remainder(4)) assert_predicate(4.remainder(Float::NAN), :nan?) end + + def test_min + assert_kind_of(Fixnum, Fixnum::MIN) + assert_kind_of(Bignum, Fixnum::MIN - 1) + if RUBY_ENGINE == 'ruby' + assert_equal(-2 ** (1.size * 8 - 2), Fixnum::MIN) + end + end + + def test_max + assert_kind_of(Fixnum, Fixnum::MAX) + assert_kind_of(Bignum, Fixnum::MAX + 1) + if RUBY_ENGINE == 'ruby' + assert_equal(2 ** (1.size * 8 - 2) - 1, Fixnum::MAX) + end + end end ~~~ ---------------------------------------- Feature #7517: Fixnum::MIN,MAX https://bugs.ruby-lang.org/issues/7517#change-48573 * Author: Yukihiro Matsumoto * Status: Assigned * Priority: Normal * Assignee: Yukihiro Matsumoto * Category: core * Target version: next minor ---------------------------------------- Maximum (or minimum) number of fixnum can not be get in portable fashion, so I propose Fixnum::MAX and Fixnum::MIN just like Float::MAX and Float::MIN. Matz -- https://bugs.ruby-lang.org/