Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
test_finfo.py87 linesDownload Raw Back to tests
1import pytest
2
3import numpy as np
4from numpy import exp2, log10
5from numpy._core import numerictypes as ntypes
6
7
8class MachArLike:
9    """Minimal class to simulate machine arithmetic parameters."""
10    def __init__(self, dtype, machep, negep, minexp, maxexp, nmant, iexp):
11        self.dtype = dtype
12        self.machep = machep
13        self.negep = negep
14        self.minexp = minexp
15        self.maxexp = maxexp
16        self.nmant = nmant
17        self.iexp = iexp
18        self.eps = exp2(dtype(-nmant))
19        self.epsneg = exp2(dtype(negep))
20        self.precision = int(-log10(self.eps))
21        self.resolution = dtype(10) ** (-self.precision)
22
23
24@pytest.fixture
25def float16_ma():
26    """Machine arithmetic parameters for float16."""
27    f16 = ntypes.float16
28    return MachArLike(f16,
29                      machep=-10,
30                      negep=-11,
31                      minexp=-14,
32                      maxexp=16,
33                      nmant=10,
34                      iexp=5)
35
36
37@pytest.fixture
38def float32_ma():
39    """Machine arithmetic parameters for float32."""
40    f32 = ntypes.float32
41    return MachArLike(f32,
42                      machep=-23,
43                      negep=-24,
44                      minexp=-126,
45                      maxexp=128,
46                      nmant=23,
47                      iexp=8)
48
49
50@pytest.fixture
51def float64_ma():
52    """Machine arithmetic parameters for float64."""
53    f64 = ntypes.float64
54    return MachArLike(f64,
55                      machep=-52,
56                      negep=-53,
57                      minexp=-1022,
58                      maxexp=1024,
59                      nmant=52,
60                      iexp=11)
61
62
63@pytest.mark.parametrize("dtype,ma_fixture", [
64    (np.half, "float16_ma"),
65    (np.float32, "float32_ma"),
66    (np.float64, "float64_ma"),
67])
68@pytest.mark.parametrize("prop", [
69    'machep', 'negep', 'minexp', 'maxexp', 'nmant', 'iexp',
70    'eps', 'epsneg', 'precision', 'resolution'
71])
72@pytest.mark.thread_unsafe(
73    reason="complex fixture setup is thread-unsafe (pytest-dev/pytest#13768.)"
74)
75def test_finfo_properties(dtype, ma_fixture, prop, request):
76    """Test that finfo properties match expected machine arithmetic values."""
77    ma = request.getfixturevalue(ma_fixture)
78    finfo = np.finfo(dtype)
79
80    actual = getattr(finfo, prop)
81    expected = getattr(ma, prop)
82
83    assert actual == expected, (
84        f"finfo({dtype}) property '{prop}' mismatch: "
85        f"expected {expected}, got {actual}"
86    )
87 
codekingpro/portable-devtools · Team Ai