diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-06-22 14:13:09 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-06-22 14:13:09 +0000 |
commit | bf8cec6db1cf9568a808449186f0bdb4e853ee36 (patch) | |
tree | 7b6e2a10085ec19b246aaeca2a826a8d0ce06279 /time.c | |
parent | 7d8d8c4f590982b47eaeb0ea0c447b29d6eeff82 (diff) | |
download | ruby-bf8cec6db1cf9568a808449186f0bdb4e853ee36.tar.gz |
* time.c (num_exact): fix for mathn. [ruby-dev:41599]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28389 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'time.c')
-rw-r--r-- | time.c | 49 |
1 files changed, 32 insertions, 17 deletions
@@ -656,35 +656,50 @@ wmod(wideval_t wx, wideval_t wy) static VALUE num_exact(VALUE v) { - switch (TYPE(v)) { + VALUE tmp; + int t; + + t = TYPE(v); + switch (t) { case T_FIXNUM: case T_BIGNUM: - case T_RATIONAL: - break; + return v; - case T_FLOAT: - v = rb_convert_type(v, T_RATIONAL, "Rational", "to_r"); + case T_RATIONAL: break; case T_STRING: case T_NIL: goto typeerror; - default: { - VALUE tmp; - if (!NIL_P(tmp = rb_check_convert_type(v, T_RATIONAL, "Rational", "to_r"))) { - if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror; + default: + if ((tmp = rb_check_funcall(v, rb_intern("to_r"), 0, NULL)) != Qundef) { + if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror; v = tmp; - } - else if (!NIL_P(tmp = rb_check_to_integer(v, "to_int"))) + break; + } + if (!NIL_P(tmp = rb_check_to_integer(v, "to_int"))) { v = tmp; - else { - typeerror: - rb_raise(rb_eTypeError, "can't convert %s into an exact number", - NIL_P(v) ? "nil" : rb_obj_classname(v)); + break; } + goto typeerror; + } + + t = TYPE(v); + switch (t) { + case T_FIXNUM: + case T_BIGNUM: + return v; + + case T_RATIONAL: + if (RRATIONAL(v)->den == INT2FIX(1)) + v = RRATIONAL(v)->num; break; - } + + default: + typeerror: + rb_raise(rb_eTypeError, "can't convert %s into an exact number", + NIL_P(v) ? "nil" : rb_obj_classname(v)); } return v; } @@ -3140,7 +3155,7 @@ time_to_r(VALUE time) GetTimeval(time, tobj); v = w2v(rb_time_unmagnify(tobj->timew)); if (TYPE(v) != T_RATIONAL) { - v = rb_convert_type(v, T_RATIONAL, "Rational", "to_r"); + v = rb_Rational1(v); } return v; } |