Dfp implements RealFieldElement<Dfp> { private static int compare(final Dfp a, final Dfp b) { if (a.mant[a.mant.length - 1] == 0 && b.mant[b.mant.length - 1] == 0 && a.nans == FINITE && b.nans == FINITE) { return 0; } if (a.sign != b.sign) { if (a.sign == -1) { return -1; } else { return 1; } } if (a.nans == INFINITE && b.nans == FINITE) { return a.sign; } if (a.nans == FINITE && b.nans == INFINITE) { return -b.sign; } if (a.nans == INFINITE && b.nans == INFINITE) { return 0; } if (b.mant[b.mant.length-1] != 0 && a.mant[b.mant.length-1] != 0) { if (a.exp < b.exp) { return -a.sign; } if (a.exp > b.exp) { return a.sign; } } for (int i = a.mant.length - 1; i >= 0; i--) { if (a.mant[i] > b.mant[i]) { return a.sign; } if (a.mant[i] < b.mant[i]) { return -a.sign; } } return 0; } 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 testCompare() { field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("0"), "equal", true, 1); cmptst(field.newDfp("0"), field.newDfp("-0"), "equal", true, 2); cmptst(field.newDfp("-0"), field.newDfp("-0"), "equal", true, 3); cmptst(field.newDfp("-0"), field.newDfp("0"), "equal", true, 4); cmptst(field.newDfp("0"), field.newDfp("1"), "equal", false, 5); cmptst(field.newDfp("1"), field.newDfp("0"), "equal", false, 6); cmptst(field.newDfp("-1"), field.newDfp("0"), "equal", false, 7); cmptst(field.newDfp("0"), field.newDfp("-1"), "equal", false, 8); cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "equal", false, 9); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "equal", false, 10); if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "equal", false, 11); cmptst(field.newDfp("0"), pinf, "equal", false, 12); cmptst(field.newDfp("0"), ninf, "equal", false, 13); cmptst(field.newDfp("-0"), pinf, "equal", false, 14); cmptst(field.newDfp("-0"), ninf, "equal", false, 15); cmptst(pinf, field.newDfp("0"), "equal", false, 16); cmptst(ninf, field.newDfp("0"), "equal", false, 17); cmptst(pinf, field.newDfp("-0"), "equal", false, 18); cmptst(ninf, field.newDfp("-0"), "equal", false, 19); cmptst(ninf, pinf, "equal", false, 19.10); cmptst(pinf, ninf, "equal", false, 19.11); cmptst(pinf, pinf, "equal", true, 19.12); cmptst(ninf, ninf, "equal", true, 19.13); cmptst(field.newDfp("1"), field.newDfp("1"), "equal", true, 20); cmptst(field.newDfp("1"), field.newDfp("-1"), "equal", false, 21); cmptst(field.newDfp("-1"), field.newDfp("-1"), "equal", true, 22); cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "equal", false, 23); cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "equal", false, 24); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "equal", true, 25); cmptst(snan, snan, "equal", false, 27); cmptst(qnan, qnan, "equal", false, 28); cmptst(snan, qnan, "equal", false, 29); cmptst(qnan, snan, "equal", false, 30); cmptst(qnan, field.newDfp("0"), "equal", false, 31); cmptst(snan, field.newDfp("0"), "equal", false, 32); cmptst(field.newDfp("0"), snan, "equal", false, 33); cmptst(field.newDfp("0"), qnan, "equal", false, 34); cmptst(qnan, pinf, "equal", false, 35); cmptst(snan, pinf, "equal", false, 36); cmptst(pinf, snan, "equal", false, 37); cmptst(pinf, qnan, "equal", false, 38); cmptst(qnan, ninf, "equal", false, 39); cmptst(snan, ninf, "equal", false, 40); cmptst(ninf, snan, "equal", false, 41); cmptst(ninf, qnan, "equal", false, 42); cmptst(qnan, field.newDfp("-1"), "equal", false, 43); cmptst(snan, field.newDfp("-1"), "equal", false, 44); cmptst(field.newDfp("-1"), snan, "equal", false, 45); cmptst(field.newDfp("-1"), qnan, "equal", false, 46); cmptst(qnan, field.newDfp("1"), "equal", false, 47); cmptst(snan, field.newDfp("1"), "equal", false, 48); cmptst(field.newDfp("1"), snan, "equal", false, 49); cmptst(field.newDfp("1"), qnan, "equal", false, 50); cmptst(snan.negate(), snan, "equal", false, 51); cmptst(qnan.negate(), qnan, "equal", false, 52); cmptst(field.newDfp("0"), field.newDfp("0"), "unequal", false, 1); cmptst(field.newDfp("0"), field.newDfp("-0"), "unequal", false, 2); cmptst(field.newDfp("-0"), field.newDfp("-0"), "unequal", false, 3); cmptst(field.newDfp("-0"), field.newDfp("0"), "unequal", false, 4); cmptst(field.newDfp("0"), field.newDfp("1"), "unequal", true, 5); cmptst(field.newDfp("1"), field.newDfp("0"), "unequal", true, 6); cmptst(field.newDfp("-1"), field.newDfp("0"), "unequal", true, 7); cmptst(field.newDfp("0"), field.newDfp("-1"), "unequal", true, 8); cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "unequal", true, 9); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "unequal", true, 10); if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "unequal", true, 11); cmptst(field.newDfp("0"), pinf, "unequal", true, 12); cmptst(field.newDfp("0"), ninf, "unequal", true, 13); cmptst(field.newDfp("-0"), pinf, "unequal", true, 14); cmptst(field.newDfp("-0"), ninf, "unequal", true, 15); cmptst(pinf, field.newDfp("0"), "unequal", true, 16); cmptst(ninf, field.newDfp("0"), "unequal", true, 17); cmptst(pinf, field.newDfp("-0"), "unequal", true, 18); cmptst(ninf, field.newDfp("-0"), "unequal", true, 19); cmptst(ninf, pinf, "unequal", true, 19.10); cmptst(pinf, ninf, "unequal", true, 19.11); cmptst(pinf, pinf, "unequal", false, 19.12); cmptst(ninf, ninf, "unequal", false, 19.13); cmptst(field.newDfp("1"), field.newDfp("1"), "unequal", false, 20); cmptst(field.newDfp("1"), field.newDfp("-1"), "unequal", true, 21); cmptst(field.newDfp("-1"), field.newDfp("-1"), "unequal", false, 22); cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "unequal", true, 23); cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "unequal", true, 24); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "unequal", false, 25); cmptst(snan, snan, "unequal", false, 27); cmptst(qnan, qnan, "unequal", false, 28); cmptst(snan, qnan, "unequal", false, 29); cmptst(qnan, snan, "unequal", false, 30); cmptst(qnan, field.newDfp("0"), "unequal", false, 31); cmptst(snan, field.newDfp("0"), "unequal", false, 32); cmptst(field.newDfp("0"), snan, "unequal", false, 33); cmptst(field.newDfp("0"), qnan, "unequal", false, 34); cmptst(qnan, pinf, "unequal", false, 35); cmptst(snan, pinf, "unequal", false, 36); cmptst(pinf, snan, "unequal", false, 37); cmptst(pinf, qnan, "unequal", false, 38); cmptst(qnan, ninf, "unequal", false, 39); cmptst(snan, ninf, "unequal", false, 40); cmptst(ninf, snan, "unequal", false, 41); cmptst(ninf, qnan, "unequal", false, 42); cmptst(qnan, field.newDfp("-1"), "unequal", false, 43); cmptst(snan, field.newDfp("-1"), "unequal", false, 44); cmptst(field.newDfp("-1"), snan, "unequal", false, 45); cmptst(field.newDfp("-1"), qnan, "unequal", false, 46); cmptst(qnan, field.newDfp("1"), "unequal", false, 47); cmptst(snan, field.newDfp("1"), "unequal", false, 48); cmptst(field.newDfp("1"), snan, "unequal", false, 49); cmptst(field.newDfp("1"), qnan, "unequal", false, 50); cmptst(snan.negate(), snan, "unequal", false, 51); cmptst(qnan.negate(), qnan, "unequal", false, 52); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare unequal flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0"), field.newDfp("0"), "lessThan", false, 1); cmptst(field.newDfp("0"), field.newDfp("-0"), "lessThan", false, 2); cmptst(field.newDfp("-0"), field.newDfp("-0"), "lessThan", false, 3); cmptst(field.newDfp("-0"), field.newDfp("0"), "lessThan", false, 4); cmptst(field.newDfp("0"), field.newDfp("1"), "lessThan", true, 5); cmptst(field.newDfp("1"), field.newDfp("0"), "lessThan", false, 6); cmptst(field.newDfp("-1"), field.newDfp("0"), "lessThan", true, 7); cmptst(field.newDfp("0"), field.newDfp("-1"), "lessThan", false, 8); cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "lessThan", true, 9); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "lessThan", true, 10); if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "lessThan", true, 11); cmptst(field.newDfp("0"), pinf, "lessThan", true, 12); cmptst(field.newDfp("0"), ninf, "lessThan", false, 13); cmptst(field.newDfp("-0"), pinf, "lessThan", true, 14); cmptst(field.newDfp("-0"), ninf, "lessThan", false, 15); cmptst(pinf, field.newDfp("0"), "lessThan", false, 16); cmptst(ninf, field.newDfp("0"), "lessThan", true, 17); cmptst(pinf, field.newDfp("-0"), "lessThan", false, 18); cmptst(ninf, field.newDfp("-0"), "lessThan", true, 19); cmptst(ninf, pinf, "lessThan", true, 19.10); cmptst(pinf, ninf, "lessThan", false, 19.11); cmptst(pinf, pinf, "lessThan", false, 19.12); cmptst(ninf, ninf, "lessThan", false, 19.13); cmptst(field.newDfp("1"), field.newDfp("1"), "lessThan", false, 20); cmptst(field.newDfp("1"), field.newDfp("-1"), "lessThan", false, 21); cmptst(field.newDfp("-1"), field.newDfp("-1"), "lessThan", false, 22); cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "lessThan", true, 23); cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "lessThan", false, 24); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "lessThan", false, 25); cmptst(snan, snan, "lessThan", false, 27); cmptst(qnan, qnan, "lessThan", false, 28); cmptst(snan, qnan, "lessThan", false, 29); cmptst(qnan, snan, "lessThan", false, 30); cmptst(qnan, field.newDfp("0"), "lessThan", false, 31); cmptst(snan, field.newDfp("0"), "lessThan", false, 32); cmptst(field.newDfp("0"), snan, "lessThan", false, 33); cmptst(field.newDfp("0"), qnan, "lessThan", false, 34); cmptst(qnan, pinf, "lessThan", false, 35); cmptst(snan, pinf, "lessThan", false, 36); cmptst(pinf, snan, "lessThan", false, 37); cmptst(pinf, qnan, "lessThan", false, 38); cmptst(qnan, ninf, "lessThan", false, 39); cmptst(snan, ninf, "lessThan", false, 40); cmptst(ninf, snan, "lessThan", false, 41); cmptst(ninf, qnan, "lessThan", false, 42); cmptst(qnan, field.newDfp("-1"), "lessThan", false, 43); cmptst(snan, field.newDfp("-1"), "lessThan", false, 44); cmptst(field.newDfp("-1"), snan, "lessThan", false, 45); cmptst(field.newDfp("-1"), qnan, "lessThan", false, 46); cmptst(qnan, field.newDfp("1"), "lessThan", false, 47); cmptst(snan, field.newDfp("1"), "lessThan", false, 48); cmptst(field.newDfp("1"), snan, "lessThan", false, 49); cmptst(field.newDfp("1"), qnan, "lessThan", false, 50); cmptst(snan.negate(), snan, "lessThan", false, 51); cmptst(qnan.negate(), qnan, "lessThan", false, 52); if (field.getIEEEFlags() != DfpField.FLAG_INVALID) Assert.fail("assersion failed. compare lessThan flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("0"), "greaterThan", false, 1); cmptst(field.newDfp("0"), field.newDfp("-0"), "greaterThan", false, 2); cmptst(field.newDfp("-0"), field.newDfp("-0"), "greaterThan", false, 3); cmptst(field.newDfp("-0"), field.newDfp("0"), "greaterThan", false, 4); cmptst(field.newDfp("0"), field.newDfp("1"), "greaterThan", false, 5); cmptst(field.newDfp("1"), field.newDfp("0"), "greaterThan", true, 6); cmptst(field.newDfp("-1"), field.newDfp("0"), "greaterThan", false, 7); cmptst(field.newDfp("0"), field.newDfp("-1"), "greaterThan", true, 8); cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "greaterThan", false, 9); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "greaterThan", false, 10); if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "greaterThan", false, 11); cmptst(field.newDfp("0"), pinf, "greaterThan", false, 12); cmptst(field.newDfp("0"), ninf, "greaterThan", true, 13); cmptst(field.newDfp("-0"), pinf, "greaterThan", false, 14); cmptst(field.newDfp("-0"), ninf, "greaterThan", true, 15); cmptst(pinf, field.newDfp("0"), "greaterThan", true, 16); cmptst(ninf, field.newDfp("0"), "greaterThan", false, 17); cmptst(pinf, field.newDfp("-0"), "greaterThan", true, 18); cmptst(ninf, field.newDfp("-0"), "greaterThan", false, 19); cmptst(ninf, pinf, "greaterThan", false, 19.10); cmptst(pinf, ninf, "greaterThan", true, 19.11); cmptst(pinf, pinf, "greaterThan", false, 19.12); cmptst(ninf, ninf, "greaterThan", false, 19.13); cmptst(field.newDfp("1"), field.newDfp("1"), "greaterThan", false, 20); cmptst(field.newDfp("1"), field.newDfp("-1"), "greaterThan", true, 21); cmptst(field.newDfp("-1"), field.newDfp("-1"), "greaterThan", false, 22); cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "greaterThan", false, 23); cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "greaterThan", true, 24); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "greaterThan", false, 25); cmptst(snan, snan, "greaterThan", false, 27); cmptst(qnan, qnan, "greaterThan", false, 28); cmptst(snan, qnan, "greaterThan", false, 29); cmptst(qnan, snan, "greaterThan", false, 30); cmptst(qnan, field.newDfp("0"), "greaterThan", false, 31); cmptst(snan, field.newDfp("0"), "greaterThan", false, 32); cmptst(field.newDfp("0"), snan, "greaterThan", false, 33); cmptst(field.newDfp("0"), qnan, "greaterThan", false, 34); cmptst(qnan, pinf, "greaterThan", false, 35); cmptst(snan, pinf, "greaterThan", false, 36); cmptst(pinf, snan, "greaterThan", false, 37); cmptst(pinf, qnan, "greaterThan", false, 38); cmptst(qnan, ninf, "greaterThan", false, 39); cmptst(snan, ninf, "greaterThan", false, 40); cmptst(ninf, snan, "greaterThan", false, 41); cmptst(ninf, qnan, "greaterThan", false, 42); cmptst(qnan, field.newDfp("-1"), "greaterThan", false, 43); cmptst(snan, field.newDfp("-1"), "greaterThan", false, 44); cmptst(field.newDfp("-1"), snan, "greaterThan", false, 45); cmptst(field.newDfp("-1"), qnan, "greaterThan", false, 46); cmptst(qnan, field.newDfp("1"), "greaterThan", false, 47); cmptst(snan, field.newDfp("1"), "greaterThan", false, 48); cmptst(field.newDfp("1"), snan, "greaterThan", false, 49); cmptst(field.newDfp("1"), qnan, "greaterThan", false, 50); cmptst(snan.negate(), snan, "greaterThan", false, 51); cmptst(qnan.negate(), qnan, "greaterThan", false, 52); if (field.getIEEEFlags() != DfpField.FLAG_INVALID) Assert.fail("assersion failed. compare greaterThan flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); }