public static object Power(BigInteger x, long y) { if ((int)y == y) { return(Power(x, (int)y)); } else { if (y < 0) { return(FloatOps.Power(x.ToFloat64(), y)); } if (x == BigInteger.Zero) { return(BigInteger.Zero); } else if (x == BigInteger.One) { return(BigInteger.One); } else { throw Ops.ValueError("Number too big"); } } }
private static object FastNew(object o) { BigInteger bigInt; if (o is string) { return(Make(null, (string)o, 10)); } if (o is double) { return(FloatOps.ToInteger((double)o)); } if (o is int || o is long) { return(o); } if (o.GetType() == typeof(BigInteger)) { return(o); } else if (!Object.ReferenceEquals((bigInt = o as BigInteger), null)) { return(new BigInteger(bigInt)); } if (o is Complex64) { throw Ops.TypeError("can't convert complex to int; use int(abs(z))"); } return(Converter.ConvertToInt32(o)); }
public static object Power(BigInteger x, int y) { if (y < 0) { return(FloatOps.Power(x.ToFloat64(), y)); } return(x.Power(y)); }
public static object LessThanEqual(decimal x, object other) { object res = FloatOps.Compare((double)x, other); if (res != Ops.NotImplemented) { return(Ops.Bool2Object((int)res <= 0)); } return(res); }
public static object ReverseMod(BigInteger x, object other) { if (other is int) { return(IntOps.Mod((int)other, x)); } if (other is Complex64) { Complex64 y = (Complex64)other; if (y.IsZero) { throw Ops.ZeroDivisionError(); } return(ComplexOps.Mod(y, Complex64.MakeReal(x))); } if (other is double) { return(FloatOps.Mod((double)other, x)); } if (other is bool) { return(Mod((bool)other ? 1 : 0, x)); } if (other is long) { return(Mod((long)other, x)); } if (other is BigInteger) { return(Mod((BigInteger)other, x)); } if (other is ExtensibleInt) { return(Mod(((ExtensibleInt)other).value, x)); } if (other is ExtensibleComplex) { Complex64 y = ((ExtensibleComplex)other).value; if (y.IsZero) { throw Ops.ZeroDivisionError(); } return(ComplexOps.Mod(y, Complex64.MakeReal(x))); } if (other is byte) { return(IntOps.Mod((int)((byte)other), x)); } if (other is ExtensibleFloat) { return(FloatOps.Mod(((ExtensibleFloat)other).value, x)); } return(Ops.NotImplemented); }
public static object FloorDivide(BigInteger x, object other) { if (other is int) { return(Divide(x, (int)other)); } if (other is Complex64) { Complex64 y = (Complex64)other; if (y.IsZero) { throw Ops.ZeroDivisionError(); } return(ComplexOps.FloorDivide(Complex64.MakeReal(x), y)); } if (other is double) { return(FloatOps.FloorDivide(x, (double)other)); } if (other is bool) { return(Divide(x, (bool)other ? 1 : 0)); } if (other is long) { return(Divide(x, (long)other)); } if (other is BigInteger) { return(Divide(x, (BigInteger)other)); } if (other is ExtensibleInt) { return(Divide(x, ((ExtensibleInt)other).value)); } if (other is ExtensibleComplex) { Complex64 y = ((ExtensibleComplex)other).value; if (y.IsZero) { throw Ops.ZeroDivisionError(); } return(ComplexOps.FloorDivide(Complex64.MakeReal(x), y)); } if (other is byte) { return(Divide(x, (int)((byte)other))); } if (other is ExtensibleFloat) { return(FloatOps.FloorDivide(x, ((ExtensibleFloat)other).value)); } return(Ops.NotImplemented); }
public static object Compare(int self, object obj) { if (obj == null) { return(1); } int otherInt; if (obj is int) { otherInt = (int)obj; } else if (obj is ExtensibleInt) { otherInt = ((ExtensibleInt)obj).value; } else if (obj is bool) { otherInt = ((bool)obj) ? 1 : 0; } else if (obj is double) { // compare as double to avoid truncation issues return(FloatOps.Compare((double)self, (double)obj)); } else if (obj is ExtensibleFloat) { // compare as double to avoid truncation issues return(FloatOps.Compare((double)self, ((ExtensibleFloat)obj).value)); } else if (obj is Decimal) { return(FloatOps.Compare((double)self, (double)(decimal)obj)); } else { Conversion conv; otherInt = Converter.TryConvertToInt32(obj, out conv); if (conv == Conversion.None) { object res = Ops.GetDynamicType(obj).Coerce(obj, self); if (res != Ops.NotImplemented && !(res is OldInstance)) { return(Ops.Compare(((Tuple)res)[1], ((Tuple)res)[0])); } return(Ops.NotImplemented); } } return(self == otherInt ? 0 : (self < otherInt ? -1 : +1)); }
public object CompareTo(object other) { if (other == null) { return(1); } double otherDbl; Conversion conv; if (other is float) { return(FloatOps.Compare(value, (int)other)); } else if (other is ExtensibleFloat) { return(FloatOps.Compare(value, ((ExtensibleFloat)other).value)); } else if (other is bool) { return(FloatOps.Compare(value, ((bool)other) ? 1 : 0)); } else if (other is int) { return(FloatOps.Compare(value, (double)((int)other))); } else if (other is ExtensibleInt) { return(FloatOps.Compare(value, (double)((ExtensibleInt)other).value)); } else { otherDbl = Converter.TryConvertToDouble(other, out conv); if (conv != Conversion.None) { return(FloatOps.Compare(value, otherDbl)); } } 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 Power(BigInteger x, double y) { return(FloatOps.Power(x.ToFloat64(), y)); }
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); }
public static object Compare(decimal x, object other) { return(FloatOps.Compare((double)x, other)); }
private static object Power(long x, double y) { return(FloatOps.Power(x, y)); }
public static object Compare(BigInteger x, object y) { if (y == null) { return(1); } int intVal; if (y is int) { if (x.AsInt32(out intVal)) { return(IntOps.Compare(intVal, y)); } } else if (y is ExtensibleInt) { if (x.AsInt32(out intVal)) { return(IntOps.Compare(intVal, ((ExtensibleInt)y).value)); } } else if (y is double) { double dbl = x.ToFloat64(); return(FloatOps.Compare(dbl, y)); } else if (y is ExtensibleFloat) { double dbl = x.ToFloat64(); return(FloatOps.Compare(dbl, ((ExtensibleFloat)y).value)); } else if (y is bool) { if (x.AsInt32(out intVal)) { return(IntOps.Compare(intVal, ((bool)y) ? 1 : 0)); } } else if (y is decimal) { double dbl = x.ToFloat64(); return(FloatOps.Compare(dbl, y)); } Conversion conv; BigInteger bi = Converter.TryConvertToBigInteger(y, out conv); if (conv == Conversion.None) { object res = Ops.GetDynamicType(y).Coerce(y, x); if (res != Ops.NotImplemented && !(res is OldInstance)) { return(Ops.Compare(((Tuple)res)[1], ((Tuple)res)[0])); } return(Ops.NotImplemented); } BigInteger diff = x - bi; if (diff == 0) { return(0); } else if (diff < 0) { return(-1); } else { return(1); } }
public static object Power(int x, object other) { if (other is int) { return(Power(x, (int)other)); } else if (other is BigInteger) { BigInteger lexp = (BigInteger)other; int iexp; if (lexp.AsInt32(out iexp)) { return(Power(x, iexp)); } else { if (x == 0) { return(0); } if (x == 1) { return(1); } throw Ops.ValueError("number too big"); } } else if (other is long) { long lexp = (long)other; int iexp = (int)lexp; if (lexp == iexp) { return(Power(x, iexp)); } else { if (x == 0) { return(0); } if (x == 1) { return(1); } throw Ops.ValueError("Number too big"); } } else if (other is double) { return(FloatOps.Power(x, (double)other)); } else if (other is bool) { return(Power(x, (bool)other ? 1 : 0)); } else if (other is float) { return(FloatOps.Power(x, (float)other)); } else if (other is Complex64) { return(ComplexOps.Power(x, other)); } else if (other is byte) { return(Power(x, (int)(byte)other)); } else if (other is ExtensibleInt) { return(Power(x, ((ExtensibleInt)other).value)); } else if (other is ExtensibleFloat) { return(FloatOps.Power(x, ((ExtensibleFloat)other).value)); } else if (other is ExtensibleComplex) { return(ComplexOps.Power(x, ((ExtensibleComplex)other).value)); } return(Ops.NotImplemented); }
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); }