summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-15 14:46:35 (GMT)
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-15 14:46:35 (GMT)
commit0a267b79f62aac02628b02d27c716cfa8c2d67dc (patch)
tree0f12bf0c96edf7e7902fd5c8cf3b9dbbf1edcda6
parenta945eb9ddab99357a9f831c5dae99f33f223ac71 (diff)
array.c (rb_ary_sum): use rb_rational_add directly
* rational.c (rb_rational_add): rename from nurat_add. * array.c (rb_ary_sum): use rb_rational_add directly. * test/ruby/test_array.rb (test_sum): add assertions for an array of Rational values. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog9
-rw-r--r--array.c2
-rw-r--r--internal.h1
-rw-r--r--rational.c6
-rw-r--r--test/ruby/test_array.rb2
5 files changed, 16 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 6a0f305..727df82 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Fri Apr 15 23:42:00 2016 Kenta Murata <mrkn@mrkn.jp>
+
+ * rational.c (rb_rational_add): rename from nurat_add.
+
+ * array.c (rb_ary_sum): use rb_rational_add directly.
+
+ * test/ruby/test_array.rb (test_sum): add assertions for an array of
+ Rational values.
+
Fri Apr 15 22:31:00 2016 Kenta Murata <mrkn@mrkn.jp>
* array.c (rb_ary_sum): apply the precision compensated algorithm
diff --git a/array.c b/array.c
index 93d3d47..2bb31dd 100644
--- a/array.c
+++ b/array.c
@@ -5762,7 +5762,7 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary)
v = rb_to_float(v);
goto float_value;
}
- v = rb_funcall(v, idPLUS, 1, e);
+ v = rb_rational_add(e, v);
}
return v;
}
diff --git a/internal.h b/internal.h
index 15cad55..77fad73 100644
--- a/internal.h
+++ b/internal.h
@@ -1204,6 +1204,7 @@ rb_pid_t rb_fork_ruby(int *status);
void rb_last_status_clear(void);
/* rational.c */
+VALUE rb_rational_add(VALUE self, VALUE other);
VALUE rb_lcm(VALUE x, VALUE y);
VALUE rb_rational_reciprocal(VALUE x);
VALUE rb_cstr_to_rat(const char *, int);
diff --git a/rational.c b/rational.c
index 65902cd..8624ea1 100644
--- a/rational.c
+++ b/rational.c
@@ -741,8 +741,8 @@ f_addsub(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
* Rational(9, 8) + 4 #=> (41/8)
* Rational(20, 9) + 9.8 #=> 12.022222222222222
*/
-static VALUE
-nurat_add(VALUE self, VALUE other)
+VALUE
+rb_rational_add(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
{
@@ -2538,7 +2538,7 @@ Init_Rational(void)
rb_define_method(rb_cRational, "numerator", nurat_numerator, 0);
rb_define_method(rb_cRational, "denominator", nurat_denominator, 0);
- rb_define_method(rb_cRational, "+", nurat_add, 1);
+ rb_define_method(rb_cRational, "+", rb_rational_add, 1);
rb_define_method(rb_cRational, "-", nurat_sub, 1);
rb_define_method(rb_cRational, "*", nurat_mul, 1);
rb_define_method(rb_cRational, "/", nurat_div, 1);
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 929cd15..8293a78 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -2761,6 +2761,8 @@ class TestArray < Test::Unit::TestCase
assert_float_equal((FIXNUM_MAX+1).to_f, [FIXNUM_MAX, 1, 0.0].sum)
assert_float_equal((FIXNUM_MAX+1).to_f, [0.0, FIXNUM_MAX+1].sum)
+ assert_rational_equal(5/6r, [1/2r, 1/3r].sum)
+
assert_equal(2.0+3.0i, [2.0, 3.0i].sum)
assert_int_equal(13, [1, 2].sum(10))