Пример #1
0
        /// <summary>
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        /// /// <exception cref="MathsException"></exception>
        public static IList <double?> Quadratic(double a, double?b, double?c)
        {
            var result = new double?[2];

            try
            {
                // a is never null or zero
                if (a == 0.00D)
                {
                    throw new MathsException("Coefficient a is zero");
                }

                b = Objects.IsNull(b) ? 0.00D : b;
                c = Objects.IsNull(c) ? 0.00D : c;

                if (b == 0.00D && c == 0.00D)
                {
                    // only one solution
                    return new List <double?> {
                               0.00D, null
                    }
                }
                ;

                checked
                {
                    var partOne = -b;

                    if (double.IsInfinity(partOne.Value))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(partOne.Value))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var bSquare = b.Value * b.Value;

                    if (double.IsInfinity(bSquare))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(bSquare))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var fourAC = 4.00D * a * c.Value;

                    if (double.IsInfinity(fourAC))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(fourAC))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var minus = bSquare - fourAC;

                    if (double.IsInfinity(minus))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(minus))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var partTwo = Math.Sqrt(minus);

                    if (double.IsInfinity(partTwo))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(partTwo))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var partThree = 2.00D * a;

                    if (double.IsInfinity(partThree))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(partThree))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    result[0] = (partOne + partTwo) / partThree;
                    result[1] = (partOne - partTwo) / partThree;

                    if (double.IsInfinity(result[0].Value))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(result[0].Value))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    if (double.IsInfinity(result[1].Value))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(result[1].Value))
                    {
                        throw new OverflowException("Underflow detected");
                    }
                }
            }
            catch (OverflowException ovEx)
            {
                throw new MathsException(ovEx.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        ///     Multiplies a long integer by a sum of long integers.
        /// </summary>
        /// <param name="value">The long integer to put to the power of.</param>
        /// <param name="vars">The sum of long integers of the power of.</param>
        /// <returns>The power of long integer to the sum of long integers.</returns>
        /// <exception cref="GetRealGood.Utils.MathsException">
        ///     You should catch this exception, it denotes any exceptional
        ///     condition within the method.
        /// </exception>
        public static long PowerOf(long value, params long[] toThePowersOf)
        {
            if (Objects.IsNull(toThePowersOf))
            {
                throw new MathsException("Null value passed in");
            }

            if (toThePowersOf.Length == 0)
            {
                throw new MathsException("Empty value passed in");
            }

            var result = value;

            try
            {
                checked
                {
                    long?totalPowerOf = Sum(toThePowersOf);

                    if (Objects.IsNull(totalPowerOf))
                    {
                        throw new Exception();
                    }

                    if (totalPowerOf == 0)
                    {
                        return(1);
                    }

                    if (totalPowerOf == 1)
                    {
                        return(value);
                    }

                    var powerOf = 1;

                    while (powerOf++ < totalPowerOf.Value)
                    {
                        result *= value;
                    }
                }
            }
            catch (OverflowException oEx)
            {
                throw new MathsException(oEx.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            if (double.IsPositiveInfinity(result))
            {
                throw new MathsException("Positive overflow");
            }
            if (double.IsNegativeInfinity(result))
            {
                throw new MathsException("Negative overflow");
            }

            return(result);
        }