def test_root_incorect(self): self.assertEqual( m.root(2, -5), None, "Expected solution: None -> Can't do even root of a negative number" ) self.assertEqual( m.root(0, 17), None, "Expected solution: None -> Can't do zero root of a number") self.assertEqual(m.root(-8, 0), None, "Expected solution: None -> Can't do root of a zero") self.assertEqual( m.root(-8, -53), None, "Expected solution: None -> Can't do even negative root of a negative number" )
def calcBinary(a, b, operator): if operator == "+": return math.add(a, b) elif operator == "-": return math.sub(a, b) elif operator == "×": return math.mul(a, b) elif operator == "÷": try: return math.div(a, b) except Exception as e: return e elif operator == "√": try: return math.root(b, a) except Exception as e: return e elif operator == "^": try: return math.pow(a, b) except Exception as e: return e elif operator == "%": try: return math.mod(a, b) except Exception as e: return e
def eq_pressed(self): x2 = float(self.result.text()) if self.pushButton_add.isChecked(): result_label = format(m.addition(self.x1, x2), '.15g') self.pushButton_add.setChecked(False) elif self.pushButton_sub.isChecked(): result_label = format(m.substraction(self.x1, x2), '.15g') self.pushButton_sub.setChecked(False) elif self.pushButton_mul.isChecked(): result_label = format(m.multiplication(self.x1, x2), '.15g') self.pushButton_mul.setChecked(False) elif self.pushButton_div.isChecked(): if m.division(self.x1, x2) is None: result_label = "Undefined" else: result_label = format(m.division(self.x1, x2), '.15g') self.pushButton_div.setChecked(False) elif self.pushButton_mod.isChecked(): if m.modulo(self.x1, x2) is None: result_label = "Undefined" else: result_label = format(m.modulo(self.x1, x2), '.15g') self.pushButton_mod.setChecked(False) elif self.pushButton_root.isChecked(): if m.root(self.x1, x2) is None: result_label = "Undefined" else: result_label = format(m.root(self.x1, x2), '.15g') self.pushButton_root.setChecked(False) elif self.pushButton_power.isChecked(): if m.exponencial(self.x1, x2) is None: result_label = "Undefined" else: result_label = format(m.exponencial(self.x1, x2), '.15g') self.pushButton_power.setChecked(False) else: result_label = self.result.text() self.result.setText(result_label) self.x2_type = False
def test_root(self): numbers_root = [4, 2, 25, 644, 1987, 330.5, 918, 13.1, 43, 6.5, 49, 9] numbers_results = [ 2, 1.414214, 5, 25.377155, 44.575778, 18.179659, 30.298515, 3.619392, 6.557439, 2.54951, 7, 3 ] for i in range(12): self.assertEqual(math_lib.root(numbers_root[i]), numbers_results[i]) # TESTING ANOTHER ROOTS self.assertEqual(math_lib.root(8, 3), 2) self.assertEqual(math_lib.root(16, 4), 2) self.assertEqual(math_lib.root(100, 5), 2.511886) self.assertEqual(math_lib.root(9, 0.5), 81) self.assertEqual(math_lib.root(5, -1), 0.2) self.assertEqual(math_lib.root(8, -3), 0.5) # TESTING ERRORS self.assertEqual(math_lib.root(-5, 2), ValueError) self.assertEqual(math_lib.root(-5, -2), ValueError) self.assertEqual(math_lib.root(5, 0), ValueError)
def calculate_result(): global amount, sum_normal, sum_square, result sum_normal = m.multiplication(sum_normal, (m.division(1, amount))) # 1/N * (sum x) result = m.multiplication(sum_normal, sum_normal) # x^2 result = m.multiplication(result, amount) # N*x^2 result = m.substraction(sum_square, result) # (sum (xi^2)) - (N*x^2) help = m.substraction(amount, 1) # N - 1 help = m.division(1, help) # 1/(N-1) result = m.multiplication(help, result) # (1/(N-1)) * ((sum (xi^2)) - (N*x^2)) result = m.root(2, result)
def test_root_correct_negative_decimal(self): self.assertAlmostEqual(m.root(-5, -22), -0.5389090339) self.assertAlmostEqual(m.root(-12, 428.56489), 0.6034833386) self.assertAlmostEqual(m.root(5.51, -2090278243656.45878), -172.17564002024687333)
def test_root_correct_positive_decimal(self): self.assertAlmostEqual(m.root(2, 2), 1.41421356237309504880168872420) self.assertAlmostEqual(m.root(12, 428.56489), 1.657046576) self.assertAlmostEqual(m.root(5.58564, 2090278243656), 160.5803766)
def test_root_correct_negative_whole(self): self.assertAlmostEqual(m.root(7, -10460353203), -27) self.assertAlmostEqual(m.root(9, -46411484401953), -33) self.assertAlmostEqual(m.root(23, -94143178827), -3)
def test_root_correct_positive_whole(self): self.assertAlmostEqual(m.root(5, 601692057), 57) self.assertAlmostEqual(m.root(8, 16777216), 8) self.assertAlmostEqual(m.root(3, 2090278243656), 12786)