Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions quaddtype/numpy_quaddtype/src/umath/unary_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ init_quad_unary_ops(PyObject *numpy)
if (create_quad_unary_ufunc<quad_absolute, ld_absolute>(numpy, "absolute") < 0) {
return -1;
}
if (create_quad_unary_ufunc<quad_absolute, ld_absolute>(numpy, "fabs") < 0) {
return -1;
}
if (create_quad_unary_ufunc<quad_sign, ld_sign>(numpy, "sign") < 0) {
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion quaddtype/release_tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
| fmod | ✅ | ✅ |
| divmod | | |
| absolute | ✅ | ✅ |
| fabs | | |
| fabs | | ✅ |
| rint | ✅ | ✅ |
| sign | ✅ | ✅ |
| heaviside | | |
Expand Down
40 changes: 39 additions & 1 deletion quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,4 +1349,42 @@ def test_float_power_array():

assert result.dtype.name == "QuadPrecDType128"
for i in range(len(result)):
np.testing.assert_allclose(float(result[i]), expected[i], rtol=1e-13)
np.testing.assert_allclose(float(result[i]), expected[i], rtol=1e-13)


@pytest.mark.parametrize("val", [
# Positive values
"3.0", "12.5", "100.0", "1e100", "0.0",
# Negative values
"-3.0", "-12.5", "-100.0", "-1e100", "-0.0",
# Special values
"inf", "-inf", "nan", "-nan",
# Small values
"1e-100", "-1e-100"
])
def test_fabs(val):
"""
Test np.fabs ufunc for QuadPrecision dtype.
fabs computes absolute values (positive magnitude) for floating-point numbers.
It should behave identically to np.absolute for real (non-complex) types.
"""
quad_val = QuadPrecision(val)
float_val = float(val)

quad_result = np.fabs(quad_val)
float_result = np.fabs(float_val)

# Test with both scalar and array
quad_arr = np.array([quad_val], dtype=QuadPrecDType())
quad_arr_result = np.fabs(quad_arr)

# Check scalar result
np.testing.assert_array_equal(np.array(quad_result).astype(float), float_result)

# Check array result
np.testing.assert_array_equal(quad_arr_result.astype(float)[0], float_result)

# For zero results, check sign (should always be positive after fabs)
if float_result == 0.0:
assert not np.signbit(quad_result), f"fabs({val}) should not have negative sign"
assert not np.signbit(quad_arr_result[0]), f"fabs({val}) should not have negative sign"
Loading