Dfp implements RealFieldElement<Dfp> { public Dfp multiply(final Dfp x) { if (field.getRadixDigits() != x.field.getRadixDigits()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, MULTIPLY_TRAP, x, result); } Dfp result = newInstance(getZero()); if (nans != FINITE || x.nans != FINITE) { if (isNaN()) { return this; } if (x.isNaN()) { return x; } if (nans == INFINITE && x.nans == FINITE && x.mant[mant.length-1] != 0) { result = newInstance(this); result.sign = (byte) (sign * x.sign); return result; } if (x.nans == INFINITE && nans == FINITE && mant[mant.length-1] != 0) { result = newInstance(x); result.sign = (byte) (sign * x.sign); return result; } if (x.nans == INFINITE && nans == INFINITE) { result = newInstance(this); result.sign = (byte) (sign * x.sign); return result; } if ( (x.nans == INFINITE && nans == FINITE && mant[mant.length-1] == 0) || (nans == INFINITE && x.nans == FINITE && x.mant[mant.length-1] == 0) ) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); result = newInstance(getZero()); result.nans = QNAN; result = dotrap(DfpField.FLAG_INVALID, MULTIPLY_TRAP, x, result); return result; } } int[] product = new int[mant.length*2]; for (int i = 0; i < mant.length; i++) { int rh = 0; for (int j=0; j<mant.length; j++) { int r = mant[i] * x.mant[j]; r += product[i+j] + rh; rh = r / RADIX; product[i+j] = r - rh * RADIX; } product[i+mant.length] = rh; } int md = mant.length * 2 - 1; for (int i = mant.length * 2 - 1; i >= 0; i--) { if (product[i] != 0) { md = i; break; } } for (int i = 0; i < mant.length; i++) { result.mant[mant.length - i - 1] = product[md - i]; } result.exp = exp + x.exp + md - 2 * mant.length + 1; result.sign = (byte)((sign == x.sign)?1:-1); if (result.mant[mant.length-1] == 0) { result.exp = 0; } final int excp; if (md > (mant.length-1)) { excp = result.round(product[md-mant.length]); } else { excp = result.round(0); } if (excp != 0) { result = dotrap(excp, MULTIPLY_TRAP, x, 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 testMultiply() { test(field.newDfp("1").multiply(field.newDfp("1")), field.newDfp("1"), 0, "Multiply #1"); test(field.newDfp("1").multiply(1), field.newDfp("1"), 0, "Multiply #2"); test(field.newDfp("-1").multiply(field.newDfp("1")), field.newDfp("-1"), 0, "Multiply #3"); test(field.newDfp("-1").multiply(1), field.newDfp("-1"), 0, "Multiply #4"); test(field.newDfp("2").multiply(field.newDfp("3")), field.newDfp("6"), 0, "Multiply #5"); test(field.newDfp("2").multiply(3), field.newDfp("6"), 0, "Multiply #6"); test(field.newDfp("-2").multiply(field.newDfp("3")), field.newDfp("-6"), 0, "Multiply #7"); test(field.newDfp("-2").multiply(3), field.newDfp("-6"), 0, "Multiply #8"); test(field.newDfp("2").multiply(field.newDfp("-3")), field.newDfp("-6"), 0, "Multiply #9"); test(field.newDfp("-2").multiply(field.newDfp("-3")), field.newDfp("6"), 0, "Multiply #10"); test(field.newDfp("-2").multiply(field.newDfp("0")), field.newDfp("-0"), 0, "Multiply #11"); test(field.newDfp("-2").multiply(0), field.newDfp("-0"), 0, "Multiply #12"); test(field.newDfp("2").multiply(field.newDfp("0")), field.newDfp("0"), 0, "Multiply #13"); test(field.newDfp("2").multiply(0), field.newDfp("0"), 0, "Multiply #14"); test(field.newDfp("2").multiply(pinf), pinf, 0, "Multiply #15"); test(field.newDfp("2").multiply(ninf), ninf, 0, "Multiply #16"); test(field.newDfp("-2").multiply(pinf), ninf, 0, "Multiply #17"); test(field.newDfp("-2").multiply(ninf), pinf, 0, "Multiply #18"); test(ninf.multiply(field.newDfp("-2")), pinf, 0, "Multiply #18.1"); test(field.newDfp("5e131071").multiply(2), pinf, DfpField.FLAG_OVERFLOW, "Multiply #19"); test(field.newDfp("5e131071").multiply(field.newDfp("1.999999999999999")), field.newDfp("9.9999999999999950000e131071"), 0, "Multiply #20"); test(field.newDfp("-5e131071").multiply(2), ninf, DfpField.FLAG_OVERFLOW, "Multiply #22"); test(field.newDfp("-5e131071").multiply(field.newDfp("1.999999999999999")), field.newDfp("-9.9999999999999950000e131071"), 0, "Multiply #23"); test(field.newDfp("1e-65539").multiply(field.newDfp("1e-65539")), field.newDfp("1e-131078"), DfpField.FLAG_UNDERFLOW, "Multiply #24"); test(field.newDfp("1").multiply(nan), nan, 0, "Multiply #25"); test(nan.multiply(field.newDfp("1")), nan, 0, "Multiply #26"); test(nan.multiply(pinf), nan, 0, "Multiply #27"); test(pinf.multiply(nan), nan, 0, "Multiply #27"); test(pinf.multiply(field.newDfp("0")), nan, DfpField.FLAG_INVALID, "Multiply #28"); test(field.newDfp("0").multiply(pinf), nan, DfpField.FLAG_INVALID, "Multiply #29"); test(pinf.multiply(pinf), pinf, 0, "Multiply #30"); test(ninf.multiply(pinf), ninf, 0, "Multiply #31"); test(pinf.multiply(ninf), ninf, 0, "Multiply #32"); test(ninf.multiply(ninf), pinf, 0, "Multiply #33"); test(pinf.multiply(1), pinf, 0, "Multiply #34"); test(pinf.multiply(0), nan, DfpField.FLAG_INVALID, "Multiply #35"); test(nan.multiply(1), nan, 0, "Multiply #36"); test(field.newDfp("1").multiply(10000), field.newDfp("10000"), 0, "Multiply #37"); test(field.newDfp("2").multiply(1000000), field.newDfp("2000000"), 0, "Multiply #38"); test(field.newDfp("1").multiply(-1), field.newDfp("-1"), 0, "Multiply #39"); }