Skip to content

Commit c349b0c

Browse files
committed
Expose smallest subnormal and test consts
1 parent f043f8d commit c349b0c

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

quaddtype/numpy_quaddtype/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
'QuadPrecision', 'QuadPrecDType', 'SleefQuadPrecision', 'LongDoubleQuadPrecision',
1313
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128',
1414
# Constants
15-
'pi', 'e', 'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'min_value', 'epsilon',
15+
'pi', 'e', 'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'epsilon',
16+
'smallest_normal', 'smallest_subnormal',
1617
# QuadBLAS related functions
1718
'set_num_threads', 'get_num_threads', 'get_quadblas_version'
1819
]
@@ -35,6 +36,7 @@ def LongDoubleQuadPrecDType():
3536
log10e = get_sleef_constant("log10e")
3637
ln2 = get_sleef_constant("ln2")
3738
ln10 = get_sleef_constant("ln10")
38-
max_value = get_sleef_constant("quad_max")
39-
min_value = get_sleef_constant("quad_min")
40-
epsilon = get_sleef_constant("epsilon")
39+
max_value = get_sleef_constant("max_value")
40+
epsilon = get_sleef_constant("epsilon")
41+
smallest_normal = get_sleef_constant("smallest_normal")
42+
smallest_subnormal = get_sleef_constant("smallest_subnormal")

quaddtype/numpy_quaddtype/src/quaddtype_main.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,18 @@ get_sleef_constant(PyObject *self, PyObject *args)
6161
else if (strcmp(constant_name, "ln10") == 0) {
6262
result->value.sleef_value = SLEEF_M_LN10q;
6363
}
64-
else if (strcmp(constant_name, "quad_max") == 0) {
64+
else if (strcmp(constant_name, "max_value") == 0) {
6565
result->value.sleef_value = SLEEF_QUAD_MAX;
6666
}
67-
else if (strcmp(constant_name, "quad_min") == 0) {
68-
result->value.sleef_value = SLEEF_QUAD_MIN;
69-
}
7067
else if (strcmp(constant_name, "epsilon") == 0) {
7168
result->value.sleef_value = SLEEF_QUAD_EPSILON;
7269
}
70+
else if (strcmp(constant_name, "smallest_normal") == 0) {
71+
result->value.sleef_value = SLEEF_QUAD_MIN;
72+
}
73+
else if (strcmp(constant_name, "smallest_subnormal") == 0) {
74+
result->value.sleef_value = SLEEF_QUAD_DENORM_MIN;
75+
}
7376
else {
7477
PyErr_SetString(PyExc_ValueError, "Unknown constant name");
7578
Py_DECREF(result);

quaddtype/tests/test_dot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def test_matrix_matrix_with_special_values(self, special_val):
268268

269269
def test_all_nan_matrix(self):
270270
"""Test matrices filled with NaN"""
271-
A = create_quad_array([float('nan')] * 4, shape=(2, 2))
271+
A = create_quad_array([float("nan")] * 4, shape=(2, 2))
272272
B = create_quad_array([1, 2, 3, 4], shape=(2, 2))
273273

274274
result = np.matmul(A, B)
@@ -281,7 +281,7 @@ def test_all_nan_matrix(self):
281281
def test_inf_times_zero_produces_nan(self):
282282
"""Test that Inf * 0 correctly produces NaN per IEEE 754"""
283283
# Create a scenario where Inf * 0 occurs in matrix multiplication
284-
A = create_quad_array([float('inf'), 1.0], shape=(1, 2))
284+
A = create_quad_array([float("inf"), 1.0], shape=(1, 2))
285285
B = create_quad_array([0.0, 1.0], shape=(2, 1))
286286

287287
result = np.matmul(A, B)
@@ -291,7 +291,7 @@ def test_inf_times_zero_produces_nan(self):
291291

292292
def test_nan_propagation(self):
293293
"""Test that NaN properly propagates through matrix operations"""
294-
A = create_quad_array([1.0, float('nan'), 3.0, 4.0], shape=(2, 2))
294+
A = create_quad_array([1.0, float("nan"), 3.0, 4.0], shape=(2, 2))
295295
B = create_quad_array([1.0, 0.0, 0.0, 1.0], shape=(2, 2)) # Identity
296296

297297
result = np.matmul(A, B)
@@ -310,7 +310,7 @@ def test_zero_division_and_indeterminate_forms(self):
310310
# Test various indeterminate forms that should produce NaN
311311

312312
# Case: Inf - Inf form
313-
A = create_quad_array([float('inf'), float('inf')], shape=(1, 2))
313+
A = create_quad_array([float("inf"), float("inf")], shape=(1, 2))
314314
B = create_quad_array([1.0, -1.0], shape=(2, 1))
315315

316316
result = np.matmul(A, B)
@@ -321,7 +321,7 @@ def test_zero_division_and_indeterminate_forms(self):
321321
def test_mixed_inf_values(self):
322322
"""Test matrices with mixed infinite values"""
323323
# Use all-ones matrix to avoid Inf * 0 = NaN issues
324-
A = create_quad_array([float('inf'), 2, float('-inf'), 3], shape=(2, 2))
324+
A = create_quad_array([float("inf"), 2, float("-inf"), 3], shape=(2, 2))
325325
B = create_quad_array([1, 1, 1, 1], shape=(2, 2)) # All ones to avoid Inf*0
326326

327327
result = np.matmul(A, B)

quaddtype/tests/test_quaddtype.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import operator
55

6+
import numpy_quaddtype
67
from numpy_quaddtype import QuadPrecDType, QuadPrecision
78

89

@@ -11,6 +12,17 @@ def test_create_scalar_simple():
1112
assert isinstance(QuadPrecision(1.63), QuadPrecision)
1213
assert isinstance(QuadPrecision(1), QuadPrecision)
1314

15+
@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))])
16+
def test_math_constant(name, expected):
17+
assert isinstance(getattr(numpy_quaddtype, name), QuadPrecision)
18+
19+
assert np.float64(getattr(numpy_quaddtype, name)) == expected
20+
21+
22+
@pytest.mark.parametrize("name", ["max_value", "epsilon", "smallest_normal", "smallest_subnormal"])
23+
def test_finfo_constant(name):
24+
assert isinstance(getattr(numpy_quaddtype, name), QuadPrecision)
25+
1426

1527
def test_basic_equality():
1628
assert QuadPrecision("12") == QuadPrecision(
@@ -140,12 +152,12 @@ def test_inf():
140152
def test_dtype_creation():
141153
dtype = QuadPrecDType()
142154
assert isinstance(dtype, np.dtype)
143-
assert dtype.name == 'QuadPrecDType128'
155+
assert dtype.name == "QuadPrecDType128"
144156

145157

146158
def test_array_creation():
147159
arr = np.array([1, 2, 3], dtype=QuadPrecDType())
148-
assert arr.dtype.name == 'QuadPrecDType128'
160+
assert arr.dtype.name == "QuadPrecDType128"
149161
assert all(isinstance(x, QuadPrecision) for x in arr)
150162

151163

0 commit comments

Comments
 (0)