Skip to content

Commit 7da5f37

Browse files
committed
Expose smallest subnormal and test consts
1 parent 5914811 commit 7da5f37

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

quaddtype/numpy_quaddtype/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
__all__ = [
99
'QuadPrecision', 'QuadPrecDType', 'SleefQuadPrecision', 'LongDoubleQuadPrecision',
1010
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128', 'pi', 'e',
11-
'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'min_value', 'epsilon'
11+
'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'epsilon', 'smallest_normal',
12+
'smallest_subnormal'
1213
]
1314

1415
def SleefQuadPrecision(value):
@@ -29,6 +30,7 @@ def LongDoubleQuadPrecDType():
2930
log10e = get_sleef_constant("log10e")
3031
ln2 = get_sleef_constant("ln2")
3132
ln10 = get_sleef_constant("ln10")
32-
max_value = get_sleef_constant("quad_max")
33-
min_value = get_sleef_constant("quad_min")
34-
epsilon = get_sleef_constant("epsilon")
33+
max_value = get_sleef_constant("max_value")
34+
epsilon = get_sleef_constant("epsilon")
35+
smallest_normal = get_sleef_constant("smallest_normal")
36+
smallest_subnormal = get_sleef_constant("smallest_subnormal")

quaddtype/numpy_quaddtype/src/quaddtype_main.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ static PyObject* get_sleef_constant(PyObject* self, PyObject* args) {
5252
result->value.sleef_value = SLEEF_M_LN2q;
5353
} else if (strcmp(constant_name, "ln10") == 0) {
5454
result->value.sleef_value = SLEEF_M_LN10q;
55-
} else if (strcmp(constant_name, "quad_max") == 0) {
55+
} else if (strcmp(constant_name, "max_value") == 0) {
5656
result->value.sleef_value = SLEEF_QUAD_MAX;
57-
} else if (strcmp(constant_name, "quad_min") == 0) {
58-
result->value.sleef_value = SLEEF_QUAD_MIN;
5957
} else if (strcmp(constant_name, "epsilon") == 0) {
6058
result->value.sleef_value = SLEEF_QUAD_EPSILON;
59+
} else if (strcmp(constant_name, "smallest_normal") == 0) {
60+
result->value.sleef_value = SLEEF_QUAD_MIN;
61+
} else if (strcmp(constant_name, "smallest_subnormal") == 0) {
62+
result->value.sleef_value = SLEEF_QUAD_DENORM_MIN;
6163
}
6264
else {
6365
PyErr_SetString(PyExc_ValueError, "Unknown constant name");

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)