Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion quaddtype/numpy_quaddtype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128',
# Constants
'pi', 'e', 'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'epsilon',
'smallest_normal', 'smallest_subnormal',
'smallest_normal', 'smallest_subnormal', 'bits', 'precision', 'resolution',
# QuadBLAS related functions
'set_num_threads', 'get_num_threads', 'get_quadblas_version'
]
Expand All @@ -40,3 +40,6 @@ def LongDoubleQuadPrecDType():
epsilon = get_sleef_constant("epsilon")
smallest_normal = get_sleef_constant("smallest_normal")
smallest_subnormal = get_sleef_constant("smallest_subnormal")
bits = get_sleef_constant("bits")
precision = get_sleef_constant("precision")
resolution = get_sleef_constant("resolution")
16 changes: 16 additions & 0 deletions quaddtype/numpy_quaddtype/src/quaddtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ get_sleef_constant(PyObject *self, PyObject *args)
else if (strcmp(constant_name, "smallest_subnormal") == 0) {
result->value.sleef_value = SLEEF_QUAD_DENORM_MIN;
}
else if (strcmp(constant_name, "bits") == 0) {
Py_DECREF(result);
return PyLong_FromLong(sizeof(Sleef_quad) * CHAR_BIT);
}
else if (strcmp(constant_name, "precision") == 0) {
Py_DECREF(result);
// precision = int(-log10(epsilon))
int64_t precision = Sleef_cast_to_int64q1(Sleef_negq1(Sleef_log10q1_u10(SLEEF_QUAD_EPSILON)));
return PyLong_FromLong(precision);
}
else if (strcmp(constant_name, "resolution") == 0) {
// precision = int(-log10(epsilon))
int64_t precision = Sleef_cast_to_int64q1(Sleef_negq1(Sleef_log10q1_u10(SLEEF_QUAD_EPSILON)));
// resolution = 10 ** (-precision)
result->value.sleef_value = Sleef_powq1_u10(Sleef_cast_from_int64q1(10), Sleef_cast_from_int64q1(-precision));
}
else {
PyErr_SetString(PyExc_ValueError, "Unknown constant name");
Py_DECREF(result);
Expand Down
7 changes: 6 additions & 1 deletion quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ def test_math_constant(name, expected):
assert np.float64(getattr(numpy_quaddtype, name)) == expected


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


@pytest.mark.parametrize("name,value", [("bits", 128), ("precision", 33)])
def test_finfo_int_constant(name, value):
assert getattr(numpy_quaddtype, name) == value


def test_basic_equality():
assert QuadPrecision("12") == QuadPrecision(
"12.0") == QuadPrecision("12.00")
Expand Down
Loading