Exemplo n.º 1
0
        public static dynamic RandomInt(dynamic a, dynamic b)
        {
            int resultA = DynamicCast.ConvertToInt(a);
            int resultB = DynamicCast.ConvertToInt(b);

            return(resultA < resultB?Utils.RandomInt(resultA, resultB) : Utils.RandomInt(resultB, resultA));
        }
Exemplo n.º 2
0
        public static bool IsPrime(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            // http://en.wikipedia.org/wiki/Primality_test#Naive_methods
            if (Math.Abs(resultA - 2.0) < Tolerance || Math.Abs(resultA - 3.0) < Tolerance)
            {
                return(true);
            }

            // False if n is NaN, negative, is 1, or not whole. And false if n is divisible by 2 or 3.
            if (
                double.IsNaN(resultA) || resultA <= 1 || Math.Abs(resultA % 1) > Tolerance || Math.Abs(resultA % 2) < Tolerance || Math.Abs(resultA % 3) < Tolerance)
            {
                return(false);
            }

            // Check all the numbers of form 6k +/- 1, up to sqrt(n).
            for (var x = 6; x <= Math.Sqrt(resultA) + 1; x += 6)
            {
                if (Math.Abs(resultA % (x - 1)) < Tolerance || Math.Abs(resultA % (x + 1)) < Tolerance)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        public static dynamic Pow(dynamic a, dynamic b)
        {
            double resultA = DynamicCast.ConvertToDouble(a);
            double resultB = DynamicCast.ConvertToDouble(b);

            return(Math.Pow(resultA, resultB));
        }
Exemplo n.º 4
0
        public static dynamic Sum(dynamic a, dynamic b)
        {
            double resultA = DynamicCast.ConvertToDouble(a);
            double resultB = DynamicCast.ConvertToDouble(b);

            return(resultA + resultB);
        }
Exemplo n.º 5
0
        public static bool DivisionBy(dynamic a, dynamic divisionBy)
        {
            double resultA          = DynamicCast.ConvertToDouble(a);
            double resultDivisionBy = DynamicCast.ConvertToDouble(divisionBy);

            return(Math.Abs(resultA % resultDivisionBy) < Tolerance);
        }
Exemplo n.º 6
0
        public static dynamic RoundDown(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            resultA -= 0.5d;

            return(Math.Round(resultA));
        }
Exemplo n.º 7
0
        public static char GetLetterFromEnd(dynamic text, dynamic index)
        {
            string resultText  = ConvertToString(text);
            int    resultIndex = DynamicCast.ConvertToInt(index);

            if (string.IsNullOrEmpty(resultText) || resultIndex == 0)
            {
                return('\0');
            }

            return(resultText.Length - resultIndex > 0 ? resultText[resultText.Length - resultIndex] : '\0');
        }
Exemplo n.º 8
0
        public static dynamic Division(dynamic a, dynamic b)
        {
            double resultA = DynamicCast.ConvertToDouble(a);
            double resultB = DynamicCast.ConvertToDouble(b);

            if (Math.Abs(resultB) < Tolerance)
            {
                return(double.PositiveInfinity);
            }

            return(resultA / resultB);
        }
Exemplo n.º 9
0
        public static string Substring(dynamic text, string where1, dynamic at1, string where2, dynamic at2)
        {
            string resultText = ConvertToString(text);
            int    at1Result  = DynamicCast.ConvertToInt(at1);
            int    at2Result  = DynamicCast.ConvertToInt(at2);

            var getAt = new Func <dynamic, int, int>((where, at) =>
            {
                switch (@where)
                {
                case "FROM_START":
                    at--;

                    break;

                case "FROM_END":
                    at = resultText.Length - at;

                    break;

                case "FIRST":
                    at = 0;

                    break;

                case "LAST":
                    at = resultText.Length - 1;

                    break;

                default:

                    throw new ApplicationException("Unhandled option (GetSubstring).");
                }

                return(at);
            });

            at1 = getAt(where1, at1Result);
            at2 = getAt(where2, at2Result) + 1;

            try
            {
                return(resultText.Substring(at1, at2 - at1));
            }
            catch (Exception e)
            {
                Log($"Can not get Substring({at1}, {at2 - at1}); for {resultText}.");

                return("");
            }
        }
Exemplo n.º 10
0
        public static dynamic Log10(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            return(Math.Log10(resultA));
        }
Exemplo n.º 11
0
        public static dynamic Tan(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            return(Math.Tan(resultA / 180 * Math.PI));
        }
Exemplo n.º 12
0
        public static dynamic Negative(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            return(-resultA);
        }
Exemplo n.º 13
0
        public static dynamic Pow10(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            return(Math.Pow(10.0, resultA));
        }
Exemplo n.º 14
0
        public static dynamic Sqrt(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            return(Math.Sqrt(resultA));
        }
Exemplo n.º 15
0
        public static dynamic Atan(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            return(Math.Atan(resultA) / Math.PI * 180);
        }
Exemplo n.º 16
0
        public static bool IsWhole(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            return(Math.Abs(resultA % 1) < Tolerance);
        }
Exemplo n.º 17
0
        public static bool IsNegative(dynamic a)
        {
            double resultA = DynamicCast.ConvertToDouble(a);

            return(resultA < 0);
        }