You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2021/07/30 16:06:25 UTC
[commons-statistics] 07/22: Cache upper - lower + 1 for computations
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
commit 0c58ac0c23726fee6d795214cbb252a52881c37d
Author: aherbert <ah...@apache.org>
AuthorDate: Fri Jul 30 12:16:28 2021 +0100
Cache upper - lower + 1 for computations
---
.../distribution/UniformDiscreteDistribution.java | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/UniformDiscreteDistribution.java b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/UniformDiscreteDistribution.java
index 959ee2f..9fce06e 100644
--- a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/UniformDiscreteDistribution.java
+++ b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/UniformDiscreteDistribution.java
@@ -31,10 +31,10 @@ public class UniformDiscreteDistribution extends AbstractDiscreteDistribution {
private final int lower;
/** Upper bound (inclusive) of this distribution. */
private final int upper;
- /** "upper" + "lower" (to avoid overflow). */
+ /** "upper" + "lower" (as a double to avoid overflow). */
private final double upperPlusLower;
- /** "upper" - "lower" (to avoid overflow). */
- private final double upperMinusLower;
+ /** "upper" - "lower" + 1 (as a double to avoid overflow). */
+ private final double upperMinusLowerPlus1;
/**
* Creates a new uniform integer distribution using the given lower and
@@ -53,7 +53,7 @@ public class UniformDiscreteDistribution extends AbstractDiscreteDistribution {
this.lower = lower;
this.upper = upper;
upperPlusLower = (double) upper + (double) lower;
- upperMinusLower = (double) upper - (double) lower;
+ upperMinusLowerPlus1 = (double) upper - (double) lower + 1;
}
/** {@inheritDoc} */
@@ -62,7 +62,7 @@ public class UniformDiscreteDistribution extends AbstractDiscreteDistribution {
if (x < lower || x > upper) {
return 0;
}
- return 1 / (upperMinusLower + 1);
+ return 1.0 / upperMinusLowerPlus1;
}
/** {@inheritDoc} */
@@ -74,7 +74,7 @@ public class UniformDiscreteDistribution extends AbstractDiscreteDistribution {
if (x > upper) {
return 1;
}
- return (x - lower + 1) / (upperMinusLower + 1);
+ return (x - lower + 1) / upperMinusLowerPlus1;
}
/**
@@ -96,8 +96,7 @@ public class UniformDiscreteDistribution extends AbstractDiscreteDistribution {
*/
@Override
public double getVariance() {
- final double n = upperMinusLower + 1;
- return ONE_TWELFTH * (n * n - 1);
+ return ONE_TWELFTH * (upperMinusLowerPlus1 * upperMinusLowerPlus1 - 1);
}
/**