Skip to content

Implement the fmin and fmax ufuncs #123

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

Merged
merged 2 commits into from
Jul 27, 2025
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
32 changes: 30 additions & 2 deletions quaddtype/numpy_quaddtype/src/ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,22 @@ quad_maximum(const Sleef_quad *in1, const Sleef_quad *in2)
) : Sleef_icmpgeq1(*in1, *in2) ? *in1 : *in2;
}

static inline Sleef_quad
quad_fmin(const Sleef_quad *in1, const Sleef_quad *in2)
{
return Sleef_iunordq1(*in1, *in2) ? (
Sleef_iunordq1(*in2, *in2) ? *in1 : *in2
) : Sleef_icmpleq1(*in1, *in2) ? *in1 : *in2;
}

static inline Sleef_quad
quad_fmax(const Sleef_quad *in1, const Sleef_quad *in2)
{
return Sleef_iunordq1(*in1, *in2) ? (
Sleef_iunordq1(*in2, *in2) ? *in1 : *in2
) : Sleef_icmpgeq1(*in1, *in2) ? *in1 : *in2;
}

static inline Sleef_quad
quad_atan2(const Sleef_quad *in1, const Sleef_quad *in2)
{
Expand Down Expand Up @@ -452,13 +468,25 @@ ld_mod(const long double *a, const long double *b)
static inline long double
ld_minimum(const long double *in1, const long double *in2)
{
return (*in1 < *in2) ? *in1 : *in2;
return isnan(*in1) ? *in1 : (*in1 < *in2) ? *in1 : *in2;
}

static inline long double
ld_maximum(const long double *in1, const long double *in2)
{
return (*in1 > *in2) ? *in1 : *in2;
return isnan(*in1) ? *in1 : (*in1 > *in2) ? *in1 : *in2;
}

static inline long double
ld_fmin(const long double *in1, const long double *in2)
{
return fmin(*in1, *in2);
}

static inline long double
ld_fmax(const long double *in1, const long double *in2)
{
return fmax(*in1, *in2);
}

static inline long double
Expand Down
6 changes: 6 additions & 0 deletions quaddtype/numpy_quaddtype/src/umath/binary_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ init_quad_binary_ops(PyObject *numpy)
if (create_quad_binary_ufunc<quad_maximum, ld_maximum>(numpy, "maximum") < 0) {
return -1;
}
if (create_quad_binary_ufunc<quad_fmin, ld_fmin>(numpy, "fmin") < 0) {
return -1;
}
if (create_quad_binary_ufunc<quad_fmax, ld_fmax>(numpy, "fmax") < 0) {
return -1;
}
if (create_quad_binary_ufunc<quad_atan2, ld_atan2>(numpy, "arctan2") < 0) {
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions quaddtype/release_tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
| logical_not | | |
| maximum | ✅ | ✅ |
| minimum | ✅ | ✅ |
| fmax | | |
| fmin | | |
| fmax | #123 | ✅ |
| fmin | #123 | ✅ |
| isfinite | #121 | ✅ |
| isinf | #121 | ✅ |
| isnan | #221 | ✅ |
Expand Down
6 changes: 0 additions & 6 deletions quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ def test_array_comparisons(op, a, b):
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_array_minmax(op, a, b):
if op in ["fmin", "fmax"]:
pytest.skip("fmin and fmax ufuncs are not yet supported")

op_func = getattr(np, op)
quad_a = np.array([QuadPrecision(a)])
quad_b = np.array([QuadPrecision(b)])
Expand All @@ -96,9 +93,6 @@ def test_array_minmax(op, a, b):
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_array_aminmax(op, a, b):
if op in ["nanmin", "nanmax"]:
pytest.skip("fmin and fmax ufuncs are not yet supported")

op_func = getattr(np, op)
quad_ab = np.array([QuadPrecision(a), QuadPrecision(b)])
float_ab = np.array([float(a), float(b)])
Expand Down
Loading