public static object Neg(object a1) { if (a1 is int) { var d1 = (int)a1; try { checked { return(-d1); } } catch { return(-(long)d1); } } else if (a1 is Complex) { return(-AsComplex(a1)); } else if (a1 is double) { return(-AsDouble(a1)); } else if (a1 is float) { return(-AsSingle(a1)); } else if (a1 is decimal) { return(-AsDecimal(a1)); } else if (a1 is BigRational) { return(-AsBigRational(a1)); } else if (a1 is BigInteger) { return(-AsBigInteger(a1)); } else if (a1 is long) { long i1 = Convert.ToInt64(a1); try { checked { return(-i1); } } catch { return(Number.Shrink(-1 * (new BigInteger(i1)))); } } else { return(Neg(Convert.ToInt32(a1))); } }
public static object Add2(object a1, object a2) { // commonest case first if (a1 is int && a2 is int) { var i1 = (int)a1; var i2 = (int)a2; try { checked { return(i1 + i2); } } catch { return((long)i1 + (long)i2); } } else if (a1 is Complex || a2 is Complex) { return(AsComplex(a1) + AsComplex(a2)); } else if (a1 is double || a2 is double) { return(AsDouble(a1) + AsDouble(a2)); } else if (a1 is decimal || a2 is decimal) { decimal d1 = AsDecimal(a1); decimal d2 = AsDecimal(a2); try { checked { return(d1 + d2); } } catch { return((double)d1 + (double)d2); } } else if (a1 is float || a2 is float) { return(AsSingle(a1) + AsSingle(a2)); } else if (a1 is BigRational || a2 is BigRational) { return(Number.Shrink(AsBigRational(a1) + AsBigRational(a2))); } else if (a1 is BigInteger || a2 is BigInteger) { return(Number.Shrink(AsBigInteger(a1) + AsBigInteger(a2))); } else if (a1 is long || a2 is long) { long i1 = Convert.ToInt64(a1); long i2 = Convert.ToInt64(a2); try { checked { return(Number.Shrink(i1 + i2)); } } catch { return(Number.Shrink(new BigInteger(i1) + new BigInteger(i2))); } } else if (a1 is DateTime && a2 is TimeSpan) { var a = (DateTime)a1; var b = (TimeSpan)a2; return(a + b); } else if (a1 is TimeSpan && a2 is TimeSpan) { var a = (TimeSpan)a1; var b = (TimeSpan)a2; return(a + b); } else if (a1 is TimeSpan && a2 is int) { var a = (TimeSpan)a1; var b = (int)a2; return(a + new TimeSpan(b, 0, 0, 0)); } else { return(Add2(Convert.ToInt32(a1), Convert.ToInt32(a2))); } }
public static object Mul(object a1, object a2) { // commonest case first if (a1 is int && a2 is int) { var i1 = (int)a1; var i2 = (int)a2; try { checked { return(i1 * i2); } } catch { return((long)i1 * (long)i2); } } else if (a1 is Complex || a2 is Complex) { return(AsComplex(a1) * AsComplex(a2)); } else if (a1 is double || a2 is double) { return(AsDouble(a1) * AsDouble(a2)); } else if (a1 is decimal || a2 is decimal) { var d1 = AsDecimal(a1); var d2 = AsDecimal(a2); try { checked { return(d1 * d2); } } catch { return((double)d1 * (double)d2); } } else if (a1 is float || a2 is float) { return(AsSingle(a1) * AsSingle(a2)); } else if (a1 is BigRational || a2 is BigRational) { return(Number.Shrink(AsBigRational(a1) * AsBigRational(a2))); } else if (a1 is BigInteger || a2 is BigInteger) { return(Number.Shrink(AsBigInteger(a1) * AsBigInteger(a2))); } else if (a1 is long || a2 is long) { var i1 = Convert.ToInt64(a1); var i2 = Convert.ToInt64(a2); try { checked { return(i1 * i2); } } catch { return(Number.Shrink(new BigInteger(i1) * new BigInteger(i2))); } } else { return(Mul(Convert.ToInt32(a1), Convert.ToInt32(a2))); } }