Skip to content

Instantly share code, notes, and snippets.

@LosD
Created September 11, 2016 09:42
Show Gist options
  • Save LosD/d6d3bd6b2615375d840dcded45e9b898 to your computer and use it in GitHub Desktop.
Save LosD/d6d3bd6b2615375d840dcded45e9b898 to your computer and use it in GitHub Desktop.
import java.util.Random;
class TestTypeAverages {
static final long TEST_COUNT = 1000000000L;
public static void main(String[] args) {
int errorCount = 0;
Random generator = new Random();
for (long i = 0; i < TEST_COUNT; i++) {
long sum = generator.nextLong();
long count = generator.nextInt(Integer.MAX_VALUE - 1) + 1; // Only positive values, keep realistic sizes
long average = sum / count;
double doubleAverage = (double) sum / (double) count;
long convertedAverage = (long) doubleAverage;
if(average != convertedAverage) {
// System.out.format("sum: %d, count: %d, long average: %d != converted average %d (double value = %f)\n", sum, count, average, convertedAverage, doubleAverage);
errorCount++;
}
}
double percentage = ((double) errorCount * 100) / (double) TEST_COUNT;
System.out.format("errors: %d, total tests = %d, percentage: %f%%\n", errorCount, TEST_COUNT, percentage);
}
}
@kaspersorensen
Copy link

Try this in contrast to see how it compares to a more precise avg value by using a BigDecimal then:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

class TestTypeAverages {
    static final long TEST_COUNT = 1000000000L;

    public static void main(String[] args) {
        int errorCount = 0;
        int falseAlarms = 0;
        Random generator = new Random();
        for (long i = 0; i < TEST_COUNT; i++) {
            long sum = generator.nextLong();
            long count = generator.nextInt(Integer.MAX_VALUE - 1) + 1; // Only positive values, keep realistic sizes
            long average = sum / count;
            double doubleAverage = (sum * 1.0) / count;
            long convertedAverage = (long) doubleAverage;
            if (average != convertedAverage) {
                final long scientificValue = BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(count), RoundingMode.HALF_UP).longValue();
//                final long scientificValue =
//                        bd.round(new MathContext(bd.precision() - bd.scale(), RoundingMode.CEILING)).longValue();
                if (average == scientificValue) {
//                    System.out.format("sum: %d, count: %d, long average: %d != converted average %d (double value = %f)\n",
//                            sum, count, average, convertedAverage, doubleAverage);
                    errorCount++;
                } else {
                    falseAlarms++;
                }
            }
        }

        double percentage = ((double) errorCount * 100) / (double) TEST_COUNT;
        System.out.println("false alarms: " + falseAlarms);
        System.out.format("errors: %d, total tests = %d, percentage: %f%%\n", errorCount, TEST_COUNT, percentage);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment