private static Rational rational(final Number n, final Number d) { return new Rational(n, d); }
private static Rational rational(final Number a, final Number b) { return new Rational(a, b); }
public static Rational valueOf(final String s) throws NumberFormatException { if (s == null) throw new NullPointerException(); final Matcher m = _lexicalPattern.matcher(s); if (!m.matches()) throw new NumberFormatException(s); final String sLex = m.group(1); final String nLex = m.group(2); final String dLex = m.group(3); final Number n = OWLRealUtils.getCanonicalObject(DatatypeConverter.parseInteger(sLex == null ? nLex : sLex + nLex)); final Number d = OWLRealUtils.getCanonicalObject(DatatypeConverter.parseInteger(dLex)); if (OWLRealUtils.signum(d) != 1) throw new NumberFormatException(s); return new Rational(n, d); }
public static Rational valueOf(final String s) throws NumberFormatException { if (s == null) throw new NullPointerException(); final Matcher m = _lexicalPattern.matcher(s); if (!m.matches()) throw new NumberFormatException(s); final String sLex = m.group(1); final String nLex = m.group(2); final String dLex = m.group(3); final Number n = OWLRealUtils.getCanonicalObject(DatatypeConverter.parseInteger(sLex == null ? nLex : sLex + nLex)); final Number d = OWLRealUtils.getCanonicalObject(DatatypeConverter.parseInteger(dLex)); if (OWLRealUtils.signum(d) != 1) throw new NumberFormatException(s); return new Rational(n, d); }
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); }
/** * Test parsing of valid values */ @Test public void validParse() { Rational r; r = Rational.valueOf("1 / 3"); assertNotNull(r); assertEquals(new Rational(OWLRealUtils.getCanonicalObject(1), OWLRealUtils.getCanonicalObject(3)), r); r = Rational.valueOf("12 / 36"); assertNotNull(r); assertEquals(new Rational(OWLRealUtils.getCanonicalObject(12), OWLRealUtils.getCanonicalObject(36)), r); r = Rational.valueOf("-1 / 3"); assertNotNull(r); assertEquals(new Rational(OWLRealUtils.getCanonicalObject(-1), OWLRealUtils.getCanonicalObject(3)), r); } }
/** * Test that equality is dependent on numerator and denominator, not numeric value. */ @Test public void objectEquals() { assertEquals(rational("1/3"), rational("1/3")); assertFalse(rational("1/3").equals(rational("1/4"))); assertFalse(rational("1/3").equals(rational("10/30"))); assertFalse(rational("1/3").equals(rational("-1/3"))); /* * The following holds because !Integer(1).equals(Long(1)) */ assertFalse(new Rational(1, 3).equals(new Rational(1l, 3l))); }
/** * Test the compareTo method */ @Test public void numericCompare() { assertEquals(0, rational("1/3").compareTo(rational("17/51"))); assertEquals(-1, rational("1/3").compareTo(rational("18/51"))); assertEquals(1, rational("1/3").compareTo(rational("16/51"))); assertEquals(0, rational("-1/3").compareTo(rational("-17/51"))); assertEquals(1, rational("-1/3").compareTo(rational("-18/51"))); assertEquals(-1, rational("-1/3").compareTo(rational("-16/51"))); assertEquals(0, new Rational(1, 3).compareTo(new Rational(1l, 3l))); }