@Test public void testNumericallySafeSum() { final double[] numbers = { 10.3, 1e-2, -10.3, 140.5, -1e-2 }; assertEquals("numericallySafeSum() failed!", 140.5, FunctionUtil.numericallySafeSum(numbers), 0.00001); }
public static double numericallySafeSum(final double a[]) { int positiveCount = 0; for (double d : a) { if (d >= 0.0) ++positiveCount; } final double[] positiveValues = new double[positiveCount]; final double[] negativeValues = new double[a.length - positiveCount]; int positiveIndex = 0; int negativeIndex = 0; for (double d : a) { if (d >= 0.0) positiveValues[positiveIndex++] = d; else negativeValues[negativeIndex++] = d; } double positiveSum = 0.0; if (positiveValues.length > 0) { Arrays.sort(positiveValues); for (double d : positiveValues) positiveSum += d; } double negativeSum = 0.0; if (negativeValues.length > 0) { Arrays.sort(negativeValues); for (int i = negativeValues.length - 1; i >= 0; --i) negativeSum += negativeValues[i]; } return positiveSum + negativeSum; }
FunctionUtil { public static double numericallySafeSum(final double a[]) { int positiveCount = 0; for (double d : a) { if (d >= 0.0) ++positiveCount; } final double[] positiveValues = new double[positiveCount]; final double[] negativeValues = new double[a.length - positiveCount]; int positiveIndex = 0; int negativeIndex = 0; for (double d : a) { if (d >= 0.0) positiveValues[positiveIndex++] = d; else negativeValues[negativeIndex++] = d; } double positiveSum = 0.0; if (positiveValues.length > 0) { Arrays.sort(positiveValues); for (double d : positiveValues) positiveSum += d; } double negativeSum = 0.0; if (negativeValues.length > 0) { Arrays.sort(negativeValues); for (int i = negativeValues.length - 1; i >= 0; --i) negativeSum += negativeValues[i]; } return positiveSum + negativeSum; } }
FunctionUtil { public static double numericallySafeSum(final double a[]) { int positiveCount = 0; for (double d : a) { if (d >= 0.0) ++positiveCount; } final double[] positiveValues = new double[positiveCount]; final double[] negativeValues = new double[a.length - positiveCount]; int positiveIndex = 0; int negativeIndex = 0; for (double d : a) { if (d >= 0.0) positiveValues[positiveIndex++] = d; else negativeValues[negativeIndex++] = d; } double positiveSum = 0.0; if (positiveValues.length > 0) { Arrays.sort(positiveValues); for (double d : positiveValues) positiveSum += d; } double negativeSum = 0.0; if (negativeValues.length > 0) { Arrays.sort(negativeValues); for (int i = negativeValues.length - 1; i >= 0; --i) negativeSum += negativeValues[i]; } return positiveSum + negativeSum; } private  FunctionUtil(); }
FunctionUtil { public static double numericallySafeSum(final double a[]) { int positiveCount = 0; for (double d : a) { if (d >= 0.0) ++positiveCount; } final double[] positiveValues = new double[positiveCount]; final double[] negativeValues = new double[a.length - positiveCount]; int positiveIndex = 0; int negativeIndex = 0; for (double d : a) { if (d >= 0.0) positiveValues[positiveIndex++] = d; else negativeValues[negativeIndex++] = d; } double positiveSum = 0.0; if (positiveValues.length > 0) { Arrays.sort(positiveValues); for (double d : positiveValues) positiveSum += d; } double negativeSum = 0.0; if (negativeValues.length > 0) { Arrays.sort(negativeValues); for (int i = negativeValues.length - 1; i >= 0; --i) negativeSum += negativeValues[i]; } return positiveSum + negativeSum; } private  FunctionUtil(); static double getArgAsDouble(final Object arg); static String getArgAsString(final Object arg); static long getArgAsLong(final Object arg); static boolean getArgAsBoolean(final Object arg); static double numericallySafeSum(final double a[]); static String getOrdinal(final int i); static double calcSampleVariance(final double[] x); static double[] listToArray(final List<Double> a); static boolean isScalarArgType(final Class type); static boolean isTypeOfList(final Class listClassCandidate); static void addScalarArgumentTypes(final List<Class> argTypes); static double[] getDoubles(final Object[] args); static long[] getLongs(final Object[] args); static String[] getStrings(final Object[] args); static boolean[] getBooleans(final Object[] args); static Object translateObjectType(final Object input); }
FunctionUtil { public static double numericallySafeSum(final double a[]) { int positiveCount = 0; for (double d : a) { if (d >= 0.0) ++positiveCount; } final double[] positiveValues = new double[positiveCount]; final double[] negativeValues = new double[a.length - positiveCount]; int positiveIndex = 0; int negativeIndex = 0; for (double d : a) { if (d >= 0.0) positiveValues[positiveIndex++] = d; else negativeValues[negativeIndex++] = d; } double positiveSum = 0.0; if (positiveValues.length > 0) { Arrays.sort(positiveValues); for (double d : positiveValues) positiveSum += d; } double negativeSum = 0.0; if (negativeValues.length > 0) { Arrays.sort(negativeValues); for (int i = negativeValues.length - 1; i >= 0; --i) negativeSum += negativeValues[i]; } return positiveSum + negativeSum; } private  FunctionUtil(); static double getArgAsDouble(final Object arg); static String getArgAsString(final Object arg); static long getArgAsLong(final Object arg); static boolean getArgAsBoolean(final Object arg); static double numericallySafeSum(final double a[]); static String getOrdinal(final int i); static double calcSampleVariance(final double[] x); static double[] listToArray(final List<Double> a); static boolean isScalarArgType(final Class type); static boolean isTypeOfList(final Class listClassCandidate); static void addScalarArgumentTypes(final List<Class> argTypes); static double[] getDoubles(final Object[] args); static long[] getLongs(final Object[] args); static String[] getStrings(final Object[] args); static boolean[] getBooleans(final Object[] args); static Object translateObjectType(final Object input);  }