Skip to content

Expose smallest subnormal constant and add tests for consts #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions quaddtype/numpy_quaddtype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
__all__ = [
'QuadPrecision', 'QuadPrecDType', 'SleefQuadPrecision', 'LongDoubleQuadPrecision',
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128', 'pi', 'e',
'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'min_value', 'epsilon'
'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'epsilon', 'smallest_normal',
'smallest_subnormal'
]

def SleefQuadPrecision(value):
Expand All @@ -29,6 +30,7 @@ def LongDoubleQuadPrecDType():
log10e = get_sleef_constant("log10e")
ln2 = get_sleef_constant("ln2")
ln10 = get_sleef_constant("ln10")
max_value = get_sleef_constant("quad_max")
min_value = get_sleef_constant("quad_min")
epsilon = get_sleef_constant("epsilon")
max_value = get_sleef_constant("max_value")
epsilon = get_sleef_constant("epsilon")
smallest_normal = get_sleef_constant("smallest_normal")
smallest_subnormal = get_sleef_constant("smallest_subnormal")
8 changes: 5 additions & 3 deletions quaddtype/numpy_quaddtype/src/quaddtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ static PyObject* get_sleef_constant(PyObject* self, PyObject* args) {
result->value.sleef_value = SLEEF_M_LN2q;
} else if (strcmp(constant_name, "ln10") == 0) {
result->value.sleef_value = SLEEF_M_LN10q;
} else if (strcmp(constant_name, "quad_max") == 0) {
} else if (strcmp(constant_name, "max_value") == 0) {
result->value.sleef_value = SLEEF_QUAD_MAX;
} else if (strcmp(constant_name, "quad_min") == 0) {
result->value.sleef_value = SLEEF_QUAD_MIN;
} else if (strcmp(constant_name, "epsilon") == 0) {
result->value.sleef_value = SLEEF_QUAD_EPSILON;
} else if (strcmp(constant_name, "smallest_normal") == 0) {
result->value.sleef_value = SLEEF_QUAD_MIN;
} else if (strcmp(constant_name, "smallest_subnormal") == 0) {
result->value.sleef_value = SLEEF_QUAD_DENORM_MIN;
}
else {
PyErr_SetString(PyExc_ValueError, "Unknown constant name");
Expand Down
16 changes: 14 additions & 2 deletions quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import operator

import numpy_quaddtype
from numpy_quaddtype import QuadPrecDType, QuadPrecision


Expand All @@ -11,6 +12,17 @@ def test_create_scalar_simple():
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(
Expand Down Expand Up @@ -140,12 +152,12 @@ def test_inf():
def test_dtype_creation():
dtype = QuadPrecDType()
assert isinstance(dtype, np.dtype)
assert dtype.name == 'QuadPrecDType128'
assert dtype.name == "QuadPrecDType128"


def test_array_creation():
arr = np.array([1, 2, 3], dtype=QuadPrecDType())
assert arr.dtype.name == 'QuadPrecDType128'
assert arr.dtype.name == "QuadPrecDType128"
assert all(isinstance(x, QuadPrecision) for x in arr)


Expand Down