private static Expression BigIntegerConstant(BigInteger value)
        {
            int ival;

            if (value.AsInt32(out ival))
            {
                return(Expression.Call(
                           new Func <int, BigInteger>(BigInteger.Create).GetMethodInfo(),
                           Constant(ival)
                           ));
            }

            long lval;

            if (value.AsInt64(out lval))
            {
                return(Expression.Call(
                           new Func <long, BigInteger>(BigInteger.Create).GetMethodInfo(),
                           Constant(lval)
                           ));
            }

#if !FEATURE_NUMERICS
            return(Expression.Call(
                       new Func <int, uint[], BigInteger>(CompilerHelpers.CreateBigInteger).Method,
                       Constant((int)value.Sign),
                       CreateArray <uint>(value.GetWords())
                       ));
#else
            return(Expression.Call(
                       new Func <bool, byte[], BigInteger>(CompilerHelpers.CreateBigInteger).GetMethodInfo(),
                       Constant(value.Sign < 0),
                       CreateArray <byte>(value.Abs().ToByteArray())
                       ));
        }
Exemple #2
0
        private static Expression BigIntegerConstant(BigInteger value)
        {
            int ival;

            if (value.AsInt32(out ival))
            {
                return(Expression.Call(
                           new Func <int, BigInteger>(BigInteger.Create).GetMethodInfo(),
                           Constant(ival)
                           ));
            }

            long lval;

            if (value.AsInt64(out lval))
            {
                return(Expression.Call(
                           new Func <long, BigInteger>(BigInteger.Create).GetMethodInfo(),
                           Constant(lval)
                           ));
            }

            return(Expression.Call(
                       new Func <bool, byte[], BigInteger>(CompilerHelpers.CreateBigInteger).GetMethodInfo(),
                       Constant(value.Sign < 0),
                       CreateArray <byte>(value.Abs().ToByteArray())
                       ));
        }
Exemple #3
0
        public static object Power(BigInteger x, BigInteger y)
        {
            long yl;

            if (y.AsInt64(out yl))
            {
                return(Power(x, yl));
            }
            else
            {
                if (x == BigInteger.Zero)
                {
                    if (y.IsNegative())
                    {
                        throw Ops.ZeroDivisionError("0.0 cannot be raised to a negative power");
                    }
                    return(BigInteger.Zero);
                }
                else if (x == BigInteger.One)
                {
                    return(BigInteger.One);
                }
                else
                {
                    throw Ops.ValueError("Number too big");
                }
            }
        }
Exemple #4
0
        public static object Power([NotNull] BigInteger x, [NotNull] BigInteger y)
        {
            int  yl;
            long y2;

            if (y.AsInt32(out yl))
            {
                return(Power(x, yl));
            }
            else if (y.AsInt64(out y2))
            {
                return(Power(x, y2));
            }
            else
            {
                if (x == BigInteger.Zero)
                {
                    if (y.Sign < 0)
                    {
                        throw PythonOps.ZeroDivisionError("0.0 cannot be raised to a negative power");
                    }
                    return(BigInteger.Zero);
                }
                else if (x == BigInteger.One)
                {
                    return(BigInteger.One);
                }
                else
                {
                    throw PythonOps.ValueError("Number too big");
                }
            }
        }
Exemple #5
0
 public static BigInt fromBigInteger(BigInteger val)
 {
     long n;
     if (val.AsInt64(out n))
         return new BigInt(n, null);
     return new BigInt(0, val);
 }
Exemple #6
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())
                       ));
        }
Exemple #7
0
        internal static Int64 ToInt64(BigInteger value)
        {
            Int64 result;

            if (value.AsInt64(out result))
            {
                return(result);
            }
            throw RubyExceptions.CreateRangeError("number too big to convert into System::Int64");
        }
Exemple #8
0
        public static BigInt fromBigInteger(BigInteger val)
        {
            long n;

            if (val.AsInt64(out n))
            {
                return(new BigInt(n, null));
            }
            return(new BigInt(0, val));
        }
Exemple #9
0
        public static int Seek(RubyIO /*!*/ self, [NotNull] BigInteger /*!*/ pos, [DefaultProtocol, DefaultParameterValue(SEEK_SET)] int seekOrigin)
        {
            long longPos;

            if (!pos.AsInt64(out longPos))
            {
                throw RubyExceptions.CreateRangeError("bignum too big to convert into `long'");
            }
            Seek(self, longPos, seekOrigin);
            return(0);
        }
Exemple #10
0
        public static object EvalExpression(string code, Dictionary <string, object> localScope)
        {
            var scope = Prepare(localScope);

            try
            {
                ScriptSource source = scope.Engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression);
                object       obj    = source.Execute(scope);

                if (obj != null && obj.GetType() == typeof(BigInteger))
                {
                    BigInteger bint = (BigInteger)obj;

                    int   i32;
                    uint  ui32;
                    long  i64;
                    ulong ui64;

                    if (bint.AsInt32(out i32))
                    {
                        return(i32);
                    }

                    if (bint.AsInt64(out i64))
                    {
                        return(i64);
                    }

                    if (bint.AsUInt32(out ui32))
                    {
                        return(ui32);
                    }

                    if (bint.AsUInt64(out ui64))
                    {
                        return(ui64);
                    }
                }

                return(obj);
            }
            catch (Exception ex)
            {
                throw new PeachException("Error executing expression [" + code + "]: " + ex.ToString(), ex);
            }
            finally
            {
                var names = scope.GetVariableNames().ToList();
                foreach (var name in names)
                {
                    scope.RemoveVariable(name);
                }
            }
        }
Exemple #11
0
        public long ToInt64()
        {
            long result;

            if (IsFixnum)
            {
                result = _fixnum;
            }
            else if (!_bignum.AsInt64(out result))
            {
                throw RubyExceptions.CreateRangeError("Bignum too big to convert into 64-bit signed integer");
            }
            return(result);
        }
Exemple #12
0
        public static int __hash__(BigInteger self)
        {
            // TODO: we might need our own hash code implementation. This avoids assertion failure.
            if (self == -2147483648)
            {
                return(-2147483648);
            }

            // 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);
        }
Exemple #13
0
 public static bool AsInt64(BigInteger self, out long res)
 {
     return(self.AsInt64(out res));
 }
Exemple #14
0
        public static object EvalExpression(string code, Dictionary <string, object> localScope)
        {
            var missing = Imports.Except(Engine.Modules.Keys).ToList();

            foreach (string import in missing)
            {
                Engine.Modules.Add(import, Engine.Instance.ImportModule(import));
            }

            ScriptScope scope = Engine.Instance.CreateScope();

            foreach (var kv in Engine.Modules)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            foreach (var kv in GlobalScope)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            foreach (var kv in localScope)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            try
            {
                ScriptSource source = scope.Engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression);
                object       obj    = source.Execute(scope);

                if (obj != null && obj.GetType() == typeof(BigInteger))
                {
                    BigInteger bint = (BigInteger)obj;

                    int   i32;
                    uint  ui32;
                    long  i64;
                    ulong ui64;

                    if (bint.AsInt32(out i32))
                    {
                        return(i32);
                    }

                    if (bint.AsInt64(out i64))
                    {
                        return(i64);
                    }

                    if (bint.AsUInt32(out ui32))
                    {
                        return(ui32);
                    }

                    if (bint.AsUInt64(out ui64))
                    {
                        return(ui64);
                    }
                }

                return(obj);
            }
            catch (Exception ex)
            {
                throw new PeachException("Error executing expression [" + code + "]: " + ex.ToString(), ex);
            }
            finally
            {
                var names = scope.GetVariableNames().ToList();
                foreach (var name in names)
                {
                    scope.RemoveVariable(name);
                }
            }
        }
Exemple #15
0
        public static object reduce(BigInteger val)
        {
            //int bitLength = val.bitLength();
            //return (bitLength < 32)
            //    ? (object)val.intValue()
            //    : (bitLength < 64)
            //        ? (object)val.longValue()
            //        : val;
            int ival;
            if (val.AsInt32(out ival))
                return ival;

            long lval;
            if (val.AsInt64(out lval))
                return lval;

            return val;
        }
 private void AsInt64Test(BigInteger i, bool expRet, long expInt)
 {
     long v;
     bool b = i.AsInt64(out v);
     Expect(b, EqualTo(expRet));
     Expect(v, EqualTo(expInt));
 }
Exemple #17
0
        public static object MatchNumber(string s)
        {
            Match m = intRE.Match(s);

            if (m.Success)
            {
                if (m.Groups[2].Success)
                {
                    // matched 0  or 0N only
                    if (m.Groups[8].Success)
                    {
                        return(BigInt.ZERO);
                    }
                    return(0L);
                }
                bool   isNeg = m.Groups[1].Value == "-";
                string n     = null;
                int    radix = 10;
                if (m.Groups[3].Success)
                {
                    n     = m.Groups[3].Value;
                    radix = 10;
                }
                else if (m.Groups[4].Success)
                {
                    n     = m.Groups[4].Value;
                    radix = 16;
                }
                else if (m.Groups[5].Success)
                {
                    n     = m.Groups[5].Value;
                    radix = 8;
                }
                else if (m.Groups[7].Success)
                {
                    n     = m.Groups[7].Value;
                    radix = Int32.Parse(m.Groups[6].Value, System.Globalization.CultureInfo.InvariantCulture);
                }
                if (n == null)
                {
                    return(null);
                }

                BigInteger bn = BigInteger.Parse(n, radix);
                if (isNeg)
                {
                    bn = bn.Negate();
                }

                if (m.Groups[8].Success) // N suffix
                {
                    return(BigInt.fromBigInteger(bn));
                }

                long ln;
                if (bn.AsInt64(out ln))
                {
                    return(Numbers.num(ln));
                }

                return(BigInt.fromBigInteger(bn));
            }

            m = floatRE.Match(s);

            if (m.Success)
            {
                if (m.Groups[4].Success)
                {
                    string val = m.Groups[1].Value;
                    // MS implementation of java.util.BigDecimal has a bug when the string has a leading+
                    //if ( val[0] == '+' )
                    //    val = val.Substring(1);
                    return(BigDecimal.Parse(val));
                }
                return((object)Double.Parse(s, System.Globalization.CultureInfo.InvariantCulture));
            }
            m = ratioRE.Match(s);
            if (m.Success)
            {
                // There is a bug in the BigInteger c-tor that causes it barf on a leading +.
                string numerString = m.Groups[1].Value;
                string denomString = m.Groups[2].Value;
                if (numerString[0] == '+')
                {
                    numerString = numerString.Substring(1);
                }
                //return Numbers.BIDivide(new BigInteger(numerString), new BigInteger(denomString));
                return(Numbers.divide(
                           Numbers.ReduceBigInt(BigInt.fromBigInteger(BigInteger.Parse(numerString))),
                           Numbers.ReduceBigInt(BigInt.fromBigInteger(BigInteger.Parse(denomString)))));
            }
            return(null);
        }