Skip to content

Commit 34edb32

Browse files
authored
Merge pull request #132 from juntyr/consts
2 parents 116a605 + fe476c2 commit 34edb32

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

quaddtype/numpy_quaddtype/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128',
1414
# Constants
1515
'pi', 'e', 'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'epsilon',
16-
'smallest_normal', 'smallest_subnormal',
16+
'smallest_normal', 'smallest_subnormal', 'bits', 'precision', 'resolution',
1717
# QuadBLAS related functions
1818
'set_num_threads', 'get_num_threads', 'get_quadblas_version'
1919
]
@@ -40,3 +40,6 @@ def LongDoubleQuadPrecDType():
4040
epsilon = get_sleef_constant("epsilon")
4141
smallest_normal = get_sleef_constant("smallest_normal")
4242
smallest_subnormal = get_sleef_constant("smallest_subnormal")
43+
bits = get_sleef_constant("bits")
44+
precision = get_sleef_constant("precision")
45+
resolution = get_sleef_constant("resolution")

quaddtype/numpy_quaddtype/src/quaddtype_main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ get_sleef_constant(PyObject *self, PyObject *args)
7373
else if (strcmp(constant_name, "smallest_subnormal") == 0) {
7474
result->value.sleef_value = SLEEF_QUAD_DENORM_MIN;
7575
}
76+
else if (strcmp(constant_name, "bits") == 0) {
77+
Py_DECREF(result);
78+
return PyLong_FromLong(sizeof(Sleef_quad) * CHAR_BIT);
79+
}
80+
else if (strcmp(constant_name, "precision") == 0) {
81+
Py_DECREF(result);
82+
// precision = int(-log10(epsilon))
83+
int64_t precision = Sleef_cast_to_int64q1(Sleef_negq1(Sleef_log10q1_u10(SLEEF_QUAD_EPSILON)));
84+
return PyLong_FromLong(precision);
85+
}
86+
else if (strcmp(constant_name, "resolution") == 0) {
87+
// precision = int(-log10(epsilon))
88+
int64_t precision = Sleef_cast_to_int64q1(Sleef_negq1(Sleef_log10q1_u10(SLEEF_QUAD_EPSILON)));
89+
// resolution = 10 ** (-precision)
90+
result->value.sleef_value = Sleef_powq1_u10(Sleef_cast_from_int64q1(10), Sleef_cast_from_int64q1(-precision));
91+
}
7692
else {
7793
PyErr_SetString(PyExc_ValueError, "Unknown constant name");
7894
Py_DECREF(result);

quaddtype/tests/test_quaddtype.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ def test_math_constant(name, expected):
1919
assert np.float64(getattr(numpy_quaddtype, name)) == expected
2020

2121

22-
@pytest.mark.parametrize("name", ["max_value", "epsilon", "smallest_normal", "smallest_subnormal"])
22+
@pytest.mark.parametrize("name", ["max_value", "epsilon", "smallest_normal", "smallest_subnormal", "resolution"])
2323
def test_finfo_constant(name):
2424
assert isinstance(getattr(numpy_quaddtype, name), QuadPrecision)
2525

2626

27+
@pytest.mark.parametrize("name,value", [("bits", 128), ("precision", 33)])
28+
def test_finfo_int_constant(name, value):
29+
assert getattr(numpy_quaddtype, name) == value
30+
31+
2732
def test_basic_equality():
2833
assert QuadPrecision("12") == QuadPrecision(
2934
"12.0") == QuadPrecision("12.00")

0 commit comments

Comments
 (0)