/* @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); }
public void TestDivision() { //rules: cannnot divide by 0 MathComponents NewMath = new MathComponents(); // tests to check division functionality decimal?result = NewMath.Divide(1, 1); Assert.AreEqual(1, result); result = NewMath.Divide(10, 5); Assert.AreEqual(2, result); result = NewMath.Divide(10, -2.5m); Assert.AreEqual(-4, result); result = NewMath.Divide(-18, -2); Assert.AreEqual(9, result); result = NewMath.Divide(0, 452871); Assert.AreEqual(0, result); result = NewMath.Divide(11656.5834m, 0.2541m); Assert.AreEqual(45874, result); result = NewMath.Divide(20.00m, 5.00m); Assert.AreEqual(4, result); result = NewMath.Divide(-2060309849.5m, -4517); Assert.AreEqual(456123.5m, result); result = NewMath.Divide(-6.25m, -0.05m); Assert.AreEqual(125, result); result = NewMath.Divide(0.008m, 0.008m); Assert.AreEqual(1, result); //test division by 0 result = NewMath.Divide(7744654.1254m, 0); Assert.AreEqual(null, result); result = NewMath.Divide(-451255.126359m, 0); Assert.AreEqual(null, result); result = NewMath.Divide(0, 0); Assert.AreEqual(null, result); // test length of returning value (max 9 significant digits) result = NewMath.Divide(555555.55m, 5.222222m); Assert.AreEqual(RoundOff(106382.9821865098m), result); result = NewMath.Divide(3216546, 123.456m); Assert.AreEqual(RoundOff(26054.1893468118195m), result); // test error handling result = NewMath.Divide(999999999, 0.5m); Assert.AreEqual(null, result); result = NewMath.Divide(898989898, 0.005m); Assert.AreEqual(null, result); }