ruby-changes:27917
From: akr <ko1@a...>
Date: Wed, 27 Mar 2013 22:12:37 +0900 (JST)
Subject: [ruby-changes:27917] akr:r39969 (trunk): * numeric.c (LONG_MIN_MINUS_ONE_IS_LESS_THAN): New macro.
akr 2013-03-27 22:12:27 +0900 (Wed, 27 Mar 2013) New Revision: 39969 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39969 Log: * numeric.c (LONG_MIN_MINUS_ONE_IS_LESS_THAN): New macro. (LLONG_MIN_MINUS_ONE_IS_LESS_THAN): Ditto. (rb_num2long): Use LONG_MIN_MINUS_ONE_IS_LESS_THAN. (rb_num2ulong): Ditto. (rb_num2ll): Use LLONG_MIN_MINUS_ONE_IS_LESS_THAN. (rb_num2ull): Ditto. * test/-ext-/num2int/test_num2int.rb (asert_num2i_success): Test the value converted into a Float if Float can represent the value exactly. (asert_num2i_error): Ditto. Modified files: trunk/ChangeLog trunk/numeric.c trunk/test/-ext-/num2int/test_num2int.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 39968) +++ ChangeLog (revision 39969) @@ -1,3 +1,17 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Mar 27 22:09:14 2013 Tanaka Akira <akr@f...> + + * numeric.c (LONG_MIN_MINUS_ONE_IS_LESS_THAN): New macro. + (LLONG_MIN_MINUS_ONE_IS_LESS_THAN): Ditto. + (rb_num2long): Use LONG_MIN_MINUS_ONE_IS_LESS_THAN. + (rb_num2ulong): Ditto. + (rb_num2ll): Use LLONG_MIN_MINUS_ONE_IS_LESS_THAN. + (rb_num2ull): Ditto. + + * test/-ext-/num2int/test_num2int.rb (asert_num2i_success): Test the + value converted into a Float if Float can represent the value + exactly. + (asert_num2i_error): Ditto. + Wed Mar 27 20:59:47 2013 Tanaka Akira <akr@f...> * test/-ext-/num2int/test_num2int.rb (asert_num2i_success): New Index: numeric.c =================================================================== --- numeric.c (revision 39968) +++ numeric.c (revision 39969) @@ -1940,6 +1940,10 @@ num_step(int argc, VALUE *argv, VALUE fr https://github.com/ruby/ruby/blob/trunk/numeric.c#L1940 #define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1) #define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1)) #define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1)) +#define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \ + (LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \ + LONG_MIN <= (n): \ + LONG_MIN_MINUS_ONE < (n)) SIGNED_VALUE rb_num2long(VALUE val) @@ -1954,7 +1958,7 @@ rb_num2long(VALUE val) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1958 switch (TYPE(val)) { case T_FLOAT: if (RFLOAT_VALUE(val) < LONG_MAX_PLUS_ONE - && RFLOAT_VALUE(val) > LONG_MIN_MINUS_ONE) { + && LONG_MIN_MINUS_ONE_IS_LESS_THAN(RFLOAT_VALUE(val))) { return (SIGNED_VALUE)(RFLOAT_VALUE(val)); } else { @@ -1988,7 +1992,7 @@ rb_num2ulong(VALUE val) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1992 switch (TYPE(val)) { case T_FLOAT: if (RFLOAT_VALUE(val) < ULONG_MAX_PLUS_ONE - && RFLOAT_VALUE(val) > LONG_MIN_MINUS_ONE) { + && LONG_MIN_MINUS_ONE_IS_LESS_THAN(RFLOAT_VALUE(val))) { return (VALUE)RFLOAT_VALUE(val); } else { @@ -2192,6 +2196,10 @@ rb_num2fix(VALUE val) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2196 #ifndef ULLONG_MAX #define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1) #endif +#define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \ + (LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \ + LLONG_MIN <= (n): \ + LLONG_MIN_MINUS_ONE < (n)) LONG_LONG rb_num2ll(VALUE val) @@ -2205,7 +2213,7 @@ rb_num2ll(VALUE val) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2213 switch (TYPE(val)) { case T_FLOAT: if (RFLOAT_VALUE(val) < LLONG_MAX_PLUS_ONE - && RFLOAT_VALUE(val) > LLONG_MIN_MINUS_ONE) { + && (LLONG_MIN_MINUS_ONE_IS_LESS_THAN(RFLOAT_VALUE(val)))) { return (LONG_LONG)(RFLOAT_VALUE(val)); } else { @@ -2249,7 +2257,7 @@ rb_num2ull(VALUE val) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2257 case T_FLOAT: if (RFLOAT_VALUE(val) < ULLONG_MAX_PLUS_ONE - && RFLOAT_VALUE(val) > 0) { + && LLONG_MIN_MINUS_ONE_IS_LESS_THAN(RFLOAT_VALUE(val))) { return (unsigned LONG_LONG)(RFLOAT_VALUE(val)); } else { Index: test/-ext-/num2int/test_num2int.rb =================================================================== --- test/-ext-/num2int/test_num2int.rb (revision 39968) +++ test/-ext-/num2int/test_num2int.rb (revision 39969) @@ -36,6 +36,11 @@ class TestNum2int < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/num2int/test_num2int.rb#L36 assert_output(result.to_s) do Num2int.send(method, num) end + if num.to_f.to_i == num + assert_output(result.to_s) do + Num2int.send(method, num.to_f) + end + end end def asert_num2i_error(type, num) @@ -43,6 +48,11 @@ class TestNum2int < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/num2int/test_num2int.rb#L48 assert_raise(RangeError) do Num2int.send(method, num) end + if num.to_f.to_i == num + assert_raise(RangeError) do + Num2int.send(method, num) + end + end end def test_num2short -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/