public static RatNum add(RatNum x, RatNum y, int k) { IntNum x_num = x.numerator(); IntNum x_den = x.denominator(); IntNum y_num = y.numerator(); IntNum y_den = y.denominator(); if (IntNum.equals(x_den, y_den)) { return(RatNum.make(IntNum.add(x_num, y_num, k), x_den)); } return(RatNum.make(IntNum.add(IntNum.times(y_den, x_num), IntNum.times(y_num, x_den), k), IntNum.times(x_den, y_den))); }
/** Convert rational to (rounded) integer, after multiplying by 10**k. */ public static IntNum toScaledInt(RatNum r, int k) { if (k != 0) { IntNum power = IntNum.power(IntNum.ten(), k < 0 ? -k : k); IntNum num = r.numerator(); IntNum den = r.denominator(); if (k >= 0) { num = IntNum.times(num, power); } else { den = IntNum.times(den, power); } r = RatNum.make(num, den); } return(r.toExactInt(ROUND)); }
public static RatNum divide(RatNum x, RatNum y) { return(RatNum.make(IntNum.times(x.numerator(), y.denominator()), IntNum.times(x.denominator(), y.numerator()))); }