本文整理汇总了C#中Microsoft.Scripting.Math.BigInteger.AsInt32方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.AsInt32方法的具体用法?C# BigInteger.AsInt32怎么用?C# BigInteger.AsInt32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Scripting.Math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.AsInt32方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Normalize
/// <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: BigIntegerConstant
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: __format__
//.........这里部分代码省略.........
} 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: ToChar
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: AsInt32
public static bool AsInt32(BigInteger self, out int res) {
return self.AsInt32(out res);
}
示例6: __hash__
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: Compare
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: ConvertToInt32
public static int ConvertToInt32(BigInteger self) {
int res;
if (self.AsInt32(out res)) return res;
throw Converter.CannotConvertOverflow("int", self);
}
示例9: __int__
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: __hash__
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: __hash__
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: ConvertBignumToFixnum
[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: __format__
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());
}