DfpDec extends Dfp { @Override public Dfp nextAfter(Dfp x) { final String trapName = "nextAfter"; if (getField().getRadixDigits() != x.getField().getRadixDigits()) { getField().setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, trapName, x, result); } boolean up = false; Dfp result; Dfp inc; if (this.lessThan(x)) { up = true; } if (equals(x)) { return newInstance(x); } if (lessThan(getZero())) { up = !up; } if (up) { inc = power10(intLog10() - getDecimalDigits() + 1); inc = copysign(inc, this); if (this.equals(getZero())) { inc = power10K(MIN_EXP-mant.length-1); } if (inc.equals(getZero())) { result = copysign(newInstance(getZero()), this); } else { result = add(inc); } } else { inc = power10(intLog10()); inc = copysign(inc, this); if (this.equals(inc)) { inc = inc.divide(power10(getDecimalDigits())); } else { inc = inc.divide(power10(getDecimalDigits() - 1)); } if (this.equals(getZero())) { inc = power10K(MIN_EXP-mant.length-1); } if (inc.equals(getZero())) { result = copysign(newInstance(getZero()), this); } else { result = subtract(inc); } } if (result.classify() == INFINITE && this.classify() != INFINITE) { getField().setIEEEFlagsBits(DfpField.FLAG_INEXACT); result = dotrap(DfpField.FLAG_INEXACT, trapName, x, result); } if (result.equals(getZero()) && this.equals(getZero()) == false) { getField().setIEEEFlagsBits(DfpField.FLAG_INEXACT); result = dotrap(DfpField.FLAG_INEXACT, trapName, x, result); } return result; } protected  DfpDec(final DfpField factory); protected  DfpDec(final DfpField factory, byte x); protected  DfpDec(final DfpField factory, int x); protected  DfpDec(final DfpField factory, long x); protected  DfpDec(final DfpField factory, double x);  DfpDec(final Dfp d); protected  DfpDec(final DfpField factory, final String s); protected  DfpDec(final DfpField factory, final byte sign, final byte nans); @Override Dfp newInstance(); @Override Dfp newInstance(final byte x); @Override Dfp newInstance(final int x); @Override Dfp newInstance(final long x); @Override Dfp newInstance(final double x); @Override Dfp newInstance(final Dfp d); @Override Dfp newInstance(final String s); @Override Dfp newInstance(final byte sign, final byte nans); @Override Dfp nextAfter(Dfp x);  }
@Test public void testNextAfter() { test(new DfpDec(field, 1).nextAfter(pinf), new DfpDec(field, "1.0000000000000001"), 0, "NextAfter #1"); test(new DfpDec(field, "1.0000000000000001").nextAfter(ninf), new DfpDec(field, 1), 0, "NextAfter #1.5"); test(new DfpDec(field, 1).nextAfter(ninf), new DfpDec(field, "0.99999999999999999"), 0, "NextAfter #2"); test(new DfpDec(field, "0.99999999999999999").nextAfter(new DfpDec(field, 2)), new DfpDec(field, 1), 0, "NextAfter #3"); test(new DfpDec(field, -1).nextAfter(ninf), new DfpDec(field, "-1.0000000000000001"), 0, "NextAfter #4"); test(new DfpDec(field, -1).nextAfter(pinf), new DfpDec(field, "-0.99999999999999999"), 0, "NextAfter #5"); test(new DfpDec(field, "-0.99999999999999999").nextAfter(new DfpDec(field, -2)), new DfpDec(field, (byte) -1), 0, "NextAfter #6"); test(new DfpDec(field, (byte) 2).nextAfter(new DfpDec(field, 2)), new DfpDec(field, 2l), 0, "NextAfter #7"); test(new DfpDec(field, 0).nextAfter(new DfpDec(field, 0)), new DfpDec(field, 0), 0, "NextAfter #8"); test(new DfpDec(field, -2).nextAfter(new DfpDec(field, -2)), new DfpDec(field, -2), 0, "NextAfter #9"); test(new DfpDec(field, 0).nextAfter(new DfpDec(field, 1)), new DfpDec(field, "1e-131092"), DfpField.FLAG_UNDERFLOW, "NextAfter #10"); test(new DfpDec(field, 0).nextAfter(new DfpDec(field, -1)), new DfpDec(field, "-1e-131092"), DfpField.FLAG_UNDERFLOW, "NextAfter #11"); test(new DfpDec(field, "-1e-131092").nextAfter(pinf), new DfpDec(field, "-0"), DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #12"); test(new DfpDec(field, "1e-131092").nextAfter(ninf), new DfpDec(field, "0"), DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #13"); test(new DfpDec(field, "9.9999999999999999e131078").nextAfter(pinf), pinf, DfpField.FLAG_OVERFLOW|DfpField.FLAG_INEXACT, "Next After #14"); }