Skip to content

Commit

Permalink
8274517: java/util/DoubleStreamSums/CompensatedSums.java fails with e…
Browse files Browse the repository at this point in the history
…xpected [true] but found [false]

Backport-of: 97a983526b41d26fcd1caa162a089690119874b0
  • Loading branch information
GoeLin committed Jun 14, 2022
1 parent 1963985 commit dd7280f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions test/jdk/java/util/DoubleStreamSums/CompensatedSums.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,6 +24,9 @@
/*
* @test
* @bug 8214761
* @key randomness
* @library /test/lib
* @build jdk.test.lib.RandomFactory
* @run testng CompensatedSums
* @summary
*/
Expand All @@ -34,13 +37,16 @@
import java.util.function.Supplier;
import java.util.stream.DoubleStream;

import jdk.test.lib.RandomFactory;
import org.testng.Assert;
import org.testng.annotations.Test;

public class CompensatedSums {

@Test
public void testCompensatedSums() {
Random r = RandomFactory.getRandom();

double naive = 0;
double jdkSequentialStreamError = 0;
double goodSequentialStreamError = 0;
Expand All @@ -50,7 +56,7 @@ public void testCompensatedSums() {

for (int loop = 0; loop < 100; loop++) {
// sequence of random numbers of varying magnitudes, both positive and negative
double[] rand = new Random().doubles(1_000_000)
double[] rand = r.doubles(1_000_000)
.map(Math::log)
.map(x -> (Double.doubleToLongBits(x) % 2 == 0) ? x : -x)
.toArray();
Expand Down Expand Up @@ -85,7 +91,7 @@ public void testCompensatedSums() {
}

Assert.assertTrue(jdkParallelStreamError <= goodParallelStreamError);
Assert.assertTrue(badParallelStreamError > jdkParallelStreamError);
Assert.assertTrue(badParallelStreamError >= jdkParallelStreamError);

Assert.assertTrue(goodSequentialStreamError >= jdkSequentialStreamError);
Assert.assertTrue(naive > jdkSequentialStreamError);
Expand All @@ -97,7 +103,7 @@ private static double square(double arg) {
return arg * arg;
}

// from OpenJDK8 Collectors, unmodified
// from OpenJDK 18 Collectors, unmodified
static double[] sumWithCompensation(double[] intermediateSum, double value) {
double tmp = value - intermediateSum[1];
double sum = intermediateSum[0];
Expand All @@ -107,9 +113,10 @@ static double[] sumWithCompensation(double[] intermediateSum, double value) {
return intermediateSum;
}

// from OpenJDK8 Collectors, unmodified
// from OpenJDK 18 Collectors, unmodified
static double computeFinalSum(double[] summands) {
double tmp = summands[0] + summands[1];
// Final sum with better error bounds subtract second summand as it is negated
double tmp = summands[0] - summands[1];
double simpleSum = summands[summands.length - 1];
if (Double.isNaN(tmp) && Double.isInfinite(simpleSum))
return simpleSum;
Expand Down

1 comment on commit dd7280f

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.