Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,27 @@ protected Dfp(final DfpField field, long x) {
}

exp = 0;
int lostdigit = 0;
while (x != 0) {
System.arraycopy(mant, mant.length - exp, mant, mant.length - 1 - exp, exp);
if (exp < mant.length) {
System.arraycopy(mant, mant.length - exp, mant, mant.length - 1 - exp, exp);
} else {
// the mantissa is full, the least significant digit is shifted out
// (a long has at most 5 radix digits and a field at least 4,
// so at most one digit is lost)
lostdigit = mant[0];
System.arraycopy(mant, 1, mant, 0, mant.length - 1);
}
mant[mant.length - 1] = (int) (x % RADIX);
x /= RADIX;
exp++;
}

if (isLongMin) {
if (isLongMin && lostdigit != 0) {
// remove the shift added for Long.MIN_VALUE
// the last digit is the one that was shifted out
lostdigit++;
} else if (isLongMin) {
// remove the shift added for Long.MIN_VALUE
// we know in this case that fixing the last digit is sufficient
for (int i = 0; i < mant.length - 1; i++) {
Expand All @@ -253,6 +266,10 @@ protected Dfp(final DfpField field, long x) {
}
}
}

if (lostdigit != 0) {
round(lostdigit);
}
}

/** Create an instance from a double value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,60 @@ public void testLongConstructor() {
Assert.assertEquals("9223372036854775807.", new Dfp(field, Long.MAX_VALUE).toString());
}

@Test
public void testLongConstructorBeyondPrecision() {
// 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; the fifth radix digit is rounded off.
final DfpField small = new DfpField(10);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The source code shows decimal digits < 13 using 4 radix digits (radix=10000). So we have configured for 16 decimal digits. But the long value 12345678901234567L has 17 digits and is reproduced as exactly the same String, yet 1234567890123456789L has 19 digits and is reproduced as a 15 digit String plus exponent. So has rounding missed a digit? Should the result be "1.234567890123457e18"?

This should show behaviour with 16, 17, 18 and 19 digit strings that expect rounding up or down when the Dfp has to round.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the more explicit tests. Please raise a PR with a fix to the String parsing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raised #362 for it. The parser keeps a single decimal digit past the mantissa and hands round() digit * 1000, so a remainder of 5678 arrives as an exact 5000 and everything below that first digit never reaches the tie test at all; that also means FLAG_INEXACT was being missed. It now keeps a full radix digit of remainder plus a sticky bit for whatever is dropped below it. Cross-checked against BigDecimal half-even over 80k generated values across 7 field sizes: 7665 disagreed before, none after.

Assert.assertEquals(4, small.getRadixDigits());
Assert.assertEquals(DfpField.RoundingMode.ROUND_HALF_EVEN, small.getRoundingMode());

// 16 digits fill the mantissa exactly, nothing to round
Assert.assertEquals("1000000000000000.", small.newDfp(1000000000000000L).toString());
Assert.assertEquals("9999999999999999.", small.newDfp(9999999999999999L).toString());
Assert.assertEquals("-9999999999999999.", small.newDfp(-9999999999999999L).toString());

// 17 digits, 13 kept
Assert.assertEquals("1.000000000000e16", small.newDfp(10000000000000000L).toString());
// 4567 rounds down, 5567 rounds up
Assert.assertEquals("1.234567890123e16", small.newDfp(12345678901234567L).toString());
Assert.assertEquals("-1.234567890123e16", small.newDfp(-12345678901234567L).toString());
Assert.assertEquals("1.234567890124e16", small.newDfp(12345678901235567L).toString());
Assert.assertEquals("-1.234567890124e16", small.newDfp(-12345678901235567L).toString());
// halfway cases go to even: ...123 is odd so it rounds up, ...124 stays
Assert.assertEquals("1.234567890124e16", small.newDfp(12345678901235000L).toString());
Assert.assertEquals("1.234567890124e16", small.newDfp(12345678901245000L).toString());
// rounding up carries through the whole mantissa
Assert.assertEquals("1.0000000000000e17", small.newDfp(99999999999999999L).toString());
Assert.assertEquals("-1.0000000000000e17", small.newDfp(-99999999999999999L).toString());

// 18 digits, 14 kept: 4678 rounds down, 5678 rounds up
Assert.assertEquals("1.2345678901234e17", small.newDfp(123456789012344678L).toString());
Assert.assertEquals("-1.2345678901234e17", small.newDfp(-123456789012344678L).toString());
Assert.assertEquals("1.2345678901235e17", small.newDfp(123456789012345678L).toString());
Assert.assertEquals("-1.2345678901235e17", small.newDfp(-123456789012345678L).toString());
Assert.assertEquals("1.00000000000000e18", small.newDfp(999999999999999999L).toString());

// 19 digits, 15 kept: 1789 rounds down, 6789 rounds up
Assert.assertEquals("1.23456789012345e18", small.newDfp(1234567890123451789L).toString());
Assert.assertEquals("-1.23456789012345e18", small.newDfp(-1234567890123451789L).toString());
Assert.assertEquals("1.23456789012346e18", small.newDfp(1234567890123456789L).toString());
Assert.assertEquals("-1.23456789012346e18", small.newDfp(-1234567890123456789L).toString());

// the values that used to throw ArrayIndexOutOfBoundsException
Assert.assertEquals("9.22337203685478e18", small.newDfp(Long.MAX_VALUE).toString());
Assert.assertEquals("-9.22337203685478e18", small.newDfp(Long.MIN_VALUE).toString());
Assert.assertEquals("-9.22337203685478e18", small.newDfp(Long.MIN_VALUE + 1).toString());

// the long and the string constructor agree on the rounded value
Assert.assertEquals(small.newDfp("9223372036854775807"), small.newDfp(Long.MAX_VALUE));
Assert.assertEquals(small.newDfp("-9223372036854775808"), small.newDfp(Long.MIN_VALUE));
Assert.assertEquals(small.newDfp("12345678901234567"), small.newDfp(12345678901234567L));
Assert.assertEquals(small.newDfp("1234567890123456789"), small.newDfp(1234567890123456789L));
}

/*
* Test addition
*/
Expand Down
Loading