Skip to content

Commit c236a80

Browse files
committed
test: use pytest.approx for floating-point comparison in matrix inversion test
1 parent f330a89 commit c236a80

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

linear_algebra/matrix_inversion.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ def invert_matrix(matrix: list[list[float]]) -> list[list[float]]:
1111
Returns:
1212
list[list[float]]: Inverted matrix if invertible, else raises error.
1313
14-
>>> invert_matrix([[4.0, 7.0], [2.0, 6.0]])
15-
[[0.6000000000000001, -0.7000000000000001], [-0.2, 0.4]]
14+
>>> from pytest import approx
15+
>>> result = invert_matrix([[4.0, 7.0], [2.0, 6.0]])
16+
>>> expected = [[0.6000000000000001, -0.7000000000000001], [-0.2, 0.4]]
17+
>>> all (all(approx(x) == y for x, y in zip(row_res, row_exp))
18+
... for row_res, row_exp in zip(result, expected))
19+
True
1620
>>> invert_matrix([[1.0, 2.0], [0.0, 0.0]])
1721
Traceback (most recent call last):
1822
...

0 commit comments

Comments
 (0)