forked from libtom/libtomfloat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpf_cos.c
76 lines (72 loc) · 1.63 KB
/
mpf_cos.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
/* LibTomFloat, multiple-precision floating-point library
*
* LibTomFloat is a library that provides multiple-precision
* floating-point artihmetic as well as trigonometric functionality.
*
* This library requires the public domain LibTomMath to be installed.
*
* This library is free for all purposes without any express
* gurantee it works
*
* Tom St Denis, [email protected], http://float.libtomcrypt.org
*/
#include <tomfloat.h>
int mpf_cos(mp_float * a, mp_float * b)
{
mp_float x;
int err, k, sign;
long size;
if (mpf_iszero(a)) {
return mpf_const_d(b,1);
}
size = a->radix + a->exp;
if (size > a->radix) {
// 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;
}
sign = a->mantissa.sign;
if ((err = mpf_init(&x, a->radix)) != MP_OKAY) {
return err;
}
if ((err = mpf_trig_arg_reduct(a, &x, &k)) != MP_OKAY) {
goto _ERR;
}
k = k % 4;
switch (k) {
case 0:
if ((err = mpf_sincos(&x, &x, 1, 0, 0)) != MP_OKAY) {
goto _ERR;
}
break;
case 1:
if ((err = mpf_sincos(&x, &x, 0, 0, 0)) != MP_OKAY) {
goto _ERR;
}
sign = (sign == MP_NEG) ? MP_ZPOS : MP_NEG;
break;
case 2:
if ((err = mpf_sincos(&x, &x, 1, 0, 0)) != MP_OKAY) {
goto _ERR;
}
sign = (sign == MP_NEG) ? MP_ZPOS : MP_NEG;
break;
default:
if ((err = mpf_sincos(&x, &x, 0, 0, 0)) != MP_OKAY) {
goto _ERR;
}
break;
}
x.mantissa.sign = sign;
if ((err = mpf_copy(&x, b)) != MP_OKAY) {
goto _ERR;
}
_ERR:
mpf_clear(&x);
return err;
}