/** * Increments the Kahan sum by adding two sums, and updating the correction term for reducing numeric errors. */ public CompensatedSum add(CompensatedSum other) { double correctedSum = other.value() + (delta + other.delta()); double updatedValue = value + correctedSum; double updatedDelta = correctedSum - (updatedValue - value); return new CompensatedSum(updatedValue, updatedDelta); }
@Test public void testDelta() throws Exception { CompensatedSum compensatedResult1 = new CompensatedSum(0.001, 0.0); for (int i = 0; i < 10; i++) { compensatedResult1 = compensatedResult1.add(0.001); } Assert.assertEquals(0.011, compensatedResult1.value(), 0.0); Assert.assertEquals(new Double("8.673617379884035E-19"), compensatedResult1.delta(), 0.0); }
/** * Increments the Kahan sum by adding two sums, and updating the correction term for reducing numeric errors. */ public CompensatedSum add(CompensatedSum other) { double correctedSum = other.value() + (delta + other.delta()); double updatedValue = value + correctedSum; double updatedDelta = correctedSum - (updatedValue - value); return new CompensatedSum(updatedValue, updatedDelta); }
/** * Increments the Kahan sum by adding two sums, and updating the correction term for reducing numeric errors. */ public CompensatedSum add(CompensatedSum other) { double correctedSum = other.value() + (delta + other.delta()); double updatedValue = value + correctedSum; double updatedDelta = correctedSum - (updatedValue - value); return new CompensatedSum(updatedValue, updatedDelta); }