forked from numpy/numpy-user-dtypes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_quaddtype.py
More file actions
173 lines (131 loc) · 6.09 KB
/
test_quaddtype.py
File metadata and controls
173 lines (131 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import pytest
import sys
import numpy as np
import operator
import numpy_quaddtype
from numpy_quaddtype import QuadPrecDType, QuadPrecision
def test_create_scalar_simple():
assert isinstance(QuadPrecision("12.0"), QuadPrecision)
assert isinstance(QuadPrecision(1.63), QuadPrecision)
assert isinstance(QuadPrecision(1), QuadPrecision)
@pytest.mark.parametrize("name,expected", [("pi", np.pi), ("e", np.e), ("log2e", np.log2(np.e)), ("log10e", np.log10(np.e)), ("ln2", np.log(2.0)), ("ln10", np.log(10.0))])
def test_math_constant(name, expected):
assert isinstance(getattr(numpy_quaddtype, name), QuadPrecision)
assert np.float64(getattr(numpy_quaddtype, name)) == expected
@pytest.mark.parametrize("name", ["max_value", "epsilon", "smallest_normal", "smallest_subnormal"])
def test_finfo_constant(name):
assert isinstance(getattr(numpy_quaddtype, name), QuadPrecision)
def test_basic_equality():
assert QuadPrecision("12") == QuadPrecision(
"12.0") == QuadPrecision("12.00")
@pytest.mark.parametrize("op", ["add", "sub", "mul", "truediv", "pow"])
@pytest.mark.parametrize("other", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_binary_ops(op, other):
if op == "truediv" and float(other) == 0:
pytest.xfail("float division by zero")
op_func = getattr(operator, op)
quad_a = QuadPrecision("12.5")
quad_b = QuadPrecision(other)
float_a = 12.5
float_b = float(other)
quad_result = op_func(quad_a, quad_b)
float_result = op_func(float_a, float_b)
with np.errstate(invalid="ignore"):
assert (
(np.float64(quad_result) == float_result) or
(np.abs(np.float64(quad_result) - float_result) < 1e-10) or
((float_result != float_result) and (quad_result != quad_result))
)
@pytest.mark.parametrize("op", ["eq", "ne", "le", "lt", "ge", "gt"])
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_comparisons(op, a, b):
op_func = getattr(operator, op)
quad_a = QuadPrecision(a)
quad_b = QuadPrecision(b)
float_a = float(a)
float_b = float(b)
assert op_func(quad_a, quad_b) == op_func(float_a, float_b)
@pytest.mark.parametrize("op", ["eq", "ne", "le", "lt", "ge", "gt"])
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_array_comparisons(op, a, b):
op_func = getattr(operator, op)
quad_a = np.array(QuadPrecision(a))
quad_b = np.array(QuadPrecision(b))
float_a = np.array(float(a))
float_b = np.array(float(b))
assert np.array_equal(op_func(quad_a, quad_b), op_func(float_a, float_b))
@pytest.mark.parametrize("op", ["minimum", "maximum", "fmin", "fmax"])
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_array_minmax(op, a, b):
if op in ["fmin", "fmax"]:
pytest.skip("fmin and fmax ufuncs are not yet supported")
op_func = getattr(np, op)
quad_a = np.array([QuadPrecision(a)])
quad_b = np.array([QuadPrecision(b)])
float_a = np.array([float(a)])
float_b = np.array([float(b)])
quad_res = op_func(quad_a, quad_b)
float_res = op_func(float_a, float_b)
# FIXME: @juntyr: replace with array_equal once isnan is supported
with np.errstate(invalid="ignore"):
assert np.all((quad_res == float_res) | ((quad_res != quad_res) & (float_res != float_res)))
@pytest.mark.parametrize("op", ["amin", "amax", "nanmin", "nanmax"])
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_array_aminmax(op, a, b):
if op in ["nanmin", "nanmax"]:
pytest.skip("fmin and fmax ufuncs are not yet supported")
op_func = getattr(np, op)
quad_ab = np.array([QuadPrecision(a), QuadPrecision(b)])
float_ab = np.array([float(a), float(b)])
quad_res = op_func(quad_ab)
float_res = op_func(float_ab)
# FIXME: @juntyr: replace with array_equal once isnan is supported
with np.errstate(invalid="ignore"):
assert np.all((quad_res == float_res) | ((quad_res != quad_res) & (float_res != float_res)))
@pytest.mark.parametrize("op, val, expected", [
("neg", "3.0", "-3.0"),
("neg", "-3.0", "3.0"),
("pos", "3.0", "3.0"),
("pos", "-3.0", "-3.0"),
("abs", "3.0", "3.0"),
("abs", "-3.0", "3.0"),
("neg", "12.5", "-12.5"),
("pos", "100.0", "100.0"),
("abs", "-25.5", "25.5"),
])
def test_unary_ops(op, val, expected):
quad_val = QuadPrecision(val)
expected_val = QuadPrecision(expected)
if op == "neg":
result = -quad_val
elif op == "pos":
result = +quad_val
elif op == "abs":
result = abs(quad_val)
else:
raise ValueError(f"Unsupported operation: {op}")
assert result == expected_val, f"{op}({val}) should be {expected}, but got {result}"
def test_inf():
assert QuadPrecision("inf") > QuadPrecision("1e1000")
assert QuadPrecision("-inf") < QuadPrecision("-1e1000")
def test_dtype_creation():
dtype = QuadPrecDType()
assert isinstance(dtype, np.dtype)
assert dtype.name == "QuadPrecDType128"
def test_array_creation():
arr = np.array([1, 2, 3], dtype=QuadPrecDType())
assert arr.dtype.name == "QuadPrecDType128"
assert all(isinstance(x, QuadPrecision) for x in arr)
def test_array_operations():
arr1 = np.array(
[QuadPrecision("1.5"), QuadPrecision("2.5"), QuadPrecision("3.5")])
arr2 = np.array(
[QuadPrecision("0.5"), QuadPrecision("1.0"), QuadPrecision("1.5")])
result = arr1 + arr2
expected = np.array(
[QuadPrecision("2.0"), QuadPrecision("3.5"), QuadPrecision("5.0")])
assert all(x == y for x, y in zip(result, expected))