Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions quaddtype/numpy_quaddtype/src/ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ quad_atanh(const Sleef_quad *op)
return Sleef_atanhq1_u10(*op);
}

static inline Sleef_quad
quad_radians(const Sleef_quad *op)
{
// radians = degrees * π / 180
static const Sleef_quad one_eighty = Sleef_strtoq("180.0", NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using string parsing here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy and accurate, sometimes conversion from int or float gives precision errors (although here it shouldn't technically)

We can also go with qutil, but it seems overkill

Sleef_quad ratio = Sleef_divq1_u05(SLEEF_M_PIq, one_eighty);
return Sleef_mulq1_u05(*op, ratio);
}

// Unary long double operations
typedef long double (*unary_op_longdouble_def)(const long double *);

Expand Down Expand Up @@ -446,6 +455,16 @@ ld_atanh(const long double *op)
return atanhl(*op);
}

static inline long double
ld_radians(const long double *op)
{
// radians = degrees * π / 180
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
return (*op) * (static_cast<long double>(M_PI) / 180.0L);
}

// Unary Quad properties
typedef npy_bool (*unary_prop_quad_def)(const Sleef_quad *);

Expand Down
6 changes: 6 additions & 0 deletions quaddtype/numpy_quaddtype/src/umath/unary_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,11 @@ init_quad_unary_ops(PyObject *numpy)
if (create_quad_unary_ufunc<quad_atanh, ld_atanh>(numpy, "arctanh") < 0) {
return -1;
}
if (create_quad_unary_ufunc<quad_radians, ld_radians>(numpy, "radians") < 0) {
return -1;
}
if (create_quad_unary_ufunc<quad_radians, ld_radians>(numpy, "deg2rad") < 0) {
return -1;
}
return 0;
}
4 changes: 2 additions & 2 deletions quaddtype/release_tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
| arccosh | ✅ | ✅ |
| arctanh | ✅ | ✅ |
| degrees | | |
| radians | | |
| deg2rad | | |
| radians | | ✅ |
| deg2rad | | ✅ |
| rad2deg | | |
| bitwise_and | | |
| bitwise_or | | |
Expand Down
41 changes: 41 additions & 0 deletions quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,3 +1905,44 @@ def test_hypot(x1, x2, expected):
else:
np.testing.assert_allclose(float(result), expected, rtol=1e-13)


@pytest.mark.parametrize("op", [np.radians, np.deg2rad])
@pytest.mark.parametrize("degrees,expected_radians", [
# Basic conversions
(0.0, 0.0),
(30.0, np.pi / 6),
(45.0, np.pi / 4),
(60.0, np.pi / 3),
(90.0, np.pi / 2),
(180.0, np.pi),
(270.0, 3 * np.pi / 2),
(360.0, 2 * np.pi),
# Negative values
(-90.0, -np.pi / 2),
(-180.0, -np.pi),
# Special values
(np.inf, np.inf),
(-np.inf, -np.inf),
(np.nan, np.nan),
# Edge cases
(0.0, 0.0),
(-0.0, -0.0),
])
def test_radians(op, degrees, expected_radians):
"""Test radians and deg2rad ufuncs convert degrees to radians"""
q_deg = QuadPrecision(degrees)
result = op(q_deg)

assert isinstance(result, QuadPrecision)

if np.isnan(expected_radians):
assert np.isnan(float(result))
elif np.isinf(expected_radians):
assert np.isinf(float(result))
if expected_radians > 0:
assert float(result) > 0
else:
assert float(result) < 0
else:
np.testing.assert_allclose(float(result), expected_radians, rtol=1e-13)

Loading