You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2018/02/02 13:29:01 UTC
[06/50] commons-numbers git commit: NUMBERS-43: Remove "round(float)".
NUMBERS-43: Remove "round(float)".
See discussion on the JIRA page:
https://issues.apache.org/jira/browse/NUMBERS-43
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/bf05e066
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/bf05e066
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/bf05e066
Branch: refs/heads/feature__NUMBERS-51__field
Commit: bf05e066c816245609e0315b27103192906b2d17
Parents: 4b955ca
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Sat Jan 27 17:31:11 2018 +0100
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Sat Jan 27 17:31:11 2018 +0100
----------------------------------------------------------------------
.../apache/commons/numbers/core/Precision.java | 117 -------------------
.../commons/numbers/core/PrecisionTest.java | 107 -----------------
2 files changed, 224 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/bf05e066/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
----------------------------------------------------------------------
diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
index e71e1e4..11fe704 100644
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
+++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
@@ -461,123 +461,6 @@ public class Precision {
}
/**
- * Rounds the given value to the specified number of decimal places.
- * The value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method.
- *
- * @param x Value to round.
- * @param scale Number of digits to the right of the decimal point.
- * @return the rounded value.
- */
- public static float round(float x, int scale) {
- return round(x, scale, BigDecimal.ROUND_HALF_UP);
- }
-
- /**
- * Rounds the given value to the specified number of decimal places.
- * The value is rounded using the given method which is any method defined
- * in {@link BigDecimal}.
- *
- * @param x Value to round.
- * @param scale Number of digits to the right of the decimal point.
- * @param roundingMethod Rounding method as defined in {@link BigDecimal}.
- * @return the rounded value.
- * @throws ArithmeticException if an exact operation is required but result is not exact
- * @throws IllegalArgumentException if {@code roundingMethod} is not a valid rounding method.
- */
- public static float round(float x, int scale, int roundingMethod) {
- final float sign = Math.copySign(1f, x);
- final float factor = (float) Math.pow(10.0f, scale) * sign;
- return (float) roundUnscaled(x * factor, sign, roundingMethod) / factor;
- }
-
- /**
- * Rounds the given non-negative value to the "nearest" integer. Nearest is
- * determined by the rounding method specified. Rounding methods are defined
- * in {@link BigDecimal}.
- *
- * @param unscaled Value to round.
- * @param sign Sign of the original, scaled value.
- * @param roundingMethod Rounding method, as defined in {@link BigDecimal}.
- * @return the rounded value.
- * @throws ArithmeticException if an exact operation is required but result is not exact
- * @throws IllegalArgumentException if {@code roundingMethod} is not a valid rounding method.
- */
- private static double roundUnscaled(double unscaled,
- double sign,
- int roundingMethod) {
- switch (roundingMethod) {
- case BigDecimal.ROUND_CEILING :
- if (sign == -1) {
- unscaled = Math.floor(Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
- } else {
- unscaled = Math.ceil(Math.nextAfter(unscaled, Double.POSITIVE_INFINITY));
- }
- break;
- case BigDecimal.ROUND_DOWN :
- unscaled = Math.floor(Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
- break;
- case BigDecimal.ROUND_FLOOR :
- if (sign == -1) {
- unscaled = Math.ceil(Math.nextAfter(unscaled, Double.POSITIVE_INFINITY));
- } else {
- unscaled = Math.floor(Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
- }
- break;
- case BigDecimal.ROUND_HALF_DOWN : {
- unscaled = Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY);
- double fraction = unscaled - Math.floor(unscaled);
- if (fraction > 0.5) {
- unscaled = Math.ceil(unscaled);
- } else {
- unscaled = Math.floor(unscaled);
- }
- break;
- }
- case BigDecimal.ROUND_HALF_EVEN : {
- double fraction = unscaled - Math.floor(unscaled);
- if (fraction > 0.5) {
- unscaled = Math.ceil(unscaled);
- } else if (fraction < 0.5) {
- unscaled = Math.floor(unscaled);
- } else {
- // The following equality test is intentional and needed for rounding purposes
- if (Math.floor(unscaled) / 2.0 == Math.floor(Math.floor(unscaled) / 2.0)) { // even
- unscaled = Math.floor(unscaled);
- } else { // odd
- unscaled = Math.ceil(unscaled);
- }
- }
- break;
- }
- case BigDecimal.ROUND_HALF_UP : {
- unscaled = Math.nextAfter(unscaled, Double.POSITIVE_INFINITY);
- double fraction = unscaled - Math.floor(unscaled);
- if (fraction >= 0.5) {
- unscaled = Math.ceil(unscaled);
- } else {
- unscaled = Math.floor(unscaled);
- }
- break;
- }
- case BigDecimal.ROUND_UNNECESSARY :
- if (unscaled != Math.floor(unscaled)) {
- throw new ArithmeticException();
- }
- break;
- case BigDecimal.ROUND_UP :
- // do not round if the discarded fraction is equal to zero
- if (unscaled != Math.floor(unscaled)) {
- unscaled = Math.ceil(Math.nextAfter(unscaled, Double.POSITIVE_INFINITY));
- }
- break;
- default :
- throw new IllegalArgumentException("Unhandled rounding method: " + roundingMethod);
- }
- return unscaled;
- }
-
-
- /**
* Computes a number {@code delta} close to {@code originalDelta} with
* the property that <pre><code>
* x + delta - x
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/bf05e066/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java
index 188c949..8d63c43 100644
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java
+++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java
@@ -394,113 +394,6 @@ public class PrecisionTest {
Assert.assertEquals("-0.0", Double.toString(Precision.round(-1e-10, 0)));
}
- @Test
- public void testRoundFloat() {
- float x = 1.234567890f;
- Assert.assertEquals(1.23f, Precision.round(x, 2), 0.0);
- Assert.assertEquals(1.235f, Precision.round(x, 3), 0.0);
- Assert.assertEquals(1.2346f, Precision.round(x, 4), 0.0);
-
- // BZ 35904
- Assert.assertEquals(30.1f, Precision.round(30.095f, 2), 0.0f);
- Assert.assertEquals(30.1f, Precision.round(30.095f, 1), 0.0f);
- Assert.assertEquals(50.09f, Precision.round(50.085f, 2), 0.0f);
- Assert.assertEquals(50.19f, Precision.round(50.185f, 2), 0.0f);
- Assert.assertEquals(50.01f, Precision.round(50.005f, 2), 0.0f);
- Assert.assertEquals(30.01f, Precision.round(30.005f, 2), 0.0f);
- Assert.assertEquals(30.65f, Precision.round(30.645f, 2), 0.0f);
-
- Assert.assertEquals(1.24f, Precision.round(x, 2, BigDecimal.ROUND_CEILING), 0.0);
- Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_CEILING), 0.0);
- Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_CEILING), 0.0);
- Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_CEILING), 0.0);
- Assert.assertEquals(-1.234f, Precision.round(-x, 3, BigDecimal.ROUND_CEILING), 0.0);
- Assert.assertEquals(-1.2345f, Precision.round(-x, 4, BigDecimal.ROUND_CEILING), 0.0);
-
- Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_DOWN), 0.0);
- Assert.assertEquals(1.234f, Precision.round(x, 3, BigDecimal.ROUND_DOWN), 0.0);
- Assert.assertEquals(1.2345f, Precision.round(x, 4, BigDecimal.ROUND_DOWN), 0.0);
- Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_DOWN), 0.0);
- Assert.assertEquals(-1.234f, Precision.round(-x, 3, BigDecimal.ROUND_DOWN), 0.0);
- Assert.assertEquals(-1.2345f, Precision.round(-x, 4, BigDecimal.ROUND_DOWN), 0.0);
-
- Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_FLOOR), 0.0);
- Assert.assertEquals(1.234f, Precision.round(x, 3, BigDecimal.ROUND_FLOOR), 0.0);
- Assert.assertEquals(1.2345f, Precision.round(x, 4, BigDecimal.ROUND_FLOOR), 0.0);
- Assert.assertEquals(-1.24f, Precision.round(-x, 2, BigDecimal.ROUND_FLOOR), 0.0);
- Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_FLOOR), 0.0);
- Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_FLOOR), 0.0);
-
- Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_HALF_DOWN), 0.0);
- Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_HALF_DOWN), 0.0);
- Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_HALF_DOWN), 0.0);
- Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_HALF_DOWN), 0.0);
- Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_HALF_DOWN), 0.0);
- Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_HALF_DOWN), 0.0);
- Assert.assertEquals(1.234f, Precision.round(1.2345f, 3, BigDecimal.ROUND_HALF_DOWN), 0.0);
- Assert.assertEquals(-1.234f, Precision.round(-1.2345f, 3, BigDecimal.ROUND_HALF_DOWN), 0.0);
-
- Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_HALF_EVEN), 0.0);
- Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_HALF_EVEN), 0.0);
- Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_HALF_EVEN), 0.0);
- Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_HALF_EVEN), 0.0);
- Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_HALF_EVEN), 0.0);
- Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_HALF_EVEN), 0.0);
- Assert.assertEquals(1.234f, Precision.round(1.2345f, 3, BigDecimal.ROUND_HALF_EVEN), 0.0);
- Assert.assertEquals(-1.234f, Precision.round(-1.2345f, 3, BigDecimal.ROUND_HALF_EVEN), 0.0);
- Assert.assertEquals(1.236f, Precision.round(1.2355f, 3, BigDecimal.ROUND_HALF_EVEN), 0.0);
- Assert.assertEquals(-1.236f, Precision.round(-1.2355f, 3, BigDecimal.ROUND_HALF_EVEN), 0.0);
-
- Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_HALF_UP), 0.0);
- Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_HALF_UP), 0.0);
- Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_HALF_UP), 0.0);
- Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_HALF_UP), 0.0);
- Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_HALF_UP), 0.0);
- Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_HALF_UP), 0.0);
- Assert.assertEquals(1.235f, Precision.round(1.2345f, 3, BigDecimal.ROUND_HALF_UP), 0.0);
- Assert.assertEquals(-1.235f, Precision.round(-1.2345f, 3, BigDecimal.ROUND_HALF_UP), 0.0);
-
- Assert.assertEquals(-1.23f, Precision.round(-1.23f, 2, BigDecimal.ROUND_UNNECESSARY), 0.0);
- Assert.assertEquals(1.23f, Precision.round(1.23f, 2, BigDecimal.ROUND_UNNECESSARY), 0.0);
-
- try {
- Precision.round(1.234f, 2, BigDecimal.ROUND_UNNECESSARY);
- Assert.fail();
- } catch (ArithmeticException ex) {
- // success
- }
-
- Assert.assertEquals(1.24f, Precision.round(x, 2, BigDecimal.ROUND_UP), 0.0);
- Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_UP), 0.0);
- Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_UP), 0.0);
- Assert.assertEquals(-1.24f, Precision.round(-x, 2, BigDecimal.ROUND_UP), 0.0);
- Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_UP), 0.0);
- Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_UP), 0.0);
-
- try {
- Precision.round(1.234f, 2, 1923);
- Assert.fail();
- } catch (IllegalArgumentException ex) {
- // success
- }
-
- // special values
- TestUtils.assertEquals(Float.NaN, Precision.round(Float.NaN, 2), 0.0f);
- Assert.assertEquals(0.0f, Precision.round(0.0f, 2), 0.0f);
- Assert.assertEquals(Float.POSITIVE_INFINITY, Precision.round(Float.POSITIVE_INFINITY, 2), 0.0f);
- Assert.assertEquals(Float.NEGATIVE_INFINITY, Precision.round(Float.NEGATIVE_INFINITY, 2), 0.0f);
- // comparison of positive and negative zero is not possible -> always equal thus do string comparison
- Assert.assertEquals("-0.0", Float.toString(Precision.round(-0.0f, 0)));
- Assert.assertEquals("-0.0", Float.toString(Precision.round(-1e-10f, 0)));
-
- // MATH-1070
- Assert.assertEquals(0.0f, Precision.round(0f, 2, BigDecimal.ROUND_UP), 0.0f);
- Assert.assertEquals(0.05f, Precision.round(0.05f, 2, BigDecimal.ROUND_UP), 0.0f);
- Assert.assertEquals(0.06f, Precision.round(0.051f, 2, BigDecimal.ROUND_UP), 0.0f);
- Assert.assertEquals(0.06f, Precision.round(0.0505f, 2, BigDecimal.ROUND_UP), 0.0f);
- Assert.assertEquals(0.06f, Precision.round(0.059f, 2, BigDecimal.ROUND_UP), 0.0f);
- }
-
@Test
public void testIssue721() {