Last active
May 1, 2026 07:43
-
-
Save coderodde/e30edba46c4ae195222ddff7259334c5 to your computer and use it in GitHub Desktop.
CR.ComputeMean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.math.BigDecimal; | |
| import java.util.*; | |
| public class Test1 { | |
| private static double computeMean1(double... values) { | |
| double sum = 0.0; | |
| for (double x : values) { | |
| sum += x; | |
| } | |
| return sum / values.length; | |
| } | |
| private static double computeMean2(double... values) { | |
| double sum = 0.0; | |
| for (double x : values) { | |
| sum += x / values.length; | |
| } | |
| return sum; | |
| } | |
| private static double computeMean3(double... values) { | |
| double sum = 0.0; | |
| double f = 1.0 / values.length; | |
| for (double x : values) { | |
| sum += x * f; | |
| } | |
| return sum; | |
| } | |
| private static int dcmp(double a, double b) { | |
| a = Math.abs(a); | |
| b = Math.abs(b); | |
| return Double.compare(a, b); | |
| } | |
| private static double computeMean4(Double... values) { | |
| Arrays.sort(values, (a, b) -> dcmp(a, b)); | |
| double sum = 0.0; | |
| for (double d : values) { | |
| sum += d; | |
| } | |
| return sum / values.length; | |
| } | |
| private static BigDecimal computeMeanBD(BigDecimal... values) { | |
| BigDecimal sum = BigDecimal.ZERO; | |
| for (BigDecimal bd : values) { | |
| sum = sum.add(bd); | |
| } | |
| return sum.divide(BigDecimal.valueOf(values.length)); | |
| } | |
| public static void main(String[] args) { | |
| double[] values1 = { 10E9, 0.001, 0.001, 0.001 }; | |
| System.out.println(computeMean1(values1)); | |
| System.out.println(computeMean2(values1)); | |
| System.out.println(computeMean3(values1)); | |
| Double[] values2 = { 10E9, 0.001, 0.001, 0.001 }; | |
| System.out.println(computeMean4(values2)); | |
| BigDecimal[] values3 = { BigDecimal.valueOf(10E9), | |
| BigDecimal.valueOf(0.001), | |
| BigDecimal.valueOf(0.001), | |
| BigDecimal.valueOf(0.001) }; | |
| System.out.println(computeMeanBD(values3)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Test2 { | |
| public static void main(String[] args) { | |
| double[] values = new double[1_000_000]; | |
| values[0] = 1e9; | |
| for (int i = 1; i < values.length; ++i) { | |
| values[i] = 1e-6; | |
| } | |
| printSum(values); | |
| reverse(values); | |
| printSum(values); | |
| } | |
| private static void printSum(double[] values) { | |
| double sum = 0.0; | |
| for (double d : values) { | |
| sum += d; | |
| } | |
| System.out.println(sum); | |
| } | |
| private static void reverse(double[] values) { | |
| for (int lo = 0, hi = values.length - 1; lo < hi; ++lo, --hi) { | |
| double tmp = values[lo]; | |
| values[lo] = values[hi]; | |
| values[hi] = tmp; | |
| } | |
| } | |
| } |
And here's a reference type typo: computeMean4(Double... values).
Sorry, no, that's correct.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, you can use
Arrays.sort(values, Test1::dcmp);andreturn sum.divide(BigDecimal.valueOf(values.length), MathContext.DECIMAL128);.