diff options
-rw-r--r-- | rational.c | 22 | ||||
-rw-r--r-- | test/ruby/test_rational.rb | 4 |
2 files changed, 18 insertions, 8 deletions
diff --git a/rational.c b/rational.c index 3fbfc7f1b1..b57677031d 100644 --- a/rational.c +++ b/rational.c @@ -2368,6 +2368,18 @@ islettere(int c) return (c == 'e' || c == 'E'); } +static VALUE +negate_num(VALUE num) +{ + if (FIXNUM_P(num)) { + return rb_int_uminus(num); + } + else { + BIGNUM_NEGATE(num); + return rb_big_norm(num); + } +} + static int read_num(const char **s, const char *const end, VALUE *num, VALUE *div) { @@ -2422,7 +2434,7 @@ read_num(const char **s, const char *const end, VALUE *num, VALUE *div) else { if (fn != ZERO) exp = rb_int_minus(exp, fn); if (INT_NEGATIVE_P(exp)) { - *div = f_expt10(exp); + *div = f_expt10(negate_num(exp)); } else { *num = rb_int_mul(n, f_expt10(exp)); @@ -2483,13 +2495,7 @@ parse_rat(const char *s, const char *const e, int strict) } if (sign == '-') { - if (FIXNUM_P(num)) { - num = rb_int_uminus(num); - } - else { - BIGNUM_NEGATE(num); - num = rb_big_norm(num); - } + num = negate_num(num); } if (!canonicalization || den != ONE) diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 74fbbb49ec..fe267d5a1f 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -110,6 +110,10 @@ class Rational_Test < Test::Unit::TestCase assert_equal(Rational(3),Rational('3')) assert_equal(Rational(1),Rational('3.0','3.0')) assert_equal(Rational(1),Rational('3/3','3/3')) + assert_equal(Rational(111, 10), Rational('1.11e+1')) + assert_equal(Rational(111, 10), Rational('1.11e1')) + assert_equal(Rational(111, 100), Rational('1.11e0')) + assert_equal(Rational(111, 1000), Rational('1.11e-1')) assert_raise(TypeError){Rational(nil)} assert_raise(ArgumentError){Rational('')} assert_raise_with_message(ArgumentError, /\u{221a 2668}/) { |