From de2b7e9f3c17892b585ea51ef622c02fea4e8f26 Mon Sep 17 00:00:00 2001 From: zimmerma Date: Sat, 21 Dec 2013 09:25:05 +0000 Subject: improve starting precision when input is tiny git-svn-id: svn://scm.gforge.inria.fr/svn/mpc/trunk@1400 211d60ee-9f03-0410-a15a-8952a2c7a4e4 --- src/sin_cos.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/sin_cos.c b/src/sin_cos.c index fe44c42..7051708 100644 --- a/src/sin_cos.c +++ b/src/sin_cos.c @@ -303,7 +303,24 @@ mpc_sin_cos (mpc_ptr rop_sin, mpc_ptr rop_cos, mpc_srcptr op, prec = 2; if (rop_sin != NULL) - prec = MPC_MAX (prec, MPC_MAX_PREC (rop_sin)); + { + mp_prec_t er, ei; + prec = MPC_MAX (prec, MPC_MAX_PREC (rop_sin)); + /* since the Taylor expansion of sin(x) at x=0 is x - x^3/6 + O(x^5), + if x <= 2^(-p), then the second term/x is about 2^(-2p)/6, thus we + need at least 2p+3 bits of precision. This is true only when x is + exactly representable in the target precision. */ + if (MPC_MAX_PREC (op) <= prec) + { + er = mpfr_get_exp (mpc_realref (op)); + ei = mpfr_get_exp (mpc_imagref (op)); + /* consider the maximal exponent only */ + er = (er < ei) ? ei : er; + if (er < 0) + if (prec < 2 * (mp_prec_t) (-er) + 3) + prec = 2 * (mp_prec_t) (-er) + 3; + } + } if (rop_cos != NULL) prec = MPC_MAX (prec, MPC_MAX_PREC (rop_cos)); -- cgit v1.2.1