diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-27 03:13:39 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-27 03:13:39 +0000 |
commit | ee2f3c6def27ae7ea069ac8a2472ce2e80bcbb5b (patch) | |
tree | bdfae6f75c59424a0eceb49cb79c5e37e2e05413 /sprintf.c | |
parent | 3ce3e196ac281fb12e15819566c7bd0fa0fa32a0 (diff) | |
download | ruby-ee2f3c6def27ae7ea069ac8a2472ce2e80bcbb5b.tar.gz |
sprintf.c: Rational f by generic Integer functions
* sprintf.c (rb_str_format): convert Rational to floating point
format by using gereric Integer functions, not by methods which
can be overwritten.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54302 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sprintf.c')
-rw-r--r-- | sprintf.c | 17 |
1 files changed, 7 insertions, 10 deletions
@@ -1056,22 +1056,19 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) sign = -1; } } - else if (rb_num_negative_p(num)) { + else if (BIGNUM_NEGATIVE_P(num)) { sign = -1; - num = rb_funcallv(num, idUMinus, 0, 0); + num = rb_big_uminus(num); } - if (den != INT2FIX(1) || prec > 1) { - const ID idDiv = rb_intern("div"); - VALUE p10 = rb_int_positive_pow(10, prec); - VALUE den_2 = rb_funcall(den, idDiv, 1, INT2FIX(2)); - num = rb_funcallv(num, '*', 1, &p10); - num = rb_funcallv(num, '+', 1, &den_2); - num = rb_funcallv(num, idDiv, 1, &den); + if (den != INT2FIX(1)) { + num = rb_int_mul(num, rb_int_positive_pow(10, prec)); + num = rb_int_plus(num, rb_int_idiv(den, INT2FIX(2))); + num = rb_int_idiv(num, den); } else if (prec >= 0) { zero = prec; } - val = rb_obj_as_string(num); + val = rb_int2str(num, 10); len = RSTRING_LEN(val) + zero; if (prec >= len) ++len; /* integer part 0 */ if (sign || (flags&FSPACE)) ++len; |