public static object LeftShift(int x, object other) { if (other is int) { return(LeftShift(x, (int)other)); } else if (other is long) { return(Int64Ops.LeftShift((long)x, other)); } else if (other is BigInteger) { return(LongOps.LeftShift(BigInteger.Create(x), other)); } else if (other is bool) { return(LeftShift(x, (bool)other ? 1 : 0)); } else if (other is ExtensibleInt) { return(LeftShift(x, ((ExtensibleInt)other).value)); } else if (other is byte) { return(LeftShift(x, (byte)other)); } return(Ops.NotImplemented); }
public static object Negate(int x) { try { return(checked (-x)); } catch (OverflowException) { return(LongOps.Negate(x)); } }
public static object Compare(long x, object other) { if (other is int) { return(CompareWorker(x, (long)(int)other)); } else if (other is long) { return(CompareWorker(x, (long)other)); } else if (other is double) { return(CompareWorker(x, (double)other)); } else if (other is BigInteger) { return(-(int)LongOps.Compare((BigInteger)other, x)); } else if (other is Complex64) { return(ComplexOps.TrueCompare(new Complex64((double)x, 0), (Complex64)other)); } else if (other is float) { return(CompareWorker(x, (double)(float)other)); } else if (other is bool) { return(CompareWorker(x, ((bool)other) ? 1L : 0L)); } else if (other is decimal) { return(CompareWorker(x, (decimal)other)); } else if (other is ulong) { return(CompareWorker(x, (ulong)other)); } Conversion conversion; double y = Converter.TryConvertToInt64(other, out conversion); if (conversion != Conversion.None) { CompareWorker(x, y); } return(Ops.NotImplemented); }
public static object PowerMod(int x, object y, object z) { if (y is int) { return(PowerMod(x, (int)y, z)); } else if (y is long) { return(Int64Ops.PowerMod(x, y, z)); } else if (y is BigInteger) { return(LongOps.PowerMod(x, y, z)); } return(Ops.NotImplemented); }
public static object PowerMod(int x, int y, object z) { if (z is int) { return(PowerMod(x, y, (int)z)); } else if (z is long) { return(Int64Ops.PowerMod(x, y, (long)z)); } else if (z is BigInteger) { return(LongOps.PowerMod(BigInteger.Create(x), BigInteger.Create(y), (BigInteger)z)); } return(Ops.NotImplemented); }
internal static object Power(int x, int power) { if (power == 0) { return(1); } if (power < 0) { if (x == 0) { throw Ops.ZeroDivisionError("0.0 cannot be raised to a negative power"); } return(FloatOps.Power(x, power)); } int factor = x; int result = 1; int savePower = power; try { checked { while (power != 0) //??? this loop has redundant checks for exit condition { if ((power & 1) != 0) { result = result * factor; } if (power == 1) { break; } factor = factor * factor; power >>= 1; } return(result); } } catch (OverflowException) { return(LongOps.Power(BigInteger.Create(x), savePower)); } }
public static object Power(long x, long exp) { if (exp == 0) { return(1); } if (exp == 1) { return(x); } if (exp < 0) { return(FloatOps.Power(x, exp)); } long saveexp = exp; long result = 1; long factor = x; try { checked { while (exp != 0) { if ((exp & 1) != 0) { result = result * factor; } if (exp == 1) { break; // save possible overflow in the multiply } factor = factor * factor; exp >>= 1; } return(result); } } catch (OverflowException) { return(LongOps.Power(BigInteger.Create(x), saveexp)); } }
public static object RightShift(int x, object other) { if (other is int) { return(RightShift(x, (int)other)); } else if (other is BigInteger) { return(LongOps.RightShift(BigInteger.Create(x), other)); } else if (other is bool) { return(RightShift(x, (bool)other ? 1 : 0)); } else if (other is long) { long y = (long)other; if (y < 0) { throw Ops.ValueError("negative shift count"); } if (y >= Int32.MaxValue) { return(x > 0 ? 0 : 1); } return(RightShift(x, (int)y)); } else if (other is ExtensibleInt) { return(RightShift(x, ((ExtensibleInt)other).value)); } else if (other is byte) { return(RightShift(x, (byte)other)); } return(Ops.NotImplemented); }
public new object CompareTo(object other) { return(LongOps.Compare(this, other)); }
public static object Mod(long x, object other) { if (other is int) { int y = (int)other; try { return(Ops.Long2Object(Mod(x, y))); } catch (OverflowException) { return(BigInteger.Create(x) % y); } } else if (other is BigInteger) { return(LongOps.Mod(BigInteger.Create(x), (BigInteger)other)); } else if (other is double) { return(FloatOps.Mod(x, (double)other)); } else if (other is Complex64) { return(ComplexOps.Mod(Complex64.MakeReal(x), (Complex64)other)); } else if (other is bool) { int y = (bool)other ? 1 : 0; try { return(Ops.Long2Object(Mod(x, y))); } catch (OverflowException) { return(BigInteger.Create(x) % y); } } else if (other is long) { long y = (long)other; try { return(Mod(x, y)); } catch (OverflowException) { return(BigInteger.Create(x) % y); } } else if (other is float) { return(FloatOps.Mod(x, (float)other)); } else if (other is ExtensibleInt) { int y = ((ExtensibleInt)other).value; try { return(Ops.Long2Object(Mod(x, y))); } catch (OverflowException) { return(BigInteger.Create(x) % y); } } else if (other is ExtensibleFloat) { return(FloatOps.Mod(x, ((ExtensibleFloat)other).value)); } else if (other is ExtensibleComplex) { return(ComplexOps.Mod(Complex64.MakeReal(x), ((ExtensibleComplex)other).value)); } else if (other is byte) { int y = (int)((byte)other); try { return(Ops.Long2Object(Mod(x, y))); } catch (OverflowException) { return(BigInteger.Create(x) % y); } } return(Ops.NotImplemented); }
private static object Power(long x, BigInteger y) { return(LongOps.Power(BigInteger.Create(x), y)); }
public static object Mod(int x, object other) { if (other is int) { int y = (int)other; try { return(Ops.Int2Object(Mod(x, y))); } catch (OverflowException) { return(LongOps.Mod(BigInteger.Create(x), y)); } } else if (other is BigInteger) { return(LongOps.Mod(BigInteger.Create(x), (BigInteger)other)); } else if (other is double) { return(FloatOps.Mod(x, (double)other)); } else if (other is Complex64) { Complex64 y = (Complex64)other; if (y.IsZero) { throw Ops.ZeroDivisionError(); } return(ComplexOps.Mod(Complex64.MakeReal(x), y)); } else if (other is bool) { bool b = (bool)other; return(x % (b ? 1 : 0)); } else if (other is long) { long y = (long)other; try { return(Mod(x, y)); } catch (OverflowException) { return(LongOps.Mod(BigInteger.Create(x), y)); } } else if (other is float) { return(FloatOps.Mod(x, (float)other)); } else if (other is byte) { return(Ops.Int2Object(Mod(x, (int)((byte)other)))); } else if (other is ExtensibleInt) { int y = ((ExtensibleInt)other).value; try { return(Ops.Int2Object(Mod(x, y))); } catch (OverflowException) { return(LongOps.Mod(BigInteger.Create(x), y)); } } else if (other is ExtensibleFloat) { return(FloatOps.Mod(x, ((ExtensibleFloat)other).value)); } else if (other is ExtensibleComplex) { Complex64 y = ((ExtensibleComplex)other).value; if (y.IsZero) { throw Ops.ZeroDivisionError(); } return(ComplexOps.Mod(Complex64.MakeReal(x), y)); } return(Ops.NotImplemented); }