예제 #1
0
        /* @brief Loads numbers from input, accumulates their sum, sum of their squares and their count; calculated standard deviation and prints it
         */
        private static int Main(string[] args)
        {
            MathComponents math = new MathComponents();

            bool    conversionSuccessful;
            string  input;
            decimal square               = 0;
            decimal linearAccumulator    = 0;
            decimal quadraticAccumulator = 0;
            int     count = 0;

            while (!string.IsNullOrEmpty(input = Console.ReadLine()))
            {
                conversionSuccessful = Decimal.TryParse(input, out decimal currentNumber);
                if (!conversionSuccessful)
                {
                    Console.WriteLine("Conversion error");
                    return(-1);
                }
                count++;
                linearAccumulator    = CheckNull(math.Add(linearAccumulator, currentNumber));
                square               = CheckNull(math.Exponentiate(currentNumber, 2));
                quadraticAccumulator = CheckNull(math.Add(quadraticAccumulator, square));
            }

            if (count < 2)
            {
                Console.WriteLine("Minimum of 2 numbers required");
                return(-1);
            }

            decimal average = CheckNull(math.Divide(1, count));

            average = CheckNull(math.Multiply(average, linearAccumulator));

            decimal deviationLeft = CheckNull(math.Subtract(count, 1));

            deviationLeft = CheckNull(math.Divide(1, deviationLeft));

            decimal deviationRight = CheckNull(math.Exponentiate(average, 2));

            deviationRight = CheckNull(math.Multiply(count, deviationRight));
            deviationRight = CheckNull(math.Subtract(quadraticAccumulator, deviationRight));

            decimal deviation = CheckNull(math.Multiply(deviationLeft, deviationRight));

            deviation = CheckNull(math.Root(2, deviation));

            /* DEBUG INFO
             * Console.WriteLine("N: " + count);
             * Console.WriteLine("sum x: " + linearAccumulator);
             * Console.WriteLine("sum x^2: " + quadraticAccumulator);
             * Console.WriteLine("average x: " + average);
             * // END INFO */

            Console.WriteLine(deviation);

            //Console.ReadLine();

            return(0);
        }
예제 #2
0
        /**
         * @brief Tests to check multiplication functionality
         * @brief  testing if length is max 9 significant digits
         */
        public void TestMultiplication()
        {
            //no mathematical rules for multiplication
            MathComponents NewMath = new MathComponents();

            // tests to check multiplication functionality
            decimal?result = NewMath.Multiply(1, 1);

            Assert.AreEqual(1, result);

            result = NewMath.Multiply(0, 0);
            Assert.AreEqual(0, result);

            result = NewMath.Multiply(1, 1);
            Assert.AreEqual(1, result);

            result = NewMath.Multiply(125487, 0);
            Assert.AreEqual(0, result);

            result = NewMath.Multiply(0, 123547.254m);
            Assert.AreEqual(0, result);

            result = NewMath.Multiply(5.00m, 4.00m);
            Assert.AreEqual(20, result);

            result = NewMath.Multiply(-6, -5);
            Assert.AreEqual(30, result);

            result = NewMath.Multiply(7, -6);
            Assert.AreEqual(-42, result);

            result = NewMath.Multiply(-8, 9);
            Assert.AreEqual(-72, result);

            result = NewMath.Multiply(10, -0.5m);
            Assert.AreEqual(-5, result);

            // test length of returning value (max 9 significant digits)
            result = NewMath.Multiply(0.5125479645m, 5.4521m);
            Assert.AreEqual(RoundOff(2.79446275725045m), result);

            result = NewMath.Multiply(-999454, -9.000005m);
            Assert.AreEqual(RoundOff(8995090.99727m), result);

            result = NewMath.Multiply(0.55555555m, 0.9999999m);
            Assert.AreEqual(RoundOff(0.55555549m), result);

            // test error handling
            result = NewMath.Multiply(423135487, 47712318);
            Assert.AreEqual(null, result);

            result = NewMath.Multiply(54545454, 808.80855m);
            Assert.AreEqual(null, result);
        }