summaryrefslogtreecommitdiff
path: root/src/atan.c
blob: b394b28c2e5bb023e16cc6d3a6e20d3878e8712c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* mpc_atan -- arctangent of a complex number.

Copyright (C) 2009 Philippe Th\'eveny

This file is part of the MPC Library.

The MPC Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The MPC Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the MPC Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include "mpc-impl.h"

/* set rop to
   -pi/2 if s < 0
   +pi/2 else
   rounded in the direction rnd
*/
static int
set_pi_over_2 (mpfr_ptr rop, int s, mpfr_rnd_t rnd)
{
  int inex;

  inex = mpfr_const_pi (rop, s ? INV_RND (rnd) : rnd);
  mpfr_div_2ui (rop, rop, 1, GMP_RNDN);
  if (s)
    {
      inex = -inex;
      mpfr_neg (rop, rop, GMP_RNDN);
    }

  return inex;
}

int
mpc_atan (mpc_ptr rop, mpc_srcptr op, mpc_rnd_t rnd)
{
  int s_re;
  int s_im;

  s_re = mpfr_signbit (MPC_RE (op));
  s_im = mpfr_signbit (MPC_IM (op));

  /* special values */
  if (mpfr_nan_p (MPC_RE (op)) || mpfr_nan_p (MPC_IM (op)))
    {
      int inex_re;
      inex_re = 0;

      if (mpfr_nan_p (MPC_RE (op)))
        {
          mpfr_set_nan (MPC_RE (rop));
          if (mpfr_zero_p (MPC_IM (op)) || mpfr_inf_p (MPC_IM (op)))
            {
              mpfr_set_ui (MPC_IM (rop), 0, GMP_RNDN);
              if (s_im)
                mpc_conj (rop, rop, MPC_RNDNN);
            }
          else
            mpfr_set_nan (MPC_IM (rop));
        }
      else
        {
          if (mpfr_inf_p (MPC_RE (op)))
            {
              inex_re = set_pi_over_2 (MPC_RE (rop), s_re, MPC_RND_RE (rnd));
              mpfr_set_ui (MPC_IM (rop), 0, GMP_RNDN);
            }
          else
            {
              mpfr_set_nan (MPC_RE (rop));
              mpfr_set_nan (MPC_IM (rop));
            }
        }
      return MPC_INEX (inex_re, 0);
    }

  if (mpfr_inf_p (MPC_RE (op)) || mpfr_inf_p (MPC_IM (op)))
    {
      int inex_re;
      inex_re = set_pi_over_2 (MPC_RE (rop), s_re, MPC_RND_RE (rnd));

      mpfr_set_ui (MPC_IM (rop), 0, GMP_RNDN);
      if (s_im)
        mpc_conj (rop, rop, GMP_RNDN);

      return MPC_INEX (inex_re, 0);
    }

  /* pure real argument */
  if (mpfr_zero_p (MPC_IM (op)))
    {
      int inex_re;

      inex_re = mpfr_atan (MPC_RE (rop), MPC_RE (op), MPC_RND_RE (rnd));
      
      mpfr_set_ui (MPC_IM (rop), 0, GMP_RNDN);
      if (s_im)
        mpc_conj (rop, rop, GMP_RNDN);

      return MPC_INEX (inex_re, 0);
    }

  /* pure imaginary argument */
  if (mpfr_zero_p (MPC_RE (op)))
    {
      int inex_re;
      int inex_im;
      int cmp_1;

      inex_re = 0;
      inex_im = 0;

      if (s_im)
        cmp_1 = -mpfr_cmp_si (MPC_IM (op), -1);
      else
        cmp_1 = mpfr_cmp_ui (MPC_IM (op), +1);

      if (cmp_1 < 0)
        {
          /* atan(+0+iy) = +0 +i*atanh(y), if |y| < 1
             atan(-0+iy) = -0 +i*atanh(y), if |y| < 1 */

          mpfr_set_ui (MPC_RE (rop), 0, GMP_RNDN);
          if (s_re)
            mpfr_neg (MPC_RE (rop), MPC_RE (rop), GMP_RNDN);

          inex_im = mpfr_atanh (MPC_IM (rop), MPC_IM (op), MPC_RND_IM (rnd));
        }
      else if (cmp_1 == 0)
        {
          /* atan(+/-0+i) = NaN +i*inf
             atan(+/-0-i) = NaN -i*inf */
          mpfr_set_nan (MPC_RE (rop));
          mpfr_set_inf (MPC_IM (rop), s_im ? -1 : +1);
        }
      else
        {
          /* atan(+0+iy) = +pi/2 +i*atanh(1/y), if |y| > 1
             atan(-0+iy) = -pi/2 +i*atanh(1/y), if |y| > 1 */
          mp_rnd_t rnd_im;
          mpfr_t y;
          mp_prec_t p, p_im;
          mp_prec_t err;
          int ok;
          int cmp_2;

          rnd_im = MPC_RND_IM (rnd);
          mpfr_init (y);
          p_im = mpfr_get_prec (MPC_IM (op));
          p = p_im;

          /* a = o(1/y)      with error(a) < 1 ulp(a)
             b = o(atanh(a)) with error(b) < (1+2^{1+Exp(a)-Exp(b)}) ulp(b)

             * if 1 < |y| <= 2 then Exp(a) = 0 and Exp(b) >= 0, so, at most, 2
               bits of precision are lost.
             * if |y| > 2, no simplification. */
          if (s_im)
            cmp_2 = -mpfr_cmp_si (MPC_IM (op), -2);
          else
            cmp_2 = mpfr_cmp_ui (MPC_IM (op), +2);
          
          err = 2;

          do
            {
              p += mpc_ceil_log2 (p) + err;
              mpfr_set_prec (y, p);
              inex_im = mpfr_ui_div (y, 1, MPC_IM (op), GMP_RNDZ);
              if (mpfr_zero_p (y))
                break; /* underflow. */
              /* FIXME: should we consider the case with unreasonably huge
                 precision prec(y)>3*exp_min, where atanh(1/Im(op)) could be
                 representable while 1/Im(op) underflows ? */
              if (cmp_2 > 0)
                err = 2 + (mp_prec_t)MPC_MAX (MPFR_EXP (y), 0);

              /* atanh cannot underflow: |atanh(x)| > |x| for |x| < 1 */
              inex_im |= mpfr_atanh (y, y, GMP_RNDZ);
              if (cmp_2 > 0)
                err += MPC_MAX (SAFE_ABS (mp_prec_t, MPFR_EXP (y)), 0);

              ok = inex_im == 0 
                || mpfr_can_round (y, p-err, GMP_RNDZ, rnd_im,
                                   p_im + (rnd_im == GMP_RNDN));
            } while (ok == 0);

          inex_re = set_pi_over_2 (MPC_RE (rop), s_re, MPC_RND_RE (rnd));
          inex_im = mpfr_set (MPC_IM (rop), y, rnd_im);
          mpfr_clear (y);
        }
      return MPC_INEX (inex_re, inex_im);
    }

  /* regular number argument */

  /* TODO */
  mpfr_set_nan (MPC_RE (rop));
  mpfr_set_nan (MPC_IM (rop));

  return 0;
}