File: atan.c

package info (click to toggle)
flint-arb 1%3A2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: ansic: 204,753; sh: 570; makefile: 287; python: 268
file content (126 lines) | stat: -rw-r--r-- 2,665 bytes parent folder | download | duplicates (3)
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
/*
    Copyright (C) 2017 Fredrik Johansson

    This file is part of Arb.

    Arb is free software: you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.  See <http://www.gnu.org/licenses/>.
*/

#include "mag.h"
#include "arb.h"  /* for atan table */

static double
mag_atan_d(double x)
{
    double t, u, v;
    int p, q, flip;

    t = x;
    flip = 0;
    if (t > 1.0)
    {
        t = 1.0 / t;
        flip = 1;
    }

    q = ldexp(1.0, ARB_ATAN_TAB1_BITS);
    p = t * (1 << ARB_ATAN_TAB1_BITS);
    if (p == (1 << ARB_ATAN_TAB1_BITS))
        p--;

#if FLINT_BITS == 64
    v = arb_atan_tab1[p][ARB_ATAN_TAB1_LIMBS - 1] * ldexp(1.0, -FLINT_BITS);
#else
    v = arb_atan_tab1[p][ARB_ATAN_TAB1_LIMBS - 1] * ldexp(1.0, -FLINT_BITS)
      + arb_atan_tab1[p][ARB_ATAN_TAB1_LIMBS - 2] * ldexp(1.0, -2 * FLINT_BITS);
#endif

    t = (q * t - p) / (p * t + q);

    /* t - t^3/3 + t^5/5 */
    u = t * t;
    t = (t * (1.0 / 15.0)) * (15.0 - 5.0 * u + 3.0 * (u * u));
    v += t;

    if (flip)
        v = 1.5707963267948966192 - v;

    return v;
}

void
mag_atan(mag_t res, const mag_t x)
{
    if (mag_is_zero(x))
    {
        mag_zero(res);
    }
    else if (mag_cmp_2exp_si(x, MAG_BITS) > 0)
    {
        mag_const_pi(res);
        mag_mul_2exp_si(res, res, -1);
    }
    else if (mag_cmp_2exp_si(x, -(MAG_BITS / 2)) < 0)
    {
        mag_set(res, x);
    }
    else
    {
        double t;

        t = ldexp(MAG_MAN(x), MAG_EXP(x) - MAG_BITS);
        t = mag_atan_d(t);
        t = t * (1.0 + 1e-12);

        mag_set_d(res, t);
    }
}

void
mag_atan_lower(mag_t res, const mag_t x)
{
    if (mag_is_zero(x))
    {
        mag_zero(res);
    }
    else if (mag_is_inf(x))
    {
        mag_const_pi_lower(res);
        mag_mul_2exp_si(res, res, -1);
    }
    else if (mag_cmp_2exp_si(x, MAG_BITS) > 0)
    {
        /* atan(x) > pi/2 - 1/x */
        mag_t t;
        mag_init(t);
        mag_one(t);
        mag_div(t, t, x);
        mag_const_pi_lower(res);
        mag_mul_2exp_si(res, res, -1);
        mag_sub_lower(res, res, t);
        mag_clear(t);
    }
    else if (mag_cmp_2exp_si(x, -MAG_BITS) < 0)
    {
        /* atan(x) > x - x^2 */
        mag_t t;
        mag_init(t);
        mag_mul(t, x, x);
        mag_sub_lower(res, x, t);
        mag_clear(t);
    }
    else
    {
        double t;

        t = ldexp(MAG_MAN(x), MAG_EXP(x) - MAG_BITS);
        t = mag_atan_d(t);
        t = t * (1.0 - 1e-12);

        mag_set_d_lower(res, t);
    }
}