public static boolean isInteger(final Number n) { final Type t = Type.get(n.getClass()); if (t == null) { final String msg = format("Unexpected number type %s passed to isInteger method.", n.getClass().getCanonicalName()); _logger.warning(msg); throw new IllegalArgumentException(msg); } if (t.isIntegerOnly()) return true; if (t.equals(Type.BIG_DECIMAL)) { final BigDecimal decVal = (BigDecimal) n; return BigInteger.ZERO.equals(decVal.unscaledValue()) || decVal.stripTrailingZeros().scale() <= 0; } else if (t.equals(Type.RATIONAL)) { final Rational ratVal = (Rational) n; if (compare(1, ratVal.getDenominator()) == 0) return true; else return compare(1, Rational.simplify(ratVal).getDenominator()) == 0; } return false; }
public static boolean isInteger(final Number n) { final Type t = Type.get(n.getClass()); if (t == null) { final String msg = format("Unexpected number type %s passed to isInteger method.", n.getClass().getCanonicalName()); _logger.warning(msg); throw new IllegalArgumentException(msg); } if (t.isIntegerOnly()) return true; if (t.equals(Type.BIG_DECIMAL)) { final BigDecimal decVal = (BigDecimal) n; return BigInteger.ZERO.equals(decVal.unscaledValue()) || decVal.stripTrailingZeros().scale() <= 0; } else if (t.equals(Type.RATIONAL)) { final Rational ratVal = (Rational) n; if (compare(1, ratVal.getDenominator()) == 0) return true; else return compare(1, Rational.simplify(ratVal).getDenominator()) == 0; } return false; }
public static Rational simplify(final Rational r) { BigInteger n = OWLRealUtils.bigInteger(r.getNumerator()); BigInteger d = OWLRealUtils.bigInteger(r.getDenominator()); BigInteger gcd = n.gcd(d); while (!gcd.equals(BigInteger.ONE)) { n = n.divide(gcd); d = d.divide(gcd); gcd = n.gcd(d); } final Number canonicalNum = OWLRealUtils.getCanonicalObject(n); final Number canonicalDenom = OWLRealUtils.getCanonicalObject(d); if (canonicalNum.equals(r.getNumerator()) && canonicalDenom.equals(r.getDenominator())) return r; else return new Rational(canonicalNum, canonicalDenom); }
public static Rational simplify(final Rational r) { BigInteger n = OWLRealUtils.bigInteger(r.getNumerator()); BigInteger d = OWLRealUtils.bigInteger(r.getDenominator()); BigInteger gcd = n.gcd(d); while (!gcd.equals(BigInteger.ONE)) { n = n.divide(gcd); d = d.divide(gcd); gcd = n.gcd(d); } final Number canonicalNum = OWLRealUtils.getCanonicalObject(n); final Number canonicalDenom = OWLRealUtils.getCanonicalObject(d); if (canonicalNum.equals(r.getNumerator()) && canonicalDenom.equals(r.getDenominator())) return r; else return new Rational(canonicalNum, canonicalDenom); }
if (compare(1, r.getDenominator()) != 0) if (compare(1, r.getDenominator()) != 0) throw new IllegalArgumentException();
if (compare(1, r.getDenominator()) != 0) if (compare(1, r.getDenominator()) != 0) throw new IllegalArgumentException();
if (as == bs) if (OWLRealUtils.compare(a.getDenominator(), b.getDenominator()) == 0) return OWLRealUtils.compare(a.getNumerator(), b.getNumerator()); else final BigInteger aprod = OWLRealUtils.bigInteger(a.getNumerator()).multiply(OWLRealUtils.bigInteger(b.getDenominator())); final BigInteger bprod = OWLRealUtils.bigInteger(b.getNumerator()).multiply(OWLRealUtils.bigInteger(a.getDenominator())); return aprod.compareTo(bprod);
if (as == bs) if (OWLRealUtils.compare(a.getDenominator(), b.getDenominator()) == 0) return OWLRealUtils.compare(a.getNumerator(), b.getNumerator()); else final BigInteger aprod = OWLRealUtils.bigInteger(a.getNumerator()).multiply(OWLRealUtils.bigInteger(b.getDenominator())); final BigInteger bprod = OWLRealUtils.bigInteger(b.getNumerator()).multiply(OWLRealUtils.bigInteger(a.getDenominator())); return aprod.compareTo(bprod);