def test_root_is_found2(Solver, a, b, root): epsilon = 1e-3 solver = Solver(epsilon=epsilon) f = lambda x: x**3 - x**2 - 3 * x + 2 # three roots: -1.618, 0.618, 2 result = solver(f, a, b) assert result.converged assert nearly_equal(result.x0, root, epsilon) assert nearly_equal(result.fx0, 0, epsilon) assert result.msg == Solver.messages["convergence"]
def test_root_is_found(Solver): f = lambda x: x**2 - 3 * x + 2 a, b = (0.5, 1.5) epsilon = 1e-5 solver = Solver(epsilon=epsilon) result = solver(f, a, b) assert result.converged assert nearly_equal(result.x0, 1, epsilon) assert nearly_equal(result.fx0, 0, epsilon) assert result.msg == Solver.messages["convergence"]
def test_root_is_found(Solver): f = lambda x: x**2 - 3 * x + 2 a, b = (0.5, 1.5) epsilon=1e-5 solver = Solver(epsilon=epsilon) result = solver(f, a, b) assert result.converged assert nearly_equal(result.x0, 1, epsilon) assert nearly_equal(result.fx0, 0, epsilon) assert result.msg == Solver.messages["convergence"]
def test_root_is_found3(Solver): # http://en.wikipedia.org/wiki/Bisection_method#Example:_Finding_the_root_of_a_polynomial f = lambda x: x**3 - x - 2 a, b = (1, 2) epsilon = 1e-3 solver = Solver(epsilon=epsilon) result = solver(f, a, b) assert result.converged assert nearly_equal(result.x0, 1.521, epsilon) assert nearly_equal(result.fx0, 0, epsilon) assert result.msg == Solver.messages["convergence"]
def test_infinities(epsilon): """ Test comparisons with infinity. """ pinf = float("inf") ninf = -pinf max_float = sys.float_info.max min_float = -max_float # infinite is equal to itself no matter the epsilon value used. assert nearly_equal(pinf, pinf, epsilon) assert nearly_equal(ninf, ninf, epsilon) # but the two infs compare as unequal assert not nearly_equal(pinf, ninf, epsilon) # infinite is not equal to the extreme allowed float values. assert not nearly_equal(ninf, min_float, epsilon) assert not nearly_equal(pinf, max_float, epsilon)
def test_opposite_sign2(): """ Test numbers with opposite sign. """ # mpf is a very small positive value. Something in the order of 1e-300 # The following compraisons are valid for epsilons > sys.float_info.epsilon mpf = sys.float_info.min epsilon = 1e-5 assert not nearly_equal(-1.0, 1.0, epsilon=epsilon) assert not nearly_equal(1.0, -1.0, epsilon=epsilon) assert not nearly_equal(1.000000001, -1.0, epsilon=epsilon) assert not nearly_equal(-1.0, 1.000000001, epsilon=epsilon) assert not nearly_equal(-1.000000001, 1.0, epsilon=epsilon) assert not nearly_equal(1.0, -1.000000001, epsilon=epsilon) assert not nearly_equal(1e-3, -1e-3, epsilon=epsilon) assert nearly_equal(1e-7, -1e-7, epsilon=epsilon) assert nearly_equal(10 * mpf, 10 * -mpf, epsilon=epsilon) assert nearly_equal(1e6 * mpf, 1e7 * -mpf, epsilon=epsilon)
def test_opposite_sign(num, epsilon): """ Test numbers with opposite sign. """ # mpf is a very small positive value. Something in the order of 1e-300 # The following compraisons are valid for epsilons > sys.float_info.epsilon mpf = sys.float_info.min # Comparing very small numbers should evaluate to True even if the numbers # have opposite signs. assert nearly_equal(mpf, mpf, epsilon=epsilon) assert nearly_equal(mpf, -mpf, epsilon=epsilon) assert nearly_equal(-mpf, -mpf, epsilon=epsilon) # comparing very big numbers with opposite sings should evaluate to False num = 1e40 assert not nearly_equal(num, -num, epsilon=epsilon) assert not nearly_equal(-num, num, epsilon=epsilon)
def test_ulp(epsilon): # mpf is a very small positive value. Something in the order of 1e-300 # The following compraisons are valid for epsilons > sys.float_info.epsilon mpf = sys.float_info.min assert nearly_equal(mpf, -mpf, epsilon) assert nearly_equal(mpf, 0, epsilon) assert nearly_equal(0, mpf, epsilon) assert nearly_equal(-mpf, 0, epsilon) assert nearly_equal(0, -mpf, epsilon) assert nearly_equal(1e-40, -mpf, epsilon) assert nearly_equal(1e-40, mpf, epsilon) assert nearly_equal(mpf, 1e-40, epsilon) assert nearly_equal(-mpf, 1e-40, epsilon)
def test_same_sign(num, epsilon): """ Test numbers with the same sign. """ # Define two "big" numbers n1 = num n2 = num + 9 * epsilon # if we change "9" to "10" tests will fail!!! # epsilon >= diff(n1, n2) assert nearly_equal(n1, n2, epsilon=epsilon * 1e+1) assert nearly_equal(n2, n1, epsilon=epsilon * 1e+1) assert nearly_equal(n1, n2, epsilon=epsilon * 1e+3) assert nearly_equal(n2, n1, epsilon=epsilon * 1e+3) assert nearly_equal(n1, n2, epsilon=epsilon * 1e+6) assert nearly_equal(n2, n1, epsilon=epsilon * 1e+6) # epsilon < diff(n1, n2) assert not nearly_equal(n1, n2, epsilon=epsilon * 1e-1) assert not nearly_equal(n2, n1, epsilon=epsilon * 1e-1) assert not nearly_equal(n1, n2, epsilon=epsilon * 1e-3) assert not nearly_equal(n2, n1, epsilon=epsilon * 1e-3) assert not nearly_equal(n1, n2, epsilon=epsilon * 1e-6) assert not nearly_equal(n2, n1, epsilon=epsilon * 1e-6)
def test_nan(epsilon): """ Test comparisons with NaN. """ pos = float("inf") neg = -float("inf") nan = float("nan") max_float = sys.float_info.max min_float = -max_float assert not nearly_equal(nan, nan, epsilon=epsilon) assert not nearly_equal(nan, 0.0, epsilon=epsilon) assert not nearly_equal(-0.0, nan, epsilon=epsilon) assert not nearly_equal(nan, -0.0, epsilon=epsilon) assert not nearly_equal(0.0, nan, epsilon=epsilon) assert not nearly_equal(nan, pos, epsilon=epsilon) assert not nearly_equal(pos, nan, epsilon=epsilon) assert not nearly_equal(nan, neg, epsilon=epsilon) assert not nearly_equal(neg, nan, epsilon=epsilon) assert not nearly_equal(nan, max_float, epsilon=epsilon) assert not nearly_equal(max_float, nan, epsilon=epsilon) assert not nearly_equal(nan, min_float, epsilon=epsilon) assert not nearly_equal(min_float, nan, epsilon=epsilon) assert not nearly_equal(nan, min_float, epsilon=epsilon) assert not nearly_equal(min_float, nan, epsilon=epsilon) assert not nearly_equal(nan, -min_float, epsilon=epsilon) assert not nearly_equal(-min_float, nan, epsilon=epsilon)
def test_zero(epsilon): """ Test comparisons involving zero. """ # zero equals itself... (duh) assert nearly_equal(0.0, 0.0, epsilon=epsilon) assert nearly_equal(0.0, -0.0, epsilon=epsilon) assert nearly_equal(-0.0, -0.0, epsilon=epsilon)