public static void main(String[] args) { Rational d, e, f; d = new Rational(1, 2); e = new Rational(2, 3); f = new Rational(2, 4); // System.out.println(d.getA()); System.out.println(d.ratNum()); System.out.println(d.equals(e)); System.out.println(d.equals(f)); System.out.println((d.mult(f)).ratNum()); System.out.println(d.compareTo(e)); }
/** * Returns a property for a tag with a RATIONAL value. If rawOutput is true, returns a property * with type RATIONAL. Otherwise, returns a property with type STRING, and the text representation * of the Rational value as a floating-point ratio. */ protected Property addRationalProperty(String name, Rational r, boolean rawOutput) { Property prop = null; if (!rawOutput) { prop = new Property(name, PropertyType.STRING, _format.format(r.toDouble())); } if (prop == null) { prop = new Property(name, PropertyType.RATIONAL, r); } return prop; }
protected static Rational average(Rational r1, Rational r2) { long d1 = r1.getDenominator(); long d2 = r2.getDenominator(); Rational f1 = new Rational(r1.getNumerator() * d2, r1.getDenominator() * d2); Rational f2 = new Rational(r2.getNumerator() * d1, r2.getDenominator() * d1); return new Rational((f1.getNumerator() + f2.getNumerator()) / 2, f1.getDenominator()); }