diff options
-rw-r--r-- | rational.c | 27 | ||||
-rw-r--r-- | test/test_mathn.rb | 9 | ||||
-rw-r--r-- | version.h | 2 |
3 files changed, 32 insertions, 6 deletions
diff --git a/rational.c b/rational.c index a78b347c08..2fd020429d 100644 --- a/rational.c +++ b/rational.c @@ -2365,11 +2365,22 @@ read_num(const char **s, int numsign, int strict, exp = rb_int_uminus(exp); } - if (numsign == '-') - *num = rb_rational_uminus(*num); + if (numsign == '-') { + if (RB_TYPE_P(*num, T_RATIONAL)) { + *num = rb_rational_uminus(*num); + } + else { + *num = rb_int_uminus(*num); + } + } if (!NIL_P(exp)) { VALUE l = f_expt10(exp); - *num = nurat_mul(*num, l); + if (RB_TYPE_P(*num, T_RATIONAL)) { + *num = nurat_mul(*num, l); + } + else { + *num = rb_int_mul(*num, l); + } } return 1; } @@ -2395,8 +2406,14 @@ read_rat_nos(const char **s, int sign, int strict, (*s)++; if (!read_den(s, strict, &den)) return 0; - if (!(FIXNUM_P(den) && FIX2LONG(den) == 1)) - *num = nurat_div(*num, den); + if (!(FIXNUM_P(den) && FIX2LONG(den) == 1)) { + if (RB_TYPE_P(*num, T_RATIONAL)) { + *num = nurat_div(*num, den); + } + else { + *num = rb_int_div(*num, den); + } + } } return 1; } diff --git a/test/test_mathn.rb b/test/test_mathn.rb index 00130eadf1..b50a09e4df 100644 --- a/test/test_mathn.rb +++ b/test/test_mathn.rb @@ -180,4 +180,13 @@ class TestMathn < Test::Unit::TestCase assert_equal((-13/5), (-13/5).round(2, half: :down)) EOS end + + def test_rational + assert_separately(%w[-rmathn], "#{<<-"begin;"}\n#{<<-"end;"}", ignore_stderr: true) + begin; + assert_equal(-5, "-5".to_r) + assert_equal(1, "5/5".to_r) + assert_equal(5, "5e0".to_r) + end; + end end @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.4.0" #define RUBY_RELEASE_DATE "2017-03-11" -#define RUBY_PATCHLEVEL 10 +#define RUBY_PATCHLEVEL 11 #define RUBY_RELEASE_YEAR 2017 #define RUBY_RELEASE_MONTH 3 |