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 MathLibraryTestPow(unittest.TestCase): def setUp(self): self.mathlib = Mathlibrary() def testPowNegativeExponent(self): with self.assertRaises(ValueError): self.mathlib.pow(-5, -4) with self.assertRaises(ValueError): self.mathlib.pow(423, -5892) def testPowFloatExponent(self): with self.assertRaises(ValueError): self.mathlib.pow(42, 4.20424) with self.assertRaises(ValueError): self.mathlib.pow(532.43, 49.00001) def testPowZeroExponent(self): self.assertEqual(self.mathlib.pow(0, 0), 1) self.assertEqual(self.mathlib.pow(123, 0), 1) self.assertEqual(self.mathlib.pow(-123, 0), 1) self.assertEqual(self.mathlib.pow(-12.3, 0), 1) self.assertEqual(self.mathlib.pow(12.3, 0), 1) def testPowEvenExponent(self): self.assertEqual(self.mathlib.pow(2, 2), 4) self.assertEqual(self.mathlib.pow(12, 6), 2985984) self.assertEqual(self.mathlib.pow(43, 4), 3418801) self.assertEqual(self.mathlib.pow(-4, 2), 16) self.assertEqual(self.mathlib.pow(-80, 4), 40960000) def testPowOddExponent(self): self.assertEqual(self.mathlib.pow(3, 3), 27) self.assertEqual(self.mathlib.pow(47, 5), 229345007) self.assertEqual(self.mathlib.pow(-12, 7), -35831808) self.assertEqual(self.mathlib.pow(-37, 5), -69343957)