mainmatter / rust-python-interoperability

A self-paced course to write Python extensions in Rust, one exercise at a time.
https://rust-exercises.com/rust-python-interop/
33 stars 2 forks source link

Typo fixes, nitpicks, and `pytest.approx` #3

Closed jaywonchung closed 1 month ago

jaywonchung commented 1 month ago

As I was going through the course, I gathered some typo fixes and nitpicks.

Other than text tweaks, one non-trivial change I made is to the Python test file of 02_classes/07_outro. When comparing two floating point numbers, small precision differences can lead to unwanted assertion fails like:

    def test_discount():
        discount = Discount(0.1)
>       assert discount.percentage == 0.1
E       assert 0.10000000149011612 == 0.1

I got this when I used f32 for the floats instead of f64. But I think tests ought to pass when f32 is used.

That's where pytest.approx comes in:

    def test_discount():
        discount = Discount(0.1)
        assert discount.percentage == pytest.approx(0.1)  # Now passes

It's all up to the author -- Please feel free to accept and reject individual changes as you see fit. Thanks as always for the wonderful course.

LukeMathWalker commented 1 month ago

That's where pytest.approx comes in:

    def test_discount():
        discount = Discount(0.1)
        assert discount.percentage == pytest.approx(0.1)  # Now passes

This is indeed the right way of comparing floats, thanks for patching it up!