aboutsummaryrefslogtreecommitdiffstats
path: root/array.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-02-18 03:42:35 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-02-18 03:42:35 +0000
commitc6d70b130cb859d604bb0c986b73dda586abe692 (patch)
tree10ed7458b2e65ca689c47f4e78df52b08ca2e283 /array.c
parent8abdd524abec4d3046af7b154b7b75c684dd5eea (diff)
downloadruby-c6d70b130cb859d604bb0c986b73dda586abe692.tar.gz
array.c: finish_exact_sum
* array.c (finish_exact_sum): extract duplicate code from rb_ary_sum. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57649 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c41
1 files changed, 19 insertions, 22 deletions
diff --git a/array.c b/array.c
index e970ca3787..d9d6bdbf87 100644
--- a/array.c
+++ b/array.c
@@ -5733,6 +5733,23 @@ rb_ary_dig(int argc, VALUE *argv, VALUE self)
return rb_obj_dig(argc, argv, self, Qnil);
}
+static inline VALUE
+finish_exact_sum(long n, VALUE r, VALUE v)
+{
+ if (n != 0)
+ v = rb_fix_plus(LONG2FIX(n), v);
+ if (r != Qundef) {
+ /* r can be an Integer when mathn is loaded */
+ if (FIXNUM_P(r))
+ v = rb_fix_plus(r, v);
+ else if (RB_TYPE_P(r, T_BIGNUM))
+ v = rb_big_plus(r, v);
+ else
+ v = rb_rational_plus(r, v);
+ }
+ return v;
+}
+
/*
* call-seq:
* ary.sum(init=0) -> number
@@ -5814,31 +5831,11 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary)
else
goto not_exact;
}
- if (n != 0)
- v = rb_fix_plus(LONG2FIX(n), v);
- if (r != Qundef) {
- /* r can be an Integer when mathn is loaded */
- if (FIXNUM_P(r))
- v = rb_fix_plus(r, v);
- else if (RB_TYPE_P(r, T_BIGNUM))
- v = rb_big_plus(r, v);
- else
- v = rb_rational_plus(r, v);
- }
+ v = finish_exact_sum(n, r, v);
return v;
not_exact:
- if (n != 0)
- v = rb_fix_plus(LONG2FIX(n), v);
- if (r != Qundef) {
- /* r can be an Integer when mathn is loaded */
- if (FIXNUM_P(r))
- v = rb_fix_plus(r, v);
- else if (RB_TYPE_P(r, T_BIGNUM))
- v = rb_big_plus(r, v);
- else
- v = rb_rational_plus(r, v);
- }
+ v = finish_exact_sum(n, r, v);
if (RB_FLOAT_TYPE_P(e)) {
/*