insaf021 commented on code in PR #359:
URL: https://github.com/apache/commons-math/pull/359#discussion_r4698235111


##########
commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/dfp/DfpTest.java:
##########
@@ -116,6 +116,24 @@ public void testLongConstructor() {
         Assert.assertEquals("9223372036854775807.", new Dfp(field, 
Long.MAX_VALUE).toString());
     }
 
+    @Test
+    public void testLongConstructorBeyondPrecision() {
+        // 4 radix digits (16 decimal digits) cannot hold every long
+        final DfpField small = new DfpField(10);

Review Comment:
   No digit missed. The `12345678901234567L` line was not asserting the string 
form, that was the `assertEquals` message argument, which reads like an 
expectation. My fault for writing it that way. It actually gives 
`1.234567890123e16`.
   
   The precision is quantised rather than a flat 16 decimal digits. The decimal 
point has to land on a radix boundary, so at 19 digits the leading radix digit 
carries only 3 decimal digits (`123`) and one decimal position inside it is a 
leading zero. That leaves 15 significant digits, so `1.23456789012346e18` is 
correct and `1.234567890123457e18` is not representable on this field. 17 
digits keeps 13, 18 keeps 14, 19 keeps 15. `newDfp("1234567890123456789")` 
returns the same value.
   
   I checked each case against `BigDecimal.round(new MathContext(keep, 
HALF_EVEN))` and the long constructor lands on the nearest representable value 
every time, including the carry out of the mantissa and the halfway ties.
   
   Test rewritten with explicit expected strings for 16, 17, 18 and 19 digits, 
rounding down and up in each band, both signs, the two halfway cases that show 
the tie going to even, and the `99999999999999999L` carry.
   
   One thing that fell out of this: at 18 digits the two constructors disagree. 
`123456789012345678L` gives `1.2345678901235e17` from the long path, which is 
nearest, but `1.2345678901234e17` from the string path. The string constructor 
keeps only the first dropped decimal digit and passes `digit * 1000` to 
`round()`, so `...5678` looks like an exact tie and half-even takes it down. 
That is pre-existing and I have left it alone here. Happy to fix it separately 
if you want it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to