AsInt32() public method

public AsInt32 ( int &ret ) : bool
ret int
return bool
示例#1
0
 /// <summary>
 /// Converts a BigInteger to int if it is small enough
 /// </summary>
 /// <param name="x">The value to convert</param>
 /// <returns>An int if x is small enough, otherwise x.</returns>
 /// <remarks>
 /// Use this helper to downgrade BigIntegers as necessary.
 /// </remarks>
 public static object/*!*/ Normalize(BigInteger/*!*/ x) {
     int result;
     if (x.AsInt32(out result)) {
         return ScriptingRuntimeHelpers.Int32ToObject(result);
     }
     return x;
 }
示例#2
0
        private static Expression BigIntegerConstant(BigInteger value) {
            int ival;
            if (value.AsInt32(out ival)) {
                return Expression.Call(
                    typeof(BigInteger).GetMethod("Create", new Type[] { typeof(int) }),
                    Expression.Constant(ival)
                );
            }

            long lval;
            if (value.AsInt64(out lval)) {
                return Expression.Call(
                    typeof(BigInteger).GetMethod("Create", new Type[] { typeof(long) }),
                    Expression.Constant(lval)
                );
            }

            return Expression.New(
                typeof(BigInteger).GetConstructor(new Type[] { typeof(int), typeof(uint[]) }),
                Expression.Constant((int)value.Sign),
                CreateUIntArray(value.GetBits())
            );
        }
示例#3
0
        public static string/*!*/ __format__(CodeContext/*!*/ context, BigInteger/*!*/ self, [NotNull]string/*!*/ formatSpec) {
            StringFormatSpec spec = StringFormatSpec.FromString(formatSpec);

            if (spec.Precision != null) {
                throw PythonOps.ValueError("Precision not allowed in integer format specifier");
            }

            BigInteger val = self;
            if (self < 0) {
                val = -self;
            }
            string digits;
            
            switch (spec.Type) {
                case 'n':
                    CultureInfo culture = PythonContext.GetContext(context).NumericCulture;

                    if (culture == CultureInfo.InvariantCulture) {
                        // invariant culture maps to CPython's C culture, which doesn't
                        // include any formatting info.
                        goto case 'd';
                    }

                    digits = ToCultureString(val, PythonContext.GetContext(context).NumericCulture);
                    break;
#if !FEATURE_NUMERICS
                case null:
                case 'd':
                    digits = val.ToString();
                    break;
                case '%':
                    if (val == BigInteger.Zero) {
                        digits = "0.000000%";
                    } else {
                        digits = val.ToString() + "00.000000%";
                    }
                    break;
                case 'e': digits = ToExponent(val, true, 6, 7); break;
                case 'E': digits = ToExponent(val, false, 6, 7); break;
                case 'f':
                    if (val != BigInteger.Zero) {
                        digits = val.ToString() + ".000000";
                    } else {
                        digits = "0.000000";
                    }
                    break;
                case 'F':
                    if (val != BigInteger.Zero) {
                        digits = val.ToString() + ".000000";
                    } else {
                        digits = "0.000000";
                    }
                    break;
                case 'g':
                    if (val >= 1000000) {
                        digits = ToExponent(val, true, 0, 6);
                    } else {
                        digits = val.ToString();
                    }
                    break;
                case 'G':                    
                    if (val >= 1000000) {
                        digits = ToExponent(val, false, 0, 6);
                    } else {
                        digits = val.ToString();
                    }
                    break;
#else
                case null:
                case 'd':
                    if (spec.ThousandsComma) {
                        digits = val.ToString("#,0", CultureInfo.InvariantCulture);
                    } else {
                        digits = val.ToString("D", CultureInfo.InvariantCulture);
                    }
                    break;
                case '%':
                    if (spec.ThousandsComma) {
                        digits = val.ToString("#,0.000000%", CultureInfo.InvariantCulture);
                    } else {
                        digits = val.ToString("0.000000%", CultureInfo.InvariantCulture);
                    }
                    break;
                case 'e':
                    if (spec.ThousandsComma) {
                        digits = val.ToString("#,0.000000e+00", CultureInfo.InvariantCulture);
                    } else {
                        digits = val.ToString("0.000000e+00", CultureInfo.InvariantCulture);
                    }
                    break;
                case 'E':
                    if (spec.ThousandsComma) {
                        digits = val.ToString("#,0.000000E+00", CultureInfo.InvariantCulture);
                    } else {
                        digits = val.ToString("0.000000E+00", CultureInfo.InvariantCulture);
                    }
                    break;
                case 'f':
                case 'F':
                    if (spec.ThousandsComma) {
                        digits = val.ToString("#,########0.000000", CultureInfo.InvariantCulture);
                    } else {
                        digits = val.ToString("#########0.000000", CultureInfo.InvariantCulture);
                    }
                    break;
                case 'g':
                    if (val >= 1000000) {
                        digits = val.ToString("0.#####e+00", CultureInfo.InvariantCulture);
                    } else if (spec.ThousandsComma) {
                        digits = val.ToString("#,0", CultureInfo.InvariantCulture);
                    } else {
                        digits = val.ToString(CultureInfo.InvariantCulture);
                    }
                    break;
                case 'G':
                    if (val >= 1000000) {
                        digits = val.ToString("0.#####E+00", CultureInfo.InvariantCulture);
                    } else if (spec.ThousandsComma) {
                        digits = val.ToString("#,0", CultureInfo.InvariantCulture);
                    } else {
                        digits = val.ToString(CultureInfo.InvariantCulture);
                    }
                    break;
#endif
                case 'X':
                    digits = AbsToHex(val, false);
                    break;
                case 'x':
                    digits = AbsToHex(val, true);
                    break;
                case 'o': // octal
                    digits = ToOctal(val, true);
                    break;
                case 'b': // binary
                    digits = ToBinary(val, false, true);
                    break;
                case 'c': // single char
                    int iVal;
                    if (spec.Sign != null) {
                        throw PythonOps.ValueError("Sign not allowed with integer format specifier 'c'");
                    } else if (!self.AsInt32(out iVal)) {
                        throw PythonOps.OverflowError("long int too large to convert to int");
                    } else if(iVal < 0 || iVal > 0xFF) {
                        throw PythonOps.OverflowError("%c arg not in range(0x10000)");
                    }

                    digits = ScriptingRuntimeHelpers.CharToString((char)iVal);
                    break;
                default:
                    throw PythonOps.ValueError("Unknown format code '{0}'", spec.Type.ToString());
            }

            Debug.Assert(digits[0] != '-');

            return spec.AlignNumericText(digits, self.IsZero(), self.IsPositive());
        }
示例#4
0
 public static char ToChar(BigInteger self, IFormatProvider provider) {
     int res;
     if (self.AsInt32(out res) && res <= Char.MaxValue && res >= Char.MinValue) {
         return (char)res;
     }
     throw new OverflowException("big integer won't fit into char");
 }
示例#5
0
 public static bool AsInt32(BigInteger self, out int res) {
     return self.AsInt32(out res);
 }
示例#6
0
        public static int __hash__(BigInteger self) {
#if CLR4 // TODO: we might need our own hash code implementation. This avoids assertion failure.
            if (self == -2147483648) {
                return -2147483648;
            }
#endif

            // check if it's in the Int64 or UInt64 range, and use the built-in hashcode for that instead
            // this ensures that objects added to dictionaries as (U)Int64 can be looked up with Python longs
            Int64 i64;
            if (self.AsInt64(out i64)) {
                return Int64Ops.__hash__(i64);
            } else {
                UInt64 u64;
                if (self.AsUInt64(out u64)) {
                    return UInt64Ops.__hash__(u64);
                }
            }

            // Call the DLR's BigInteger hash function, which will return an int32 representation of
            // b if b is within the int32 range. We use that as an optimization for hashing, and 
            // assert the assumption below.
            int hash = self.GetHashCode();
#if DEBUG
            int i;
            if (self.AsInt32(out i)) {
                Debug.Assert(i == hash, String.Format("hash({0}) == {1}", i, hash));
            }
#endif
            return hash;
        }
示例#7
0
        public static int Compare(BigInteger x, int y) {
            int ix;
            if (x.AsInt32(out ix)) {                
                return ix == y ? 0 : ix > y ? 1 : -1;
            }

            return BigInteger.Compare(x, y);
        }
示例#8
0
        public static int ConvertToInt32(BigInteger self) {
            int res;
            if (self.AsInt32(out res)) return res;

            throw Converter.CannotConvertOverflow("int", self);
        }
示例#9
0
        public static object __int__(BigInteger x) {
            // The python spec says __int__  should return a long if needed, rather than overflow.
            int i32;
            if (x.AsInt32(out i32)) {
                return Microsoft.Scripting.Runtime.ScriptingRuntimeHelpers.Int32ToObject(i32);
            }

            return x;
        }
示例#10
0
        public static int __hash__(BigInteger self) {
#if CLR4 // TODO: we might need our own hash code implementation. This avoids assertion failure.
            if (self == -2147483648) {
                return -2147483648;
            }
#endif
            // Call the DLR's BigInteger hash function, which will return an int32 representation of
            // b if b is within the int32 range. We use that as an optimization for hashing, and 
            // assert the assumption below.
            int hash = self.GetHashCode();
#if DEBUG
            int i;
            if (self.AsInt32(out i)) {
                Debug.Assert(i == hash, String.Format("hash({0}) == {1}", i, hash));
            }
#endif
            return hash;
        }
示例#11
0
        public static int __hash__(BigInteger self) {
            // Call the DLR's BigInteger hash function, which will return an int32 representation of
            // b if b is within the int32 range. We use that as an optimization for hashing, and 
            // assert the assumption below.
            int hash = self.GetHashCode();
#if DEBUG
            int i;
            if (self.AsInt32(out i)) {
                Debug.Assert(i == hash, "input:" + i);
            }
#endif
            return hash;
        }
示例#12
0
 [Emitted] // ConvertToFixnumAction
 public static int ConvertBignumToFixnum(BigInteger/*!*/ bignum) {
     int fixnum;
     if (bignum.AsInt32(out fixnum)) {
         return fixnum;
     }
     throw RubyExceptions.CreateRangeError("bignum too big to convert into `long'");
 }
示例#13
0
        public static string/*!*/ __format__(CodeContext/*!*/ context, BigInteger/*!*/ self, [NotNull]string/*!*/ formatSpec) {
            StringFormatSpec spec = StringFormatSpec.FromString(formatSpec);

            if (spec.Precision != null) {
                throw PythonOps.ValueError("Precision not allowed in integer format specifier");
            }

            BigInteger val = self;
            if (self < 0) {
                val = -self;
            }
            string digits;
            
            switch (spec.Type) {
                case 'n':
                    CultureInfo culture = PythonContext.GetContext(context).NumericCulture;

                    if (culture == CultureInfo.InvariantCulture) {
                        // invariant culture maps to CPython's C culture, which doesn't
                        // include any formatting info.
                        goto case 'd';
                    }

                    digits = ToCultureString(val, PythonContext.GetContext(context).NumericCulture);
                    break;
                case null:
                case 'd':
                    digits = val.ToString();
                    break;
                case '%': digits = val.ToString() + "00.000000%"; break;
                case 'e': digits = ToExponent(val, true, 6, 7); break;
                case 'E': digits = ToExponent(val, false, 6, 7); break;
                case 'f': digits = val.ToString() + ".000000"; break;
                case 'F': digits = val.ToString() + ".000000"; break;
                case 'g':
                    if (val >= 1000000) {
                        digits = ToExponent(val, true, 0, 6);
                    } else {
                        digits = val.ToString();
                    }
                    break;
                case 'G':                    
                    if (val >= 1000000) {
                        digits = ToExponent(val, false, 0, 6);
                    } else {
                        digits = val.ToString();
                    }
                    break;
                case 'X':
                    digits = ToDigits(val, 16, false);
                    
                    if (spec.IncludeType) {
                        digits = "0X" + digits;
                    }
                    break;
                case 'x':
                    digits = ToHex(val, spec.IncludeType);

                    break;
                case 'b': // binary                    
                    digits = ToBinary(val, spec.IncludeType);
                    break;
                case 'c': // single char
                    int iVal;
                    if (spec.Sign != null) {
                        throw PythonOps.ValueError("Sign not allowed with integer format specifier 'c'");
                    } else if (!self.AsInt32(out iVal)) {
                        throw PythonOps.OverflowError("long int too large to convert to int");
                    } else if(iVal < Char.MinValue || iVal > Char.MaxValue) {
                        throw PythonOps.OverflowError("%c arg not in range(0x10000)");
                    }

                    digits = Builtin.chr(iVal);
                    break;
                case 'o': // octal                    
                    digits = ToDigits(val, 8, false);

                    if (spec.IncludeType) {
                        digits = "0o" + digits;
                    }
                    break;
                default:
                    throw PythonOps.ValueError("Unknown conversion type {0}", spec.Type.ToString());
            }

            Debug.Assert(digits[0] != '-');

            return spec.AlignNumericText(digits, self.IsZero(), self.IsPositive());
        }