Example #1
0
        public void PowNegativeDecimal()
        {
            const decimal expected = 0.5380018430410047863745228634M;
            decimal       actual   = DecimalMath.Pow(1.2M, -3.4M);

            Assert.AreEqual <decimal>(expected, actual);
        }
Example #2
0
        public void FloatingPower(decimal bas, decimal exp, decimal expect, decimal error)
        {
            var actual      = DecimalMath.Pow(bas, exp);
            var actualError = Math.Abs(expect - actual);

            Assert.True(error >= actualError, $"Error of {actualError}, expected no more than error of {error}");
        }
Example #3
0
        public void PowDecimal()
        {
            const decimal expected = 1.8587296919794811670420219951M;
            decimal       actual   = DecimalMath.Pow(1.2M, 3.4M);

            Assert.AreEqual <decimal>(expected, actual);
        }
Example #4
0
        public void PowNegativeInt()
        {
            const decimal expected = 0.012345679012345679012345679M;
            decimal       actual   = 9.0M;

            actual = DecimalMath.Pow(actual, -2);
            Assert.AreEqual <decimal>(expected, actual);
        }
Example #5
0
        public void PowInt()
        {
            const decimal expected = 81.0M;
            decimal       actual   = 9.0M;

            actual = DecimalMath.Pow(actual, 2);
            Assert.AreEqual <decimal>(expected, actual);
        }
Example #6
0
        /// <summary>
        /// Berechnung des Abschreibungs- und Restbuchwertes (<see cref="CalculationResult"/>) für das Abschreibungsjahr (<paramref name="period"/>).
        /// </summary>
        /// <param name="data">Die grundlegenden Daten für die Abschreibungsberechnung</param>
        /// <param name="period">Das Abschreibungsjahr für das die Daten errechnet werden sollen.</param>
        /// <returns>Die errechneten Abschreibungs- und Restbuchwerte</returns>
        public CalculationResult CalculateDepreciation(CalculationData data, int period)
        {
            if (period < 1 || period > data.DepreciationRange)
            {
                throw new ArgumentOutOfRangeException(nameof(period), "The period must be greater than 0 and less than the value of depreciationRange.");
            }

            var factor = CalculateFactor(data);

            var factorForOldValue = DecimalMath.Pow(factor, period - 1);
            var oldValue          = data.AcquisitionValue * factorForOldValue;
            var remainingValue    = factor * oldValue;
            var depreciation      = oldValue - remainingValue;

            return(new CalculationResult(period, depreciation, remainingValue));
        }
Example #7
0
        /// <summary>
        /// Berechnung des Abschreibungs- und Restbuchwertes (<see cref="CalculationResult"/>) für das Abschreibungsjahr (<paramref name="period"/>).
        /// </summary>
        /// <param name="data">Die grundlegenden Daten für die Abschreibungsberechnung</param>
        /// <param name="period">Das Abschreibungsjahr für das die Daten errechnet werden sollen.</param>
        /// <returns>Die errechneten Abschreibungs- und Restbuchwerte</returns>
        public CalculationResult CalculateDepreciation(CalculationData data, int period)
        {
            if (period < 1 || period > data.DepreciationRange)
            {
                throw new ArgumentOutOfRangeException(nameof(period), "The period must be greater than 0 and less than the value of depreciationRange.");
            }

            var factor = CalculateFactor(data);

            var depreciations =
                Enumerable.Range(0, 6)
                .Select(x => DecimalMath.Pow(factor, x))
                .Select(x => data.AcquisitionValue * x)
                .SelectWithPrevious((prev, current) => prev - current)
                .Reverse()
                .ToList();

            var depreciation   = depreciations[period - 1];
            var remainingValue = data.AcquisitionValue - depreciations.Take(period).Sum();

            return(new CalculationResult(period, depreciation, remainingValue));
        }
        // TODO: incorporate hinting here
        /// <summary>
        /// Attempts to compile the operation with the given arguments.
        /// </summary>
        /// <param name="bas">the base of the operation</param>
        /// <param name="exp">the exponent of the operation</param>
        /// <param name="result">the result of the compilation</param>
        /// <returns><see langword="true"/> if it was compiled, <see langword="false"/> otherwise.</returns>
        public bool TryCompile(Expression bas, Expression exp, [MaybeNullWhen(false)] out Expression result)
        {
            result = null;

            var powMethod = Helpers.GetMethod <Action <decimal> >(a => DecimalMath.Pow(a, a)) !;

            Type outType = typeof(decimal);

            if (CompilerHelpers.IsIntegral(bas.Type) &&
                CompilerHelpers.IsIntegral(exp.Type) &&
                !CompilerHelpers.IsSigned(exp.Type))
            {
                outType = CompilerHelpers.IsSigned(bas.Type) ? typeof(long) : typeof(ulong);
            }
            try
            {
                if (bas.Type != typeof(decimal))
                {
                    bas = Expression.Convert(bas, typeof(decimal));
                }
                if (exp.Type != typeof(decimal))
                {
                    exp = Expression.Convert(exp, typeof(decimal));
                }
                result = Expression.Call(powMethod, bas, exp);
                if (result.Type != outType)
                {
                    result = Expression.Convert(result, outType);
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #9
0
        public decimal calculate(Queue <string> polska, decimal x)
        {
            decimal answer = 0;

            Stack <decimal> opers = new Stack <decimal>();

            foreach (string s in polska)
            {
                if (standartOperators.Contains(s))
                {
                    decimal a;
                    decimal b;

                    switch (s)
                    {
                    case "+":
                        opers.Push(opers.Pop() + opers.Pop());
                        break;

                    case "-":
                        b = opers.Pop();
                        a = opers.Pop();
                        opers.Push(a - b);
                        break;

                    case "*":
                        opers.Push(opers.Pop() * opers.Pop());
                        break;

                    case "/":
                        b = opers.Pop();
                        a = opers.Pop();
                        opers.Push(a / b);
                        break;

                    case "^":
                        b = opers.Pop();
                        a = opers.Pop();
                        opers.Push(DecimalMath.Pow(a, b));
                        break;

                    case "%":
                        b = opers.Pop();
                        a = opers.Pop();
                        opers.Push(a % b);
                        break;

                    case "sqrt":
                        opers.Push(DecimalMath.Sqrt(opers.Pop()));
                        break;

                    case "sin":
                        opers.Push(DecimalMath.Sin(opers.Pop()));
                        break;

                    case "cos":
                        opers.Push(DecimalMath.Cos(opers.Pop()));
                        break;

                    case "tan":
                        opers.Push(DecimalMath.Tan(opers.Pop()));
                        break;

                    case "atan":
                        opers.Push(DecimalMath.Atan(opers.Pop()));
                        break;

                    case "acos":
                        opers.Push(DecimalMath.Acos(opers.Pop()));
                        break;

                    case "asin":
                        opers.Push(DecimalMath.Asin(opers.Pop()));
                        break;

                    case "acotan":
                        opers.Push(DecimalMath.Atan(1 / opers.Pop()));
                        break;

                    case "exp":
                        opers.Push(DecimalMath.Exp(opers.Pop()));
                        break;

                    case "log":
                        opers.Push(DecimalMath.Log(opers.Pop()));
                        break;

                    case "ln":
                        opers.Push(DecimalMath.Log10(opers.Pop()));
                        break;

                    case "sinh":
                        opers.Push(DecimalMath.Sinh(opers.Pop()));
                        break;

                    case "cosh":
                        opers.Push(DecimalMath.Cosh(opers.Pop()));
                        break;

                    case "tanh":
                        opers.Push(DecimalMath.Tanh(opers.Pop()));
                        break;

                    case "abs":
                        opers.Push(DecimalMath.Abs(opers.Pop()));
                        break;

                    case "ceil":
                        opers.Push(DecimalMath.Ceiling(opers.Pop()));
                        break;

                    case "floor":
                        opers.Push(DecimalMath.Floor(opers.Pop()));
                        break;

                    case "fac":
                        opers.Push(factorial(opers.Pop()));
                        break;

                    case "sfac":
                        opers.Push(semifactorial(opers.Pop()));
                        break;

                    case "round":
                        opers.Push(DecimalMath.Round(opers.Pop()));
                        break;

                    case "fpart":
                        a = opers.Pop();
                        opers.Push(a - DecimalMath.Truncate(a));
                        break;
                    }
                }
                else if (s == "x")
                {
                    opers.Push(x);
                }
                else
                {
                    opers.Push(decimal.Parse(s));
                }
            }

            answer = opers.Pop();
            return(answer);
        }
Example #10
0
 public void FloatingPowerThrow(decimal bas, decimal exp)
 {
     Assert.Throws <OverflowException>(() => DecimalMath.Pow(bas, exp));
 }
Example #11
0
 public void IntegerPower(long bas, long exp, long expect)
 {
     Assert.Equal((decimal)expect, DecimalMath.Pow(bas, exp));
 }