aboutsummaryrefslogtreecommitdiffstats
path: root/enum.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-23 12:41:00 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-23 12:41:00 +0000
commitca21c0a6ac6f49d0435d5817434d500ab422dbf7 (patch)
tree32cb85a9607db1a3c17d8b4da54e090ede05b867 /enum.c
parent8bc77e72eb806fca3be0f2293dc081e76803de4b (diff)
downloadruby-ca21c0a6ac6f49d0435d5817434d500ab422dbf7.tar.gz
* enum.c (ary_inject_op): Use Kahan's compensated summation algorithm
for summing up float values. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/enum.c b/enum.c
index 3cef60c7ce..e6ca6ab0fc 100644
--- a/enum.c
+++ b/enum.c
@@ -634,7 +634,7 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
ID id;
VALUE v, e;
long i, n;
- double f;
+ double f, c;
if (RARRAY_LEN(ary) == 0)
return init == Qundef ? Qnil : init;
@@ -686,16 +686,23 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
rb_method_basic_definition_p(rb_cFloat, idPLUS)) {
f = RFLOAT_VALUE(v);
sum_float:
+ c = 0.0;
while (1) {
+ double y, t;
e = RARRAY_AREF(ary, i);
if (RB_FLOAT_TYPE_P(e))
- f += RFLOAT_VALUE(e);
+ y = RFLOAT_VALUE(e) - c;
else if (FIXNUM_P(e))
- f += FIX2LONG(e);
+ y = FIX2LONG(e) - c;
else if (RB_TYPE_P(e, T_BIGNUM))
- f += rb_big2dbl(e);
+ y = rb_big2dbl(e) - c;
else
break;
+
+ t = f + y;
+ c = (t - f) - y;
+ f = t;
+
i++;
if (RARRAY_LEN(ary) <= i)
return DBL2NUM(f);