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-math.git


The following commit(s) were added to refs/heads/master by this push:
     new 550307133 Round on the whole remainder in the Dfp string constructor 
(#362)
550307133 is described below

commit 55030713342afec25f296fa4a4e5129247ae4b4c
Author: insaf021 <[email protected]>
AuthorDate: Sun Nov 29 22:25:49 2026 +0530

    Round on the whole remainder in the Dfp string constructor (#362)
    
    The parser kept a single digit past the mantissa and rounded with
    round(digit * 1000). A dropped "5678" therefore looked like an exact
    half and ROUND_HALF_EVEN took it down, and a non-zero digit further
    down was lost entirely, so a tie was never broken and FLAG_INEXACT
    was not raised. Keep a full radix digit of remainder, track whether
    anything non-zero was dropped below it, and pass that to round().
---
 .../apache/commons/math4/legacy/core/dfp/Dfp.java  | 44 +++++++++++---
 .../commons/math4/legacy/core/dfp/DfpTest.java     | 68 ++++++++++++++++++++++
 2 files changed, 105 insertions(+), 7 deletions(-)

diff --git 
a/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/dfp/Dfp.java
 
b/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/dfp/Dfp.java
index 7bd171d1a..6c1b86591 100644
--- 
a/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/dfp/Dfp.java
+++ 
b/commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/dfp/Dfp.java
@@ -366,6 +366,8 @@ public class Dfp implements RealFieldElement<Dfp> {
         final int rsize = 4;   // size of radix in decimal digits
         final int offset = 4;  // Starting offset into Striped
         final char[] striped = new char[getRadixDigits() * rsize + offset * 2];
+        // positions outside the mantissa are read when rounding, so they must 
be digits
+        Arrays.fill(striped, '0');
 
         // Check some special cases
         if (s.equals(POS_INFINITY_STRING)) {
@@ -451,18 +453,26 @@ public class Dfp implements RealFieldElement<Dfp> {
 
         // Copy the string onto Stripped
         int q = offset;
-        striped[0] = '0';
-        striped[1] = '0';
-        striped[2] = '0';
-        striped[3] = '0';
         int significantDigits = 0;
+        // set when a non-zero digit is dropped from below the rounding digits
+        boolean sticky = false;
         for (;;) {
             if (p == (fpdecimal.length())) {
                 break;
             }
 
             // Don't want to run pass the end of the array
-            if (q == mant.length * rsize + offset + 1) {
+            if (q == mant.length * rsize + offset + rsize) {
+                // nothing past this point can be kept, but a non-zero digit 
there
+                // still decides a rounding tie
+                for (int j = p; j < fpdecimal.length(); j++) {
+                    final char c = fpdecimal.charAt(j);
+                    if (c >= '1' && c <= '9') {
+                        sticky = true;
+                        break;
+                    }
+                }
+
                 // the digits left before the decimal point are dropped,
                 // but they still count towards the magnitude
                 if (!decimalFound) {
@@ -496,6 +506,8 @@ public class Dfp implements RealFieldElement<Dfp> {
             significantDigits++;
         }
 
+        // one past the last digit that was copied
+        final int stripedEnd = q;
 
         // If the decimal point has been found then get rid of trailing zeros.
         if (decimalFound && q != offset) {
@@ -564,8 +576,26 @@ public class Dfp implements RealFieldElement<Dfp> {
         }
 
         if (q < striped.length) {
-            // Is there possible another digit?
-            final int excp = round((striped[q] - '0') * 1000);
+            // the digits that did not fit, as a fraction of the last mantissa 
digit
+            int n = 0;
+            for (i = 0; i < rsize; i++) {
+                n = n * 10 + (striped[q + i] - '0');
+            }
+
+            for (i = q + rsize; i < stripedEnd; i++) {
+                if (striped[i] != '0') {
+                    sticky = true;
+                    break;
+                }
+            }
+
+            if (sticky && (n & 1) == 0) {
+                // the remainder is strictly greater than n, so it is neither 
zero
+                // nor an exact half; nudging it keeps those two tests in 
round() honest
+                n++;
+            }
+
+            final int excp = round(n);
             if (excp != 0) {
                 // apply the overflow or underflow handling that the
                 // arithmetic operations get from dotrap
diff --git 
a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/dfp/DfpTest.java
 
b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/dfp/DfpTest.java
index 94da23185..0089e113a 100644
--- 
a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/dfp/DfpTest.java
+++ 
b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/dfp/DfpTest.java
@@ -232,6 +232,74 @@ public class DfpTest extends 
ExtendedFieldElementAbstractTest<Dfp> {
              DfpField.FLAG_INEXACT, "String #5");
     }
 
+    @Test
+    public void testStringConstructorRounding() {
+        // Fewer than 13 decimal digits still gives 4 radix digits, i.e. 16 
decimal
+        // digits of mantissa. The decimal point sits on a radix boundary, so 
a value
+        // of 17 to 19 digits spends its leading radix digit on 1, 2 or 3 
decimal
+        // digits and keeps 13, 14 or 15 of them.
+        final DfpField small = new DfpField(10);
+        Assert.assertEquals(4, small.getRadixDigits());
+        Assert.assertEquals(DfpField.RoundingMode.ROUND_HALF_EVEN, 
small.getRoundingMode());
+
+        // 16 digits fill the mantissa exactly, nothing to round
+        assertDfp("1000000000000000.", "1000000000000000", small, 0);
+        assertDfp("9999999999999999.", "9999999999999999", small, 0);
+        assertDfp("-9999999999999999.", "-9999999999999999", small, 0);
+
+        // 17 digits, 13 kept: 4567 rounds down, 5567 rounds up
+        assertDfp("1.234567890123e16", "12345678901234567", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("-1.234567890123e16", "-12345678901234567", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("1.234567890124e16", "12345678901235567", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("-1.234567890124e16", "-12345678901235567", small, 
DfpField.FLAG_INEXACT);
+
+        // 18 digits, 14 kept: 4678 rounds down, 5678 rounds up
+        assertDfp("1.2345678901234e17", "123456789012344678", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("1.2345678901235e17", "123456789012345678", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("-1.2345678901235e17", "-123456789012345678", small, 
DfpField.FLAG_INEXACT);
+
+        // 19 digits, 15 kept: 1789 rounds down, 6789 rounds up
+        assertDfp("1.23456789012345e18", "1234567890123451789", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("1.23456789012346e18", "1234567890123456789", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("-1.23456789012346e18", "-1234567890123456789", small, 
DfpField.FLAG_INEXACT);
+
+        // an exact half goes to even
+        assertDfp("1.234567890122e16", "12345678901215000", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("1.234567890122e16", "12345678901225000", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("1.234567890124e16", "12345678901235000", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("1.234567890124e16", "12345678901245000", small, 
DfpField.FLAG_INEXACT);
+
+        // a non-zero digit anywhere below the half means it is not a half any 
more,
+        // whether it is still inside the parsed digits or far past them
+        assertDfp("1.234567890123e16", "12345678901225000.01", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("1.234567890123e16", "12345678901225000.0000000001", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("1.234567890124e16", "1.2345678901235000e16", small, 
DfpField.FLAG_INEXACT);
+
+        // rounding up carries through the whole mantissa
+        assertDfp("1.0000000000000e17", "99999999999999995", small, 
DfpField.FLAG_INEXACT);
+        assertDfp("-1.0000000000000e17", "-99999999999999995", small, 
DfpField.FLAG_INEXACT);
+
+        // fractions round the same way
+        assertDfp("0.1234567890123457", "0.12345678901234567890123", small, 
DfpField.FLAG_INEXACT);
+
+        // a dropped digit beyond the mantissa is inexact even when it changes 
nothing
+        assertDfp("1.", "1.0000000000000000000000001", field, 
DfpField.FLAG_INEXACT);
+        assertDfp("1.", "1.0000000000000000000000000", field, 0);
+    }
+
+    /** Parse {@code s} and check the value and the flags it raised.
+     * @param expected expected {@link Dfp#toString()} of the parsed value
+     * @param s string to parse
+     * @param f field to parse within
+     * @param flags expected IEEE flags
+     */
+    private void assertDfp(String expected, String s, DfpField f, int flags) {
+        f.clearIEEEFlags();
+        Assert.assertEquals(expected, f.newDfp(s).toString());
+        Assert.assertEquals("flags for " + s, flags, f.getIEEEFlags());
+        f.clearIEEEFlags();
+    }
+
     @Test
     public void testAdd() {
         test(field.newDfp("1").add(field.newDfp("1")),      // Basic tests   
1+1 = 2

Reply via email to