Created
September 11, 2016 09:42
-
-
Save LosD/d6d3bd6b2615375d840dcded45e9b898 to your computer and use it in GitHub Desktop.
This file contains 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.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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this in contrast to see how it compares to a more precise avg value by using a BigDecimal then: