summaryrefslogtreecommitdiff
path: root/src/mp-trigonometric.c
blob: ca19ca420fa71e18ad4d6eb61b038739c8cb751a (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
 * Copyright (C) 2008-2011 Robert Ancell.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <libintl.h>

#include "mp.h"

/* Convert x to radians */
void
convert_to_radians(const MPNumber *x, MPAngleUnit unit, MPNumber *z)
{
    int i;

    switch(unit) {
    default:
    case MP_RADIANS:
        mp_set_from_mp(x, z);
        return;

    case MP_DEGREES:
        i = 180;
        break;

    case MP_GRADIANS:
        i = 200;
        break;
    }
    mpfr_t scale;
    mpfr_init2(scale, PRECISION);
    mpfr_const_pi(scale, MPFR_RNDN);
    mpfr_div_si(scale, scale, i, MPFR_RNDN);
    mpc_mul_fr(z->num, x->num, scale, MPC_RNDNN);
    mpfr_clear(scale);
}

void
convert_from_radians(const MPNumber *x, MPAngleUnit unit, MPNumber *z)
{
    int i;

    switch(unit) {
    default:
    case MP_RADIANS:
        mp_set_from_mp(x, z);
        return;

    case MP_DEGREES:
        i = 180;
        break;

    case MP_GRADIANS:
        i = 200;
        break;
    }
    mpfr_t scale;
    mpfr_init2(scale, PRECISION);
    mpfr_const_pi(scale, MPFR_RNDN);
    mpfr_si_div(scale, i, scale, MPFR_RNDN);
    mpc_mul_fr(z->num, x->num, scale, MPC_RNDNN);
    mpfr_clear(scale);
}


void
mp_get_pi (MPNumber *z)
{
    mpfr_const_pi(mpc_realref(z->num), MPFR_RNDN);
    mpfr_set_zero(mpc_imagref(z->num), 0);
}


void
mp_sin(const MPNumber *x, MPAngleUnit unit, MPNumber *z)
{
    if (mp_is_complex(x))
        mp_set_from_mp(x, z);
    else
        convert_to_radians(x, unit, z);
    mpc_sin(z->num, z->num, MPC_RNDNN);    
}


void
mp_cos(const MPNumber *x, MPAngleUnit unit, MPNumber *z)
{
    if (mp_is_complex(x))
        mp_set_from_mp(x, z);
    else
        convert_to_radians(x, unit, z);
    mpc_cos(z->num, z->num, MPC_RNDNN); 
}


void
mp_tan(const MPNumber *x, MPAngleUnit unit, MPNumber *z)
{
    MPNumber x_radians = mp_new();
    MPNumber pi = mp_new();
    MPNumber t1 = mp_new();

    convert_to_radians(x, unit, &x_radians);
    mp_get_pi(&pi);
    mp_divide_integer(&pi, 2, &t1);
    mp_subtract(&x_radians, &t1, &t1);
    mp_divide(&t1, &pi, &t1);    

    if (mp_is_integer(&t1)) {
        /* Translators: Error displayed when tangent value is undefined */
        mperr(_("Tangent is undefined for angles that are multiples of π (180°) from π∕2 (90°)"));
        mp_set_from_integer(0, z);
        return;
    }

    if (mp_is_complex(x))
        mp_set_from_mp(x, z);
    else
        mp_set_from_mp(&x_radians, z);
    mpc_tan(z->num, z->num, MPC_RNDNN);
    mp_clear(&x_radians);
    mp_clear(&pi);
    mp_clear(&t1);
}


void
mp_asin(const MPNumber *x, MPAngleUnit unit, MPNumber *z)
{
    MPNumber x_max = mp_new();
    MPNumber x_min = mp_new();
    mp_set_from_integer(1, &x_max);
    mp_set_from_integer(-1, &x_min);

    if (mp_compare(x, &x_max) > 0 || mp_compare(x, &x_min) < 0)
    {
        /* Translators: Error displayed when inverse sine value is undefined */
        mperr(_("Inverse sine is undefined for values outside [-1, 1]"));
        mp_set_from_integer(0, z);
        return;
    }
    mpc_asin(z->num, x->num, MPC_RNDNN);
    if (!mp_is_complex(z))
        convert_from_radians(z, unit, z);
    mp_clear(&x_max);
    mp_clear(&x_min);
}


void
mp_acos(const MPNumber *x, MPAngleUnit unit, MPNumber *z)
{
    MPNumber x_max = mp_new();
    MPNumber x_min = mp_new();
    mp_set_from_integer(1, &x_max);
    mp_set_from_integer(-1, &x_min);

    if (mp_compare(x, &x_max) > 0 || mp_compare(x, &x_min) < 0)
    {
        /* Translators: Error displayed when inverse sine value is undefined */
        mperr(_("Inverse cosine is undefined for values outside [-1, 1]"));
        mp_set_from_integer(0, z);
        return;
    }
    mpc_acos(z->num, x->num, MPC_RNDNN);
    if (!mp_is_complex(z))
        convert_from_radians(z, unit, z);
    mp_clear(&x_max);
    mp_clear(&x_min);
}


void
mp_atan(const MPNumber *x, MPAngleUnit unit, MPNumber *z)
{
    MPNumber i = mp_new();
    MPNumber minus_i = mp_new();
    mpc_set_si_si(i.num, 0, 1, MPC_RNDNN);
    mpc_set_si_si(minus_i.num, 0, -1, MPC_RNDNN);

    /* Check x != i and x != -i */
    if (mp_is_equal(x, &i) || mp_is_equal(x, &minus_i))
    {
        /* Translators: Error displayed when inverse sine value is undefined */
        mperr(_("Arctangent function is undefined for values i and -i"));
        mp_set_from_integer(0, z);
        return;
    }
    mpc_atan(z->num, x->num, MPC_RNDNN);
    if (!mp_is_complex(z))
        convert_from_radians(z, unit, z);
    mp_clear(&i);
    mp_clear(&minus_i);
}


void
mp_sinh(const MPNumber *x, MPNumber *z)
{
    mpc_sinh(z->num, x->num, MPC_RNDNN);
}


void
mp_cosh(const MPNumber *x, MPNumber *z)
{
    mpc_cosh(z->num, x->num, MPC_RNDNN);
}


void
mp_tanh(const MPNumber *x, MPNumber *z)
{
    mpc_tanh(z->num, x->num, MPC_RNDNN);
}


void
mp_asinh(const MPNumber *x, MPNumber *z)
{
    mpc_asinh(z->num, x->num, MPC_RNDNN);
}


void
mp_acosh(const MPNumber *x, MPNumber *z)
{
    MPNumber t = mp_new();

    /* Check x >= 1 */
    mp_set_from_integer(1, &t);
    if (mp_is_less_than(x, &t))
    {
        /* Translators: Error displayed when inverse hyperbolic cosine value is undefined */
        mperr(_("Inverse hyperbolic cosine is undefined for values less than one"));
        mp_set_from_integer(0, z);
        return;
    }

    mpc_acosh(z->num, x->num, MPC_RNDNN);
    mp_clear(&t);
}


void
mp_atanh(const MPNumber *x, MPNumber *z)
{
    MPNumber x_max = mp_new();
    MPNumber x_min = mp_new();
    mp_set_from_integer(1, &x_max);
    mp_set_from_integer(-1, &x_min);

    if (mp_compare(x, &x_max) >= 0 || mp_compare(x, &x_min) <= 0)
    {
         /* Translators: Error displayed when inverse hyperbolic tangent value is undefined */
        mperr(_("Inverse hyperbolic tangent is undefined for values outside (-1, 1)"));
        mp_set_from_integer(0, z);
        return;
    }
    mpc_atanh(z->num, x->num, MPC_RNDNN);
    mp_clear(&x_max);
    mp_clear(&x_min);
}