Import mpc-1.0.1 to new vendor branch
[dragonfly.git] / contrib / mpc / src / acosh.c
1 /* mpc_acosh -- inverse hyperbolic cosine of a complex number.
2
3 Copyright (C) 2009, 2011, 2012 INRIA
4
5 This file is part of GNU MPC.
6
7 GNU MPC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/ .
19 */
20
21 #include "mpc-impl.h"
22
23 int
24 mpc_acosh (mpc_ptr rop, mpc_srcptr op, mpc_rnd_t rnd)
25 {
26   /* acosh(z) =
27       NaN + i*NaN, if z=0+i*NaN
28      -i*acos(z), if sign(Im(z)) = -
29       i*acos(z), if sign(Im(z)) = +
30       http://functions.wolfram.com/ElementaryFunctions/ArcCosh/27/02/03/01/01/
31   */
32   mpc_t a;
33   mpfr_t tmp;
34   int inex;
35
36   if (mpfr_zero_p (mpc_realref (op)) && mpfr_nan_p (mpc_imagref (op)))
37     {
38       mpfr_set_nan (mpc_realref (rop));
39       mpfr_set_nan (mpc_imagref (rop));
40       return 0;
41     }
42
43   /* Note reversal of precisions due to later multiplication by i or -i */
44   mpc_init3 (a, MPC_PREC_IM(rop), MPC_PREC_RE(rop));
45
46   if (mpfr_signbit (mpc_imagref (op)))
47     {
48       inex = mpc_acos (a, op,
49                        MPC_RND (INV_RND (MPC_RND_IM (rnd)), MPC_RND_RE (rnd)));
50
51       /* change a to -i*a, i.e., -y+i*x to x+i*y */
52       tmp[0] = mpc_realref (a)[0];
53       mpc_realref (a)[0] = mpc_imagref (a)[0];
54       mpc_imagref (a)[0] = tmp[0];
55       MPFR_CHANGE_SIGN (mpc_imagref (a));
56       inex = MPC_INEX (MPC_INEX_IM (inex), -MPC_INEX_RE (inex));
57     }
58   else
59     {
60       inex = mpc_acos (a, op,
61                        MPC_RND (MPC_RND_IM (rnd), INV_RND(MPC_RND_RE (rnd))));
62
63       /* change a to i*a, i.e., y-i*x to x+i*y */
64       tmp[0] = mpc_realref (a)[0];
65       mpc_realref (a)[0] = mpc_imagref (a)[0];
66       mpc_imagref (a)[0] = tmp[0];
67       MPFR_CHANGE_SIGN (mpc_realref (a));
68       inex = MPC_INEX (-MPC_INEX_IM (inex), MPC_INEX_RE (inex));
69     }
70
71   mpc_set (rop, a, rnd);
72
73   mpc_clear (a);
74
75   return inex;
76 }