Skip to content

Commit

Permalink
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions core/src/main/java/org/jruby/RubyEnumerable.java
Original file line number Diff line number Diff line change
@@ -947,6 +947,9 @@ public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
});
}

if (result[0] instanceof RubyFloat) {
return ((RubyFloat) result[0]).op_plus(context, memo[0]);
}
return result[0];
}

@@ -955,7 +958,11 @@ public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
public static IRubyObject sumAdd(final ThreadContext ctx, IRubyObject lhs, IRubyObject rhs, final double c[]) {
boolean floats = false;
double f = 0.0;
double x = 0.0, y, t;
/*
* Kahan-Babuska balancing compensated summation algorithm
* See http://link.springer.com/article/10.1007/s00607-005-0139-x
*/
double x = 0.0, t;
if (lhs instanceof RubyFloat) {
if (rhs instanceof RubyFloat) {
f = ((RubyFloat) lhs).getValue();
@@ -998,9 +1005,12 @@ public static IRubyObject sumAdd(final ThreadContext ctx, IRubyObject lhs, IRuby
}

// Kahan's compensated summation algorithm
y = x - c[0];
t = f + y;
c[0] = (t - f) - y;
t = f + x;
if (Math.abs(f) >= Math.abs(x)) {
c[0] += ((f - t) + x);
} else {
c[0] += ((x - t) + f);
}
f = t;

return new RubyFloat(ctx.runtime, f);

0 comments on commit 5e8fc15

Please sign in to comment.