serhii.net

In the middle of the desert you can say anything you want

01 Feb 2024

pytest approx as almostequal

Pytest has pytest.approx() that does what unittest’s almostEqual() does for python floating point arithmetic quirks:

from pytest import approx

def test_splitting():
    ns = [
            [0.4], 
            [0.4, 0.5], 
        ]
    expected = [
            [0.4, 0.6], 
            [0.4, 0.5, 0.1], 
            ]

    for i,n  in enumerate(ns):
        assert _find_split_sizes(n)==approx(expected[i])

(Quirks being basically this)

18:22:52 ~/uuni/master/code/ua_cbt/src/ 0
> python3
Python 3.8.10 (default, Nov 22 2023, 10:22:35)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.1+0.2
0.30000000000000004
Nel mezzo del deserto posso dire tutto quello che voglio.