/* * Copyright (c) 2013, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @bug 8006572 8030212 * @summary Test for use of non-naive summation in stream-related sum and average operations.
*/ publicclass TestDoubleSumAverage { publicstaticvoid main(String... args) { int failures = 0;
/** * Test to verify that a non-empty stream with a zero average is non-empty.
*/ privatestaticint testZeroAverageOfNonEmptyStream() {
Supplier<DoubleStream> ds = () -> DoubleStream.iterate(0.0, e -> 0.0).limit(10);
/** * Compute the sum and average of a sequence of double values in * various ways and report an error if naive summation is used.
*/ privatestaticint testForCompenstation() { int failures = 0;
/* * The exact sum of the test stream is 1 + 1e6*ulp(1.0) but a * naive summation algorithm will return 1.0 since (1.0 + * ulp(1.0)/2) will round to 1.0 again.
*/ double base = 1.0; double increment = Math.ulp(base)/2.0; int count = 1_000_001;
// Factory for double a stream of [base, increment, ..., increment] limited to a size of count
Supplier<DoubleStream> ds = () -> DoubleStream.iterate(base, e -> increment).limit(count);
/** * Compute the ulp difference of two double values and compare against an error threshold.
*/ privatestaticint compareUlpDifference(double expected, double computed, double threshold) { if (!Double.isFinite(expected)) { // Handle NaN and infinity cases if (Double.compare(expected, computed) == 0) return 0; else {
System.err.printf("Unexpected sum, %g rather than %g.%n",
computed, expected); return 1;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.