diff --git a/mpfdtype/mpfdtype/src/ops.hpp b/mpfdtype/mpfdtype/src/ops.hpp index b967c588..c8808922 100644 --- a/mpfdtype/mpfdtype/src/ops.hpp +++ b/mpfdtype/mpfdtype/src/ops.hpp @@ -56,13 +56,13 @@ absolute(mpfr_t op, mpfr_t out) static inline int sqrt(mpfr_t op, mpfr_t out) { - return mpfr_pow_si(out, op, 2, MPFR_RNDN); + return mpfr_sqrt(out, op, MPFR_RNDN); } static inline int square(mpfr_t op, mpfr_t out) { - return mpfr_sqrt(out, op, MPFR_RNDN); + return mpfr_pow_si(out, op, 2, MPFR_RNDN); } static inline int @@ -140,7 +140,7 @@ arccos(mpfr_t op, mpfr_t out) static inline int arctan(mpfr_t op, mpfr_t out) { - return mpfr_tan(out, op, MPFR_RNDN); + return mpfr_atan(out, op, MPFR_RNDN); } diff --git a/mpfdtype/mpfdtype/src/umath.cpp b/mpfdtype/mpfdtype/src/umath.cpp index 65ee93c1..833ea248 100644 --- a/mpfdtype/mpfdtype/src/umath.cpp +++ b/mpfdtype/mpfdtype/src/umath.cpp @@ -176,12 +176,21 @@ int init_unary_ops(PyObject *numpy) if (create_unary_ufunc(numpy, "exp") < 0) { return -1; } - if (create_unary_ufunc(numpy, "exp2") < 0) { + if (create_unary_ufunc(numpy, "exp2") < 0) { return -1; } if (create_unary_ufunc(numpy, "expm1") < 0) { return -1; } + if (create_unary_ufunc(numpy, "sin") < 0) { + return -1; + } + if (create_unary_ufunc(numpy, "cos") < 0) { + return -1; + } + if (create_unary_ufunc(numpy, "tan") < 0) { + return -1; + } if (create_unary_ufunc(numpy, "arcsin") < 0) { return -1; } diff --git a/mpfdtype/mpfdtype/tests/test_scalar.py b/mpfdtype/mpfdtype/tests/test_scalar.py index 996de3c4..bfd4f903 100644 --- a/mpfdtype/mpfdtype/tests/test_scalar.py +++ b/mpfdtype/mpfdtype/tests/test_scalar.py @@ -39,6 +39,23 @@ def test_scalar_repr(val): expected = f"MPFloat('{val_repr}', prec=20)" assert repr(MPFloat(val, prec=20)) == expected +@pytest.mark.parametrize("op", + ["negative", "positive", "absolute", + "rint", "trunc", "floor", "ceil", + "sqrt", "square", + "log", "log2", "log10", "log1p", + "exp", "exp2", "expm1", + "sin", "cos", "tan", + "arcsin", "arccos", "arctan"]) +@pytest.mark.parametrize("val", [0.5, 3., 12.5, 100., np.nan, np.inf]) +def test_unary_ops(op, val): + op = getattr(np, op) + expected = op(val) + if np.isnan(expected): + assert op(MPFloat(val)) != op(MPFloat(val)) + else: + assert op(MPFloat(val)) == expected + @pytest.mark.parametrize("op", ["add", "sub", "mul", "pow"]) @pytest.mark.parametrize("other", [3., 12.5, 100., np.nan, np.inf])