/** Calcaulte the simplest rational between two reals. */ public static RealNum rationalize(RealNum x, RealNum y) { // This algorithm is by Alan Bawden. It has been transcribed // with permission from C-Gambit, copyright Marc Feeley. if (x.grt (y)) return simplest_rational2 (y, x); else if (! (y.grt(x))) return x; else if (x.sign() > 0) return simplest_rational2 (x, y); else if (y.isNegative ()) return (RealNum) (simplest_rational2 ((RealNum)y.neg(), (RealNum)x.neg())).neg(); else return IntNum.zero (); }
public RealNum max(RealNum x) { RealNum result = grt(x) ? this : x; return(result); }
private static RealNum simplest_rational2(RealNum x, RealNum y) { RealNum fx = x.toInt (FLOOR); RealNum fy = y.toInt (FLOOR); if (! x.grt(fx)) return fx; else if (fx.Equals(fy)) { RealNum n = (RealNum) IntNum.one().div(y.sub(fy)); RealNum d = (RealNum) IntNum.one().div(x.sub(fx)); return (RealNum) fx.add(IntNum.one().div(simplest_rational2 (n, d)), 1); } else return (RealNum) fx.add(IntNum.one(), 1); }
public static RealNum add(RealNum x, RealNum y, int k) { return (RealNum)(x.add(y, k)); }
public RealNum min(RealNum x) { RealNum result = grt (x) ? x : this; return result; }
public RealNum max(RealNum x) { RealNum result = grt (x) ? this : x; return result; }
public static RealNum times(RealNum x, RealNum y) { return (RealNum)(x.mul(y)); }
public static RealNum divide(RealNum x, RealNum y) { return (RealNum)(x.div(y)); }