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 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); }
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 Compare(decimal x, object other) { return(FloatOps.Compare((double)x, other)); }