summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-27 03:13:39 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-27 03:13:39 (GMT)
commitee2f3c6def27ae7ea069ac8a2472ce2e80bcbb5b (patch)
treebdfae6f75c59424a0eceb49cb79c5e37e2e05413 /sprintf.c
parent3ce3e196ac281fb12e15819566c7bd0fa0fa32a0 (diff)
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.c17
1 files changed, 7 insertions, 10 deletions
diff --git a/sprintf.c b/sprintf.c
index 1984cec..f0c2f8d 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -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;