From 278814ed6321f5d3b85a44439bca7fd5733a180f Mon Sep 17 00:00:00 2001 From: Mahammad Insaf Date: Wed, 28 Oct 2026 15:38:19 +0530 Subject: [PATCH] keep magnitude of truncated integer digits in Dfp string constructor --- .../commons/math4/legacy/core/dfp/Dfp.java | 12 ++++++++++++ .../commons/math4/legacy/core/dfp/DfpTest.java | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) 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 700aa399e..523cfd1c7 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 @@ -446,6 +446,18 @@ protected Dfp(final DfpField field, final String s) { // Don't want to run pass the end of the array if (q == mant.length * rsize + offset + 1) { + // the digits left before the decimal point are dropped, + // but they still count towards the magnitude + if (!decimalFound) { + decimalPos = significantDigits; + while (p < fpdecimal.length() && fpdecimal.charAt(p) != '.') { + if (fpdecimal.charAt(p) >= '0' && fpdecimal.charAt(p) <= '9') { + decimalPos++; + } + p++; + } + decimalFound = true; + } break; } 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 345d3efdc..defa68c14 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 @@ -161,6 +161,23 @@ public void testStringConstructorExponentRange() { DfpField.FLAG_UNDERFLOW, "String #1"); } + @Test + public void testStringConstructorDigitsBeyondPrecision() { + // integer digits that do not fit in the mantissa still set the magnitude + test(field.newDfp("123456789012345678901234567890"), + field.newDfp("1.2345678901234567890123e29"), + DfpField.FLAG_INEXACT, "String #2"); + test(field.newDfp("-100000000000000000000000000000"), + field.newDfp("-1e29"), + 0, "String #3"); + test(field.newDfp("1234567890123456789012345678.5"), + field.newDfp("1.2345678901234567890123e27"), + DfpField.FLAG_INEXACT, "String #4"); + test(field.newDfp("123456789012345678901234567890e-10"), + field.newDfp("1.2345678901234567890123e19"), + DfpField.FLAG_INEXACT, "String #5"); + } + @Test public void testAdd() { test(field.newDfp("1").add(field.newDfp("1")), // Basic tests 1+1 = 2