Exemplo n.º 1
0
        public static bool IsPrime(dynamic a)
        {
#if !NET_STANDARD_2_0
            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);
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 2
0
        public static dynamic Pow10(dynamic a)
        {
#if !NET_STANDARD_2_0
            double resultA = DynamicCast.ConvertToDouble(a);

            return(Math.Pow(10.0, resultA));
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 3
0
        public static bool IsNegative(dynamic a)
        {
#if !NET_STANDARD_2_0
            double resultA = DynamicCast.ConvertToDouble(a);

            return(resultA < 0);
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 4
0
        public static bool IsWhole(dynamic a)
        {
#if !NET_STANDARD_2_0
            double resultA = DynamicCast.ConvertToDouble(a);

            return(Math.Abs(resultA % 1) < Tolerance);
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 5
0
        public static dynamic Atan(dynamic a)
        {
#if !NET_STANDARD_2_0
            double resultA = DynamicCast.ConvertToDouble(a);

            return(Math.Atan(resultA) / Math.PI * 180);
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 6
0
        public static string Substring(dynamic text, string where1, dynamic at1, string where2, dynamic at2)
        {
#if !NET_STANDARD_2_0
            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("");
            }
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 7
0
        public static dynamic Sum(dynamic a, dynamic b)
        {
#if !NET_STANDARD_2_0
            double resultA = DynamicCast.ConvertToDouble(a);
            double resultB = DynamicCast.ConvertToDouble(b);

            return(resultA + resultB);
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 8
0
        public static dynamic RandomInt(dynamic a, dynamic b)
        {
#if !NET_STANDARD_2_0
            int resultA = DynamicCast.ConvertToInt(a);
            int resultB = DynamicCast.ConvertToInt(b);

            return(resultA < resultB?Utils.RandomInt(resultA, resultB) : Utils.RandomInt(resultB, resultA));
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 9
0
        public static dynamic RoundDown(dynamic a)
        {
#if !NET_STANDARD_2_0
            double resultA = DynamicCast.ConvertToDouble(a);
            resultA -= 0.5d;

            return(Math.Round(resultA));
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 10
0
        public static bool DivisionBy(dynamic a, dynamic divisionBy)
        {
#if !NET_STANDARD_2_0
            double resultA          = DynamicCast.ConvertToDouble(a);
            double resultDivisionBy = DynamicCast.ConvertToDouble(divisionBy);

            return(Math.Abs(resultA % resultDivisionBy) < Tolerance);
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 11
0
        public static char GetLetterFromEnd(dynamic text, dynamic index)
        {
#if !NET_STANDARD_2_0
            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');
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }
Exemplo n.º 12
0
        public static dynamic Division(dynamic a, dynamic b)
        {
#if !NET_STANDARD_2_0
            double resultA = DynamicCast.ConvertToDouble(a);
            double resultB = DynamicCast.ConvertToDouble(b);

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

            return(resultA / resultB);
#else
            throw new WrongApiCompatibilityLevelException();
#endif
        }