protected static LispNumber Add(LispInteger a, LispInteger b) { try { var sum = checked(a.Value + b.Value); return MakeNumber(sum); } catch (OverflowException) { return new LispBigInteger((BigInteger)a.Value + b.Value); } }
public override LispObject Apply(Cons argumentsValues, LispEnvironment env) { LispNumber sum = new LispInteger(0); foreach (var addend in argumentsValues) { var num = addend.As<LispNumber>(); sum = sum.Add(num); } return sum; }
protected static LispNumber Multiply(LispInteger a, LispInteger b) { try { int product = checked(a.Value * b.Value); return MakeNumber(product); } catch (OverflowException) { BigInteger product = (BigInteger)a.Value * b.Value; return MakeNumber(product); } }
protected static LispNumber Divide(LispInteger dividend, LispInteger divisor) { try { checked { int remainder; int quotient = Math.DivRem(dividend.Value, divisor.Value, out remainder); if (remainder != 0) { return Divide((LispDouble)dividend, (LispDouble)divisor); } return MakeNumber(quotient); } } catch (OverflowException) { return Divide((LispBigInteger)dividend, (LispBigInteger)divisor); } }
public override bool NumberEquals(LispInteger number) { return Value == number.Value; }
public abstract LispNumber Multiply(LispInteger factor);
public abstract LispNumber DivideBy(LispInteger divisor);
public abstract LispNumber Add(LispInteger addend);
public override LispNumber Add(LispInteger addend) { return new LispBigInteger(addend).Add(this); }
public LispBigInteger(LispInteger value) { this.Value = value.Value; }
public override bool NumberEquals(LispInteger number) { return NumberEquals(number, this); }
public override LispNumber Multiply(LispInteger factor) { return Multiply(this, (LispBigInteger)factor); }
public override int CompareTo(LispInteger number) { return Value.CompareTo(number.Value); }
public override LispNumber Add(LispInteger addend) { return Add(this, addend); }
protected static bool NumberEquals(LispInteger a, LispBigInteger b) { return (BigInteger)a.Value == b.Value; }
protected static bool NumberEquals(LispInteger a, LispDouble b) { return a.Value == b.Value; }
public override int CompareTo(LispInteger number) { return -Compare(number, this); }
public abstract int CompareTo(LispInteger number);
public override LispNumber DivideBy(LispInteger divisor) { return Divide(this, (LispBigInteger)divisor); }
public abstract LispNumber DivideInto(LispInteger dividend);
public override LispNumber DivideInto(LispInteger dividend) { return Divide((LispBigInteger)dividend, this); }
public abstract bool NumberEquals(LispInteger number);
protected static int Compare(LispInteger a, LispDouble b) { return ((double)a.Value).CompareTo(b.Value); }
protected static int Compare(LispInteger a, LispBigInteger b) { return ((BigInteger)a.Value).CompareTo(b.Value); }
public LispDouble(LispInteger integer) { this.Value = integer.Value; }