Skip to content

Commit e73f34f

Browse files
authored
Merge pull request #180 from SwayamInSync/fabs
2 parents df10dd7 + 7bde4c6 commit e73f34f

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

quaddtype/numpy_quaddtype/src/umath/unary_ops.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ init_quad_unary_ops(PyObject *numpy)
156156
if (create_quad_unary_ufunc<quad_absolute, ld_absolute>(numpy, "absolute") < 0) {
157157
return -1;
158158
}
159+
// fabs is simialr to absolute, just not handles complex values (we neither)
160+
// registering the absolute here
161+
if (create_quad_unary_ufunc<quad_absolute, ld_absolute>(numpy, "fabs") < 0) {
162+
return -1;
163+
}
159164
if (create_quad_unary_ufunc<quad_sign, ld_sign>(numpy, "sign") < 0) {
160165
return -1;
161166
}

quaddtype/release_tracker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
| fmod |||
2424
| divmod |||
2525
| absolute |||
26-
| fabs | | |
26+
| fabs | | |
2727
| rint |||
2828
| sign |||
2929
| heaviside | | |

quaddtype/tests/test_quaddtype.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,4 +1552,42 @@ def test_float_power_array():
15521552

15531553
assert result.dtype.name == "QuadPrecDType128"
15541554
for i in range(len(result)):
1555-
np.testing.assert_allclose(float(result[i]), expected[i], rtol=1e-13)
1555+
np.testing.assert_allclose(float(result[i]), expected[i], rtol=1e-13)
1556+
1557+
1558+
@pytest.mark.parametrize("val", [
1559+
# Positive values
1560+
"3.0", "12.5", "100.0", "1e100", "0.0",
1561+
# Negative values
1562+
"-3.0", "-12.5", "-100.0", "-1e100", "-0.0",
1563+
# Special values
1564+
"inf", "-inf", "nan", "-nan",
1565+
# Small values
1566+
"1e-100", "-1e-100"
1567+
])
1568+
def test_fabs(val):
1569+
"""
1570+
Test np.fabs ufunc for QuadPrecision dtype.
1571+
fabs computes absolute values (positive magnitude) for floating-point numbers.
1572+
It should behave identically to np.absolute for real (non-complex) types.
1573+
"""
1574+
quad_val = QuadPrecision(val)
1575+
float_val = float(val)
1576+
1577+
quad_result = np.fabs(quad_val)
1578+
float_result = np.fabs(float_val)
1579+
1580+
# Test with both scalar and array
1581+
quad_arr = np.array([quad_val], dtype=QuadPrecDType())
1582+
quad_arr_result = np.fabs(quad_arr)
1583+
1584+
# Check scalar result
1585+
np.testing.assert_array_equal(np.array(quad_result).astype(float), float_result)
1586+
1587+
# Check array result
1588+
np.testing.assert_array_equal(quad_arr_result.astype(float)[0], float_result)
1589+
1590+
# For zero results, check sign (should always be positive after fabs)
1591+
if float_result == 0.0:
1592+
assert not np.signbit(quad_result), f"fabs({val}) should not have negative sign"
1593+
assert not np.signbit(quad_arr_result[0]), f"fabs({val}) should not have negative sign"

0 commit comments

Comments
 (0)