Exemplo n.º 1
0
        public static int LinyeeOStr2d(CharPtr s, out LinyeeNumberType result)
        {
            CharPtr endptr;

            result = ly_str2number(s, out endptr);
            if (endptr == s)
            {
                return(0);                            /* conversion failed */
            }
            if (endptr[0] == 'x' || endptr[0] == 'X') /* maybe an hexadecimal constant? */
            {
                result = CastNum(strtoul(s, out endptr, 16));
            }
            if (endptr[0] == '\0')
            {
                return(1);                          /* most common case */
            }
            while (isspace(endptr[0]))
            {
                endptr = endptr.next();
            }
            if (endptr[0] != '\0')
            {
                return(0);                          /* invalid trailing characters? */
            }
            return(1);
        }
Exemplo n.º 2
0
        public static int LinyeeKNumberK(FuncState fs, LinyeeNumberType r)
        {
            TValue o = new LinyeeTypeValue();

            SetNValue(o, r);
            return(AddK(fs, o, o));
        }
Exemplo n.º 3
0
        private static int MathRandom(LinyeeState L)
        {
            /* the `%' avoids the (rare) case of r==1, and is needed also because on
             *     some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
            //LinyeeNumberType r = (LinyeeNumberType)(rng.Next()%RAND_MAX) / (LinyeeNumberType)RAND_MAX;
            LinyeeNumberType r = (LinyeeNumberType)rng.NextDouble();

            switch (LinyeeGetTop(L))    /* check number of arguments */
            {
            case 0: {                   /* no arguments */
                LinyeePushNumber(L, r); /* Number between 0 and 1 */
                break;
            }

            case 1: {              /* only upper limit */
                int u = LinyeeLCheckInt(L, 1);
                LinyeeLArgCheck(L, 1 <= u, 1, "interval is empty");
                LinyeePushNumber(L, Math.Floor(r * u) + 1);        /* int between 1 and `u' */
                break;
            }

            case 2: {              /* lower and upper limits */
                int l = LinyeeLCheckInt(L, 1);
                int u = LinyeeLCheckInt(L, 2);
                LinyeeLArgCheck(L, l <= u, 2, "interval is empty");
                LinyeePushNumber(L, Math.Floor(r * (u - l + 1)) + l);            /* int between `l' and `u' */
                break;
            }

            default: return(LinyeeLError(L, "wrong number of arguments"));
            }
            return(1);
        }
Exemplo n.º 4
0
        public static LinyeeNumberType LinyeeLCheckNumber(LinyeeState L, int narg)
        {
            LinyeeNumberType d = LinyeeToNumber(L, narg);

            if ((d == 0) && (LinyeeIsNumber(L, narg) == 0))      /* avoid extra test when d is not 0 */
            {
                TagError(L, narg, LINYEE_TNUMBER);
            }
            return(d);
        }
Exemplo n.º 5
0
        private static int MathRandomSeed(LinyeeState L)
        {
            // math.randomseed() can take a double number but Random expects an integer seed.
            // we use modulus to bring back the double to the allowed integer interval.
            LinyeeNumberType seed = Math.Abs(LinyeeLCheckNumber(L, 1));
            LinyeeNumberType max  = (LinyeeNumberType)int.MaxValue;

            while (seed > max)
            {
                seed = fmod(seed, max);
            }

            rng = new Random((int)seed);
            return(0);
        }
Exemplo n.º 6
0
        private static int MathMax(LinyeeState L)
        {
            int n = LinyeeGetTop(L);        /* number of arguments */
            LinyeeNumberType dmax = LinyeeLCheckNumber(L, 1);
            int i;

            for (i = 2; i <= n; i++)
            {
                LinyeeNumberType d = LinyeeLCheckNumber(L, i);
                if (d > dmax)
                {
                    dmax = d;
                }
            }
            LinyeePushNumber(L, dmax);
            return(1);
        }
Exemplo n.º 7
0
 public static LinyeeNumberType LinyeeLOptNumber(LinyeeState L, int narg, LinyeeNumberType def)
 {
     return(LinyeeLOpt(L, LinyeeLCheckNumber, narg, def));
 }
Exemplo n.º 8
0
 public static LinyeeIntegerType LinyeeLOptInteger(LinyeeState L, LinyeeLOptDelegateInteger f, int n, LinyeeNumberType d)
 {
     return((LinyeeIntegerType)(LinyeeIsNoneOrNil(L, n) ? d : f(L, (n))));
 }
Exemplo n.º 9
0
 private static void DumpNumber(LinyeeNumberType x, DumpState D)
 {
     DumpVar(x, D);
 }
Exemplo n.º 10
0
 internal static void SetNValue(Linyee.LinyeeTypeValue obj, LinyeeNumberType x)
 {
     obj.value.n = x;
     obj.tt      = LINYEE_TNUMBER;
 }
Exemplo n.º 11
0
 public static bool LinyeeIsNoneOrNil(LinyeeState L, LinyeeNumberType n)
 {
     return(LinyeeType(L, (int)n) <= 0);
 }