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)))
Exemple #3
0
class MathLibraryTestDiv(unittest.TestCase):
    def setUp(self):
        self.mathlib = Mathlibrary()

    def testDivByZero(self):
        with self.assertRaises(ZeroDivisionError):
            self.mathlib.div(2145, 0)
        with self.assertRaises(ZeroDivisionError):
            self.mathlib.div(0, 0)

    def testDivPossitive(self):
        self.assertEqual(self.mathlib.div(0, 2), 0)
        self.assertEqual(self.mathlib.div(234, 2), 117)
        self.assertEqual(self.mathlib.div(10206, 42), 243)
        self.assertEqual(self.mathlib.div(11348337, 49127), 231)

    def testDivNegative(self):
        self.assertEqual(self.mathlib.div(-234, 117), -2)
        self.assertEqual(self.mathlib.div(-10206, -42), 243)
        self.assertEqual(self.mathlib.div(11348337, -49127), -231)

    def testDivFloat(self):
        self.assertAlmostEqual(self.mathlib.div(2, 3), 0.6666666666)
        self.assertAlmostEqual(self.mathlib.div(345, -17), -20.294117647)
        self.assertAlmostEqual(self.mathlib.div(840143, 4910), 171.1085539714)
        self.assertAlmostEqual(self.mathlib.div(784.893253, 9523.7329), 0.08241445463)