public static bool Subtract(out object answer, object left, object right) { if (left is int) { if (right is int) { answer = Narrow((long)(int)left - (long)(int)right); } else if (right is long) { answer = Narrow((long)(int)left - (long)right); } else { throw new NotImplementedException(); } } else if (left is long) { if (right is int) { answer = Narrow((long)left - (long)(int)right); } else if (right is long) { answer = Narrow((long)left - (long)right); } else { throw new NotImplementedException(); } } else if (left is Bignum) { if (right is int) { answer = Bignum.ToInteger((Bignum)left - (int)right); } else if (right is long) { answer = Bignum.ToInteger((Bignum)left - (long)right); } else if (right is Bignum) { answer = Bignum.ToInteger((Bignum)left - (Bignum)right); } else { throw new NotImplementedException(); } } else { throw new NotImplementedException(); } return(false); }
public static bool Divide(out object answer, object left, object right) { if (left is Bignum) { if (right is int) { Bignum remainder; Bignum q = Bignum.DivRem((Bignum)left, (Bignum)(long)(int)right, out remainder); answer = new Cons(Bignum.ToInteger(q), Bignum.ToInteger(remainder)); return(false); } else if (right is long) { Bignum remainder; Bignum q = Bignum.DivRem((Bignum)left, (Bignum)(long)right, out remainder); answer = new Cons(Bignum.ToInteger(q), Bignum.ToInteger(remainder)); return(false); } if (right is Bignum) { Bignum remainder; Bignum q = Bignum.DivRem((Bignum)left, (Bignum)right, out remainder); answer = new Cons(Bignum.ToInteger(q), Bignum.ToInteger(remainder)); return(false); } else { throw new NotImplementedException("Divide bignum by non-bignum."); } } else { long w0 = Widen(left); long w1 = Widen(right); long remainder = 0; long quotient = Math.DivRem(w0, w1, out remainder); answer = new Cons(Narrow(quotient), Narrow(remainder)); return(false); } }
public static bool Multiply(out object answer, object left, object right) { if (left is int) { if (right is int) { answer = Narrow(Widen(left) * Widen(right)); } else if (right is long) { answer = Bignum.ToInteger((Bignum)(long)(int)left * (Bignum)(long)(right)); } else if (right is Bignum) { answer = Bignum.ToInteger((Bignum)(long)(int)left * (Bignum)(right)); } else { throw new NotImplementedException(); } } else if (left is long) { if (right is int) { answer = Bignum.ToInteger((Bignum)(long)(left) * (Bignum)(long)(int)(right)); } else if (right is long) { answer = Bignum.ToInteger((Bignum)(long)(left) * (Bignum)(long)(right)); } else if (right is Bignum) { answer = Bignum.ToInteger((Bignum)(long)(left) * (Bignum)(right)); } else { throw new NotImplementedException(); } } else if (left is Bignum) { if (right is int) { answer = Bignum.ToInteger((Bignum)(left) * (Bignum)(int)(right)); } else if (right is long) { answer = Bignum.ToInteger((Bignum)(left) * (Bignum)(long)(right)); } else if (right is Bignum) { answer = Bignum.ToInteger((Bignum)(left) * (Bignum)(right)); } else { throw new NotImplementedException(); } } else { throw new NotImplementedException(); } return(false); }