Dfp implements RealFieldElement<Dfp> { public Dfp divide(Dfp divisor) { int dividend[]; int quotient[]; int remainder[]; int qd; int nsqd; int trial=0; int minadj; boolean trialgood; int md=0; int excp; if (field.getRadixDigits() != divisor.field.getRadixDigits()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, DIVIDE_TRAP, divisor, result); } Dfp result = newInstance(getZero()); if (nans != FINITE || divisor.nans != FINITE) { if (isNaN()) { return this; } if (divisor.isNaN()) { return divisor; } if (nans == INFINITE && divisor.nans == FINITE) { result = newInstance(this); result.sign = (byte) (sign * divisor.sign); return result; } if (divisor.nans == INFINITE && nans == FINITE) { result = newInstance(getZero()); result.sign = (byte) (sign * divisor.sign); return result; } if (divisor.nans == INFINITE && nans == INFINITE) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); result = newInstance(getZero()); result.nans = QNAN; result = dotrap(DfpField.FLAG_INVALID, DIVIDE_TRAP, divisor, result); return result; } } if (divisor.mant[mant.length-1] == 0) { field.setIEEEFlagsBits(DfpField.FLAG_DIV_ZERO); result = newInstance(getZero()); result.sign = (byte) (sign * divisor.sign); result.nans = INFINITE; result = dotrap(DfpField.FLAG_DIV_ZERO, DIVIDE_TRAP, divisor, result); return result; } dividend = new int[mant.length+1]; quotient = new int[mant.length+2]; remainder = new int[mant.length+1]; dividend[mant.length] = 0; quotient[mant.length] = 0; quotient[mant.length+1] = 0; remainder[mant.length] = 0; for (int i = 0; i < mant.length; i++) { dividend[i] = mant[i]; quotient[i] = 0; remainder[i] = 0; } nsqd = 0; for (qd = mant.length+1; qd >= 0; qd--) { final int divMsb = dividend[mant.length]*RADIX+dividend[mant.length-1]; int min = divMsb / (divisor.mant[mant.length-1]+1); int max = (divMsb + 1) / divisor.mant[mant.length-1]; trialgood = false; while (!trialgood) { trial = (min+max)/2; int rh = 0; for (int i = 0; i < mant.length + 1; i++) { int dm = (i<mant.length)?divisor.mant[i]:0; final int r = (dm * trial) + rh; rh = r / RADIX; remainder[i] = r - rh * RADIX; } rh = 1; for (int i = 0; i < mant.length + 1; i++) { final int r = ((RADIX-1) - remainder[i]) + dividend[i] + rh; rh = r / RADIX; remainder[i] = r - rh * RADIX; } if (rh == 0) { max = trial-1; continue; } minadj = (remainder[mant.length] * RADIX)+remainder[mant.length-1]; minadj /= divisor.mant[mant.length-1] + 1; if (minadj >= 2) { min = trial+minadj; continue; } trialgood = false; for (int i = mant.length - 1; i >= 0; i--) { if (divisor.mant[i] > remainder[i]) { trialgood = true; } if (divisor.mant[i] < remainder[i]) { break; } } if (remainder[mant.length] != 0) { trialgood = false; } if (trialgood == false) { min = trial+1; } } quotient[qd] = trial; if (trial != 0 || nsqd != 0) { nsqd++; } if (field.getRoundingMode() == DfpField.RoundingMode.ROUND_DOWN && nsqd == mant.length) { break; } if (nsqd > mant.length) { break; } dividend[0] = 0; for (int i = 0; i < mant.length; i++) { dividend[i + 1] = remainder[i]; } } md = mant.length; for (int i = mant.length + 1; i >= 0; i--) { if (quotient[i] != 0) { md = i; break; } } for (int i=0; i<mant.length; i++) { result.mant[mant.length-i-1] = quotient[md-i]; } result.exp = exp - divisor.exp + md - mant.length; result.sign = (byte) ((sign == divisor.sign) ? 1 : -1); if (result.mant[mant.length-1] == 0) { result.exp = 0; } if (md > (mant.length-1)) { excp = result.round(quotient[md-mant.length]); } else { excp = result.round(0); } if (excp != 0) { result = dotrap(excp, DIVIDE_TRAP, divisor, result); } return result; } protected  Dfp(final DfpField field); protected  Dfp(final DfpField field, byte x); protected  Dfp(final DfpField field, int x); protected  Dfp(final DfpField field, long x); protected  Dfp(final DfpField field, double x);  Dfp(final Dfp d); protected  Dfp(final DfpField field, final String s); protected  Dfp(final DfpField field, final byte sign, final byte nans); Dfp newInstance(); Dfp newInstance(final byte x); Dfp newInstance(final int x); Dfp newInstance(final long x); Dfp newInstance(final double x); Dfp newInstance(final Dfp d); Dfp newInstance(final String s); Dfp newInstance(final byte sig, final byte code); DfpField getField(); int getRadixDigits(); Dfp getZero(); Dfp getOne(); Dfp getTwo(); boolean lessThan(final Dfp x); boolean greaterThan(final Dfp x); boolean negativeOrNull(); boolean strictlyNegative(); boolean positiveOrNull(); boolean strictlyPositive(); Dfp abs(); boolean isInfinite(); boolean isNaN(); boolean isZero(); @Override boolean equals(final Object other); @Override int hashCode(); boolean unequal(final Dfp x); Dfp rint(); Dfp floor(); Dfp ceil(); Dfp remainder(final Dfp d); int intValue(); int log10K(); Dfp power10K(final int e); int intLog10(); Dfp power10(final int e); Dfp add(final Dfp x); Dfp negate(); Dfp subtract(final Dfp x); Dfp multiply(final Dfp x); Dfp multiply(final int x); Dfp divide(Dfp divisor); Dfp divide(int divisor); Dfp reciprocal(); Dfp sqrt(); @Override String toString(); Dfp dotrap(int type, String what, Dfp oper, Dfp result); int classify(); static Dfp copysign(final Dfp x, final Dfp y); Dfp nextAfter(final Dfp x); double toDouble(); double[] toSplitDouble(); double getReal(); Dfp add(final double a); Dfp subtract(final double a); Dfp multiply(final double a); Dfp divide(final double a); Dfp remainder(final double a); long round(); Dfp signum(); Dfp copySign(final Dfp s); Dfp copySign(final double s); Dfp scalb(final int n); Dfp hypot(final Dfp y); Dfp cbrt(); Dfp rootN(final int n); Dfp pow(final double p); Dfp pow(final int n); Dfp pow(final Dfp e); Dfp exp(); Dfp expm1(); Dfp log(); Dfp log1p(); @Deprecated int log10(); Dfp cos(); Dfp sin(); Dfp tan(); Dfp acos(); Dfp asin(); Dfp atan(); Dfp atan2(final Dfp x); Dfp cosh(); Dfp sinh(); Dfp tanh(); Dfp acosh(); Dfp asinh(); Dfp atanh(); Dfp linearCombination(final Dfp[] a, final Dfp[] b); Dfp linearCombination(final double[] a, final Dfp[] b); Dfp linearCombination(final Dfp a1, final Dfp b1, final Dfp a2, final Dfp b2); Dfp linearCombination(final double a1, final Dfp b1, final double a2, final Dfp b2); Dfp linearCombination(final Dfp a1, final Dfp b1,
                                 final Dfp a2, final Dfp b2,
                                 final Dfp a3, final Dfp b3); Dfp linearCombination(final double a1, final Dfp b1,
                                 final double a2, final Dfp b2,
                                 final double a3, final Dfp b3); Dfp linearCombination(final Dfp a1, final Dfp b1, final Dfp a2, final Dfp b2,
                                 final Dfp a3, final Dfp b3, final Dfp a4, final Dfp b4); Dfp linearCombination(final double a1, final Dfp b1, final double a2, final Dfp b2,
                                 final double a3, final Dfp b3, final double a4, final Dfp b4); static final int RADIX; static final int MIN_EXP; static final int MAX_EXP; static final int ERR_SCALE; static final byte FINITE; static final byte INFINITE; static final byte SNAN; static final byte QNAN; }
@Test public void testDivide() { test(field.newDfp("1").divide(nan), nan, 0, "Divide #1"); test(nan.divide(field.newDfp("1")), nan, 0, "Divide #2"); test(pinf.divide(field.newDfp("1")), pinf, 0, "Divide #3"); test(pinf.divide(field.newDfp("-1")), ninf, 0, "Divide #4"); test(pinf.divide(pinf), nan, DfpField.FLAG_INVALID, "Divide #5"); test(ninf.divide(pinf), nan, DfpField.FLAG_INVALID, "Divide #6"); test(pinf.divide(ninf), nan, DfpField.FLAG_INVALID, "Divide #7"); test(ninf.divide(ninf), nan, DfpField.FLAG_INVALID, "Divide #8"); test(field.newDfp("0").divide(field.newDfp("0")), nan, DfpField.FLAG_DIV_ZERO, "Divide #9"); test(field.newDfp("1").divide(field.newDfp("0")), pinf, DfpField.FLAG_DIV_ZERO, "Divide #10"); test(field.newDfp("1").divide(field.newDfp("-0")), ninf, DfpField.FLAG_DIV_ZERO, "Divide #11"); test(field.newDfp("-1").divide(field.newDfp("0")), ninf, DfpField.FLAG_DIV_ZERO, "Divide #12"); test(field.newDfp("-1").divide(field.newDfp("-0")), pinf, DfpField.FLAG_DIV_ZERO, "Divide #13"); test(field.newDfp("1").divide(field.newDfp("3")), field.newDfp("0.33333333333333333333"), DfpField.FLAG_INEXACT, "Divide #14"); test(field.newDfp("1").divide(field.newDfp("6")), field.newDfp("0.16666666666666666667"), DfpField.FLAG_INEXACT, "Divide #15"); test(field.newDfp("10").divide(field.newDfp("6")), field.newDfp("1.6666666666666667"), DfpField.FLAG_INEXACT, "Divide #16"); test(field.newDfp("100").divide(field.newDfp("6")), field.newDfp("16.6666666666666667"), DfpField.FLAG_INEXACT, "Divide #17"); test(field.newDfp("1000").divide(field.newDfp("6")), field.newDfp("166.6666666666666667"), DfpField.FLAG_INEXACT, "Divide #18"); test(field.newDfp("10000").divide(field.newDfp("6")), field.newDfp("1666.6666666666666667"), DfpField.FLAG_INEXACT, "Divide #19"); test(field.newDfp("1").divide(field.newDfp("1")), field.newDfp("1"), 0, "Divide #20"); test(field.newDfp("1").divide(field.newDfp("-1")), field.newDfp("-1"), 0, "Divide #21"); test(field.newDfp("-1").divide(field.newDfp("1")), field.newDfp("-1"), 0, "Divide #22"); test(field.newDfp("-1").divide(field.newDfp("-1")), field.newDfp("1"), 0, "Divide #23"); test(field.newDfp("1e-65539").divide(field.newDfp("1e65539")), field.newDfp("1e-131078"), DfpField.FLAG_UNDERFLOW, "Divide #24"); test(field.newDfp("1e65539").divide(field.newDfp("1e-65539")), pinf, DfpField.FLAG_OVERFLOW, "Divide #24"); test(field.newDfp("2").divide(field.newDfp("1.5")), field.newDfp("1.3333333333333333"), DfpField.FLAG_INEXACT, "Divide #25"); test(field.newDfp("2").divide(pinf), field.newDfp("0"), 0, "Divide #26"); test(field.newDfp("2").divide(ninf), field.newDfp("-0"), 0, "Divide #27"); test(field.newDfp("0").divide(field.newDfp("1")), field.newDfp("0"), 0, "Divide #28"); }
@Test public void testDivideInt() { test(nan.divide(1), nan, 0, "DivideInt #1"); test(pinf.divide(1), pinf, 0, "DivideInt #2"); test(field.newDfp("0").divide(0), nan, DfpField.FLAG_DIV_ZERO, "DivideInt #3"); test(field.newDfp("1").divide(0), pinf, DfpField.FLAG_DIV_ZERO, "DivideInt #4"); test(field.newDfp("-1").divide(0), ninf, DfpField.FLAG_DIV_ZERO, "DivideInt #5"); test(field.newDfp("1").divide(3), field.newDfp("0.33333333333333333333"), DfpField.FLAG_INEXACT, "DivideInt #6"); test(field.newDfp("1").divide(6), field.newDfp("0.16666666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #7"); test(field.newDfp("10").divide(6), field.newDfp("1.6666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #8"); test(field.newDfp("100").divide(6), field.newDfp("16.6666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #9"); test(field.newDfp("1000").divide(6), field.newDfp("166.6666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #10"); test(field.newDfp("10000").divide(6), field.newDfp("1666.6666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #20"); test(field.newDfp("1").divide(1), field.newDfp("1"), 0, "DivideInt #21"); test(field.newDfp("1e-131077").divide(10), field.newDfp("1e-131078"), DfpField.FLAG_UNDERFLOW, "DivideInt #22"); test(field.newDfp("0").divide(1), field.newDfp("0"), 0, "DivideInt #23"); test(field.newDfp("1").divide(10000), nan, DfpField.FLAG_INVALID, "DivideInt #24"); test(field.newDfp("1").divide(-1), nan, DfpField.FLAG_INVALID, "DivideInt #25"); }