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 @@ -349,6 +349,8 @@ protected Dfp(final DfpField field, final String s) {
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)) {
Expand Down Expand Up @@ -434,18 +436,26 @@ protected Dfp(final DfpField field, final String s) {

// 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) {
Expand Down Expand Up @@ -479,6 +489,8 @@ protected Dfp(final DfpField field, final String s) {
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) {
Expand Down Expand Up @@ -547,8 +559,26 @@ protected Dfp(final DfpField field, final String s) {
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,74 @@ public void testStringConstructorDigitsBeyondPrecision() {
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
Expand Down
Loading