* array.c (rb_ary_sum): Array#sum is implemented.
Kahan's compensated summation algorithm for precise sum of float numbers is moved from ary_inject_op in enum.c. * enum.c (ary_inject_op): Don't specialize for float numbers. [ruby-core:74569] [Feature#12217] proposed by mrkn. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54565 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c258535186
commit
b359d20352
10
ChangeLog
10
ChangeLog
@ -1,3 +1,13 @@
|
|||||||
|
Wed Apr 13 22:51:38 2016 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* array.c (rb_ary_sum): Array#sum is implemented.
|
||||||
|
Kahan's compensated summation algorithm for precise sum of float
|
||||||
|
numbers is moved from ary_inject_op in enum.c.
|
||||||
|
|
||||||
|
* enum.c (ary_inject_op): Don't specialize for float numbers.
|
||||||
|
|
||||||
|
[ruby-core:74569] [Feature#12217] proposed by mrkn.
|
||||||
|
|
||||||
Wed Apr 13 15:56:35 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Wed Apr 13 15:56:35 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* numeric.c (flo_ceil): add an optional parameter, digits, as
|
* numeric.c (flo_ceil): add an optional parameter, digits, as
|
||||||
|
87
array.c
87
array.c
@ -5650,6 +5650,92 @@ rb_ary_dig(int argc, VALUE *argv, VALUE self)
|
|||||||
return rb_obj_dig(argc, argv, self, Qnil);
|
return rb_obj_dig(argc, argv, self, Qnil);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* ary.sum -> number
|
||||||
|
*
|
||||||
|
* Returns the sum of elements.
|
||||||
|
* For example, [e1, e2, e3].sum returns 0 + e1 + e2 + e3.
|
||||||
|
*
|
||||||
|
* If <i>ary</i> is empty, it returns 0.
|
||||||
|
*
|
||||||
|
* [].sum #=> 0
|
||||||
|
* [1, 2, 3].sum #=> 6
|
||||||
|
* [3, 5.5].sum #=> 8.5
|
||||||
|
*
|
||||||
|
* This method may not respect method redefinition of "+" methods
|
||||||
|
* such as Fixnum#+.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_ary_sum(VALUE ary)
|
||||||
|
{
|
||||||
|
VALUE v, e;
|
||||||
|
long i, n;
|
||||||
|
|
||||||
|
if (RARRAY_LEN(ary) == 0)
|
||||||
|
return LONG2FIX(0);
|
||||||
|
|
||||||
|
v = LONG2FIX(0);
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
for (i = 0; i < RARRAY_LEN(ary); i++) {
|
||||||
|
e = RARRAY_AREF(ary, i);
|
||||||
|
if (FIXNUM_P(e)) {
|
||||||
|
n += FIX2LONG(e); /* should not overflow long type */
|
||||||
|
if (!FIXABLE(n)) {
|
||||||
|
v = rb_big_plus(LONG2NUM(n), v);
|
||||||
|
n = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (RB_TYPE_P(e, T_BIGNUM))
|
||||||
|
v = rb_big_plus(e, v);
|
||||||
|
else
|
||||||
|
goto not_integer;
|
||||||
|
}
|
||||||
|
if (n != 0)
|
||||||
|
v = rb_fix_plus(LONG2FIX(n), v);
|
||||||
|
return v;
|
||||||
|
|
||||||
|
not_integer:
|
||||||
|
if (n != 0)
|
||||||
|
v = rb_fix_plus(LONG2FIX(n), v);
|
||||||
|
|
||||||
|
if (RB_FLOAT_TYPE_P(e)) {
|
||||||
|
/* Kahan's compensated summation algorithm */
|
||||||
|
double f, c;
|
||||||
|
f = NUM2DBL(v);
|
||||||
|
c = 0.0;
|
||||||
|
for (; i < RARRAY_LEN(ary); i++) {
|
||||||
|
double x, y, t;
|
||||||
|
e = RARRAY_AREF(ary, i);
|
||||||
|
if (RB_FLOAT_TYPE_P(e))
|
||||||
|
x = RFLOAT_VALUE(e);
|
||||||
|
else if (FIXNUM_P(e))
|
||||||
|
x = FIX2LONG(e);
|
||||||
|
else if (RB_TYPE_P(e, T_BIGNUM))
|
||||||
|
x = rb_big2dbl(e);
|
||||||
|
else
|
||||||
|
goto not_float;
|
||||||
|
|
||||||
|
y = x - c;
|
||||||
|
t = f + y;
|
||||||
|
c = (t - f) - y;
|
||||||
|
f = t;
|
||||||
|
}
|
||||||
|
return DBL2NUM(f);
|
||||||
|
|
||||||
|
not_float:
|
||||||
|
v = DBL2NUM(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; i < RARRAY_LEN(ary); i++) {
|
||||||
|
v = rb_funcall(v, idPLUS, 1, RARRAY_AREF(ary, i));
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Arrays are ordered, integer-indexed collections of any object.
|
* Arrays are ordered, integer-indexed collections of any object.
|
||||||
*
|
*
|
||||||
@ -6005,6 +6091,7 @@ Init_Array(void)
|
|||||||
rb_define_method(rb_cArray, "bsearch_index", rb_ary_bsearch_index, 0);
|
rb_define_method(rb_cArray, "bsearch_index", rb_ary_bsearch_index, 0);
|
||||||
rb_define_method(rb_cArray, "any?", rb_ary_any_p, 0);
|
rb_define_method(rb_cArray, "any?", rb_ary_any_p, 0);
|
||||||
rb_define_method(rb_cArray, "dig", rb_ary_dig, -1);
|
rb_define_method(rb_cArray, "dig", rb_ary_dig, -1);
|
||||||
|
rb_define_method(rb_cArray, "sum", rb_ary_sum, 0);
|
||||||
|
|
||||||
id_cmp = rb_intern("<=>");
|
id_cmp = rb_intern("<=>");
|
||||||
id_random = rb_intern("random");
|
id_random = rb_intern("random");
|
||||||
|
48
enum.c
48
enum.c
@ -634,7 +634,6 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
|
|||||||
ID id;
|
ID id;
|
||||||
VALUE v, e;
|
VALUE v, e;
|
||||||
long i, n;
|
long i, n;
|
||||||
double f, c;
|
|
||||||
|
|
||||||
if (RARRAY_LEN(ary) == 0)
|
if (RARRAY_LEN(ary) == 0)
|
||||||
return init == Qundef ? Qnil : init;
|
return init == Qundef ? Qnil : init;
|
||||||
@ -656,7 +655,7 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
|
|||||||
rb_method_basic_definition_p(rb_cFixnum, idPLUS) &&
|
rb_method_basic_definition_p(rb_cFixnum, idPLUS) &&
|
||||||
rb_method_basic_definition_p(rb_cBignum, idPLUS)) {
|
rb_method_basic_definition_p(rb_cBignum, idPLUS)) {
|
||||||
n = 0;
|
n = 0;
|
||||||
while (1) {
|
for (; i < RARRAY_LEN(ary); i++) {
|
||||||
e = RARRAY_AREF(ary, i);
|
e = RARRAY_AREF(ary, i);
|
||||||
if (FIXNUM_P(e)) {
|
if (FIXNUM_P(e)) {
|
||||||
n += FIX2LONG(e); /* should not overflow long type */
|
n += FIX2LONG(e); /* should not overflow long type */
|
||||||
@ -668,49 +667,18 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
|
|||||||
else if (RB_TYPE_P(e, T_BIGNUM))
|
else if (RB_TYPE_P(e, T_BIGNUM))
|
||||||
v = rb_big_plus(e, v);
|
v = rb_big_plus(e, v);
|
||||||
else
|
else
|
||||||
break;
|
goto not_integer;
|
||||||
i++;
|
|
||||||
if (RARRAY_LEN(ary) <= i)
|
|
||||||
return n == 0 ? v : rb_fix_plus(LONG2FIX(n), v);
|
|
||||||
}
|
}
|
||||||
if (n != 0) {
|
if (n != 0)
|
||||||
v = rb_fix_plus(LONG2FIX(n), v);
|
v = rb_fix_plus(LONG2FIX(n), v);
|
||||||
}
|
return v;
|
||||||
if (RB_FLOAT_TYPE_P(e) &&
|
|
||||||
rb_method_basic_definition_p(rb_cFloat, idPLUS)) {
|
|
||||||
f = NUM2DBL(v);
|
|
||||||
goto sum_float;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (RB_FLOAT_TYPE_P(v) &&
|
|
||||||
rb_method_basic_definition_p(rb_cFloat, idPLUS)) {
|
|
||||||
f = RFLOAT_VALUE(v);
|
|
||||||
sum_float:
|
|
||||||
c = 0.0;
|
|
||||||
while (1) {
|
|
||||||
double x, y, t;
|
|
||||||
e = RARRAY_AREF(ary, i);
|
|
||||||
if (RB_FLOAT_TYPE_P(e))
|
|
||||||
x = RFLOAT_VALUE(e);
|
|
||||||
else if (FIXNUM_P(e))
|
|
||||||
x = FIX2LONG(e);
|
|
||||||
else if (RB_TYPE_P(e, T_BIGNUM))
|
|
||||||
x = rb_big2dbl(e);
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
|
|
||||||
y = x - c;
|
not_integer:
|
||||||
t = f + y;
|
if (n != 0)
|
||||||
c = (t - f) - y;
|
v = rb_fix_plus(LONG2FIX(n), v);
|
||||||
f = t;
|
|
||||||
|
|
||||||
i++;
|
|
||||||
if (RARRAY_LEN(ary) <= i)
|
|
||||||
return DBL2NUM(f);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (; i<RARRAY_LEN(ary); i++) {
|
for (; i < RARRAY_LEN(ary); i++) {
|
||||||
v = rb_funcall(v, id, 1, RARRAY_AREF(ary, i));
|
v = rb_funcall(v, id, 1, RARRAY_AREF(ary, i));
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# coding: US-ASCII
|
# coding: US-ASCII
|
||||||
# frozen_string_literal: false
|
# frozen_string_literal: false
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
|
require "rbconfig/sizeof"
|
||||||
|
|
||||||
class TestArray < Test::Unit::TestCase
|
class TestArray < Test::Unit::TestCase
|
||||||
def setup
|
def setup
|
||||||
@ -2710,6 +2711,39 @@ class TestArray < Test::Unit::TestCase
|
|||||||
assert_raise(TypeError) {h.dig(1, 0)}
|
assert_raise(TypeError) {h.dig(1, 0)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
FIXNUM_MIN = -(1 << (8 * RbConfig::SIZEOF['long'] - 2))
|
||||||
|
FIXNUM_MAX = (1 << (8 * RbConfig::SIZEOF['long'] - 2)) - 1
|
||||||
|
|
||||||
|
def assert_float_equal(e, v, msg=nil)
|
||||||
|
assert_equal(Float, v.class, msg)
|
||||||
|
assert_equal(e, v, msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_sum
|
||||||
|
assert_equal(0, [].sum)
|
||||||
|
assert_equal(3, [3].sum)
|
||||||
|
assert_equal(8, [3, 5].sum)
|
||||||
|
assert_equal(15, [3, 5, 7].sum)
|
||||||
|
assert_float_equal(15.0, [3, 5, 7.0].sum)
|
||||||
|
assert_equal(2*FIXNUM_MAX, Array.new(2, FIXNUM_MAX).sum)
|
||||||
|
assert_equal(2*(FIXNUM_MAX+1), Array.new(2, FIXNUM_MAX+1).sum)
|
||||||
|
assert_equal(10*FIXNUM_MAX, Array.new(10, FIXNUM_MAX).sum)
|
||||||
|
assert_equal(0, ([FIXNUM_MAX, 1, -FIXNUM_MAX, -1]*10).sum)
|
||||||
|
assert_equal(FIXNUM_MAX*10, ([FIXNUM_MAX+1, -1]*10).sum)
|
||||||
|
assert_equal(2*FIXNUM_MIN, Array.new(2, FIXNUM_MIN).sum)
|
||||||
|
assert_equal((FIXNUM_MAX+1).to_f, [FIXNUM_MAX, 1, 0.0].sum)
|
||||||
|
assert_float_equal(8.0, [3.0, 5].sum)
|
||||||
|
assert_float_equal((FIXNUM_MAX+1).to_f, [0.0, FIXNUM_MAX+1].sum)
|
||||||
|
assert_equal(2.0+3.0i, [2.0, 3.0i].sum)
|
||||||
|
|
||||||
|
large_number = 100000000
|
||||||
|
small_number = 1e-9
|
||||||
|
until (large_number + small_number) == large_number
|
||||||
|
small_number /= 10
|
||||||
|
end
|
||||||
|
assert_equal(large_number+(small_number*10), [large_number, *[small_number]*10].sum)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def need_continuation
|
def need_continuation
|
||||||
unless respond_to?(:callcc, true)
|
unless respond_to?(:callcc, true)
|
||||||
|
@ -217,13 +217,6 @@ class TestEnumerable < Test::Unit::TestCase
|
|||||||
assert_float_equal(10.0, [3.0, 5].inject(2.0, :+))
|
assert_float_equal(10.0, [3.0, 5].inject(2.0, :+))
|
||||||
assert_float_equal((FIXNUM_MAX+1).to_f, [0.0, FIXNUM_MAX+1].inject(:+))
|
assert_float_equal((FIXNUM_MAX+1).to_f, [0.0, FIXNUM_MAX+1].inject(:+))
|
||||||
assert_equal(2.0+3.0i, [2.0, 3.0i].inject(:+))
|
assert_equal(2.0+3.0i, [2.0, 3.0i].inject(:+))
|
||||||
|
|
||||||
large_number = 100000000
|
|
||||||
small_number = 1e-9
|
|
||||||
until (large_number + small_number) == large_number
|
|
||||||
small_number /= 10
|
|
||||||
end
|
|
||||||
assert_equal(large_number+(small_number*10), [large_number, *[small_number]*10].inject(:+))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_inject_array_plus_redefined
|
def test_inject_array_plus_redefined
|
||||||
|
Loading…
x
Reference in New Issue
Block a user