def doMath(op, op1, op2): if op == "*": return Mathlibrary.mul(op1, op2) elif op == "/": return Mathlibrary.div(op1, op2) elif op == "+": return Mathlibrary.add(op1, op2) elif op == "√": return Mathlibrary.root(op2, op1) elif op == "^": return Mathlibrary.pow(op1, op2) else: return Mathlibrary.sub(op1, op2)
inputNumbers = inputNumbers.replace('\n', ' ') #Count the numbers for x in inputNumbers.split(): N += 1 #Profiling on one single string if oneString: subSum = createSubSum(inputNumbers) nextSum = createSum(inputNumbers) evalString = "2 √ ( ( 1 / ( {} - 1 ) ) * ( {} - {} ) )".format( N, nextSum, subSum) with PyCallGraph(output=GraphvizOutput()): print(resolve(evalString)) #Profiling using side calculations else: firstSum = 0 secondSum = 0 with PyCallGraph(output=GraphvizOutput()): #Calculate SUM of numbers and SUM of numbers^2 for x in inputNumbers.split(): firstSum = Mathlibrary.add(firstSum, float(x)) #sum of Xi secondSum = Mathlibrary.add(secondSum, Mathlibrary.pow(float(x), 2)) #N*(x')^2 firstSum = Mathlibrary.pow(Mathlibrary.div(firstSum, N), 2) firstSum = Mathlibrary.mul(firstSum, N) finalSum = Mathlibrary.sub(secondSum, firstSum) #final formula print(resolve("2 √ ( 1 / ( {} - 1 ) * {} )".format(N, finalSum)))
class MathLibraryTestAdd(unittest.TestCase): def setUp(self): self.mathlib = Mathlibrary() def testAddPositive(self): self.assertEqual(self.mathlib.add(0, 0), 0) self.assertEqual(self.mathlib.add(42, 37), 79) self.assertEqual(self.mathlib.add(234, 743), 977) self.assertEqual(self.mathlib.add(10000, 233891), 243891) def testAddNegative(self): self.assertEqual(self.mathlib.add(-14, -48), -62) self.assertEqual(self.mathlib.add(-34, 56), 22) self.assertEqual(self.mathlib.add(-2938, -230), -3168) self.assertEqual(self.mathlib.add(-83924, 21481), -62443) self.assertEqual(self.mathlib.add(1237471, -42194914284), -42193676813) def testAddPositiveFloat(self): self.assertAlmostEqual(self.mathlib.add(12.284523, 5.21421), 17.498733) self.assertAlmostEqual(self.mathlib.add(0.124894, 0.111111111), 0.23600511) self.assertAlmostEqual(self.mathlib.add(2389.4829, 40124.840179), 42514.323079) self.assertAlmostEqual(self.mathlib.add(99.9999999, 99.0001), 199.0000999) self.assertNotAlmostEqual(self.mathlib.add(0.0000000001, 0.0000001), 0.0000002) def testAddNegativeFloat(self): self.assertAlmostEqual(self.mathlib.add(-12.284523, -5.21421), -17.498733) self.assertAlmostEqual(self.mathlib.add(-0.124894, -0.111111111), -0.23600511) self.assertAlmostEqual(self.mathlib.add(-2389.4829, -40124.840179), -42514.323079) self.assertAlmostEqual(self.mathlib.add(-99.9999999, -99.0001), -199.0000999) self.assertNotAlmostEqual(self.mathlib.add(-0.0000000001, -0.0000001), -0.0000002) self.assertAlmostEqual(self.mathlib.add(0.9999998, -0.9999999), -0.0000001)