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 614c23d2b Keep magnitude of truncated integer digits in Dfp string 
constructor (#342)
614c23d2b is described below

commit 614c23d2b50fd3969174c7637bba540ba65f8d1a
Author: insaf021 <[email protected]>
AuthorDate: Wed Oct 28 23:33:55 2026 +0530

    Keep magnitude of truncated integer digits in Dfp string constructor (#342)
---
 .../org/apache/commons/math4/legacy/core/dfp/Dfp.java   | 12 ++++++++++++
 .../apache/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 @@ public class Dfp implements RealFieldElement<Dfp> {
 
             // 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 class DfpTest extends 
ExtendedFieldElementAbstractTest<Dfp> {
              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

Reply via email to