forked from libtom/libtomfloat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpf_cosh.c
78 lines (73 loc) · 1.91 KB
/
mpf_cosh.c
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
#include <tomfloat.h>
int mpf_cosh(mp_float * a, mp_float * b)
{
mp_float x, t1, t2;
int err, sign;
long size;
if (mpf_iszero(a)) {
return mpf_const_0(b);
}
err = MP_OKAY;
// check size of input (sinh grows rapidly!)
// global limit is at about +/- 1e11 but YMMV and if it varies: please
// contact the author of this file with the machine data
size = a->radix + a->exp;
if (size > 0) {
// upper limit of exp(x) which is about 2.1144e43,429,448,190
if (size / 3.3 > 1e11) {
// raise invalid (TLOSS in SysV)?
// feraiseexcept(FE_INVALID)
// mpf_errno = E_DOM
// or inexact?
if ((err = mpf_const_nan(b)) != MP_OKAY) {
return err;
}
return MP_VAL;
}
// use (exp(x) + exp(-x))/2 otherwise
if ((err = mpf_init_multi(a->radix, &t1, &t2, NULL)) != MP_OKAY) {
return err;
}
if ((err = mpf_exp(a, &t1)) != MP_OKAY) {
return err;
}
// The limit is much smaller, actually, but to be on the safer side...
if (size <= a->radix) {
a->mantissa.sign = (a->mantissa.sign == MP_NEG) ? MP_ZPOS : MP_NEG;
if ((err = mpf_exp(a, &t2)) != MP_OKAY) {
a->mantissa.sign =
(a->mantissa.sign == MP_NEG) ? MP_ZPOS : MP_NEG;
goto _SERR;
}
a->mantissa.sign = (a->mantissa.sign == MP_NEG) ? MP_ZPOS : MP_NEG;
if ((err = mpf_add(&t1, &t2, &t1)) != MP_OKAY) {
goto _SERR;
}
}
t1.exp -= 1;
t1.mantissa.sign = a->mantissa.sign;
if ((err = mpf_copy(&t1, b)) != MP_OKAY) {
goto _SERR;
}
_SERR:
mpf_clear_multi(&t1, &t2, NULL);
return err;
}
sign = a->mantissa.sign;
if ((err = mpf_init(&x, a->radix)) != MP_OKAY) {
return err;
}
if ((err = mpf_abs(a, &x)) != MP_OKAY) {
return err;
}
if ((err = mpf_sincos(&x, &x, 1, 0, 1)) != MP_OKAY) {
goto _ERR;
}
x.mantissa.sign = sign;
if ((err = mpf_copy(&x, b)) != MP_OKAY) {
goto _ERR;
}
_ERR:
mpf_clear(&x);
return err;
}