5
* Copyright (C) 1999 - 2007 Michael C. Ring
7
* Permission to use, copy, and distribute this software and its
8
* documentation for any purpose with or without fee is hereby granted,
9
* provided that the above copyright notice appear in all copies and
10
* that both that copyright notice and this permission notice appear
11
* in supporting documentation.
13
* Permission to modify the software is granted. Permission to distribute
14
* the modified code is granted. Modifications are to be distributed by
15
* using the file 'license.txt' as a template to modify the file header.
16
* 'license.txt' is available in the official MAPM distribution.
18
* This software is provided "as is" without express or implied warranty.
23
* This file contains the basic series expansion functions for
24
* the SIN / COS functions.
28
#include "pgscript/utilities/mapm-lib/m_apm_lc.h"
30
/****************************************************************************/
33
sin(x) = x - --- + --- - --- + --- ...
36
void M_raw_sin(M_APM rr, int places, M_APM xx)
38
M_APM sum, term, tmp2, tmp7, tmp8;
39
int tolerance, flag, local_precision, dplaces;
42
sum = M_get_stack_var();
43
term = M_get_stack_var();
44
tmp2 = M_get_stack_var();
45
tmp7 = M_get_stack_var();
46
tmp8 = M_get_stack_var();
50
m_apm_multiply(tmp8, xx, xx);
51
m_apm_round(tmp2, (places + 6), tmp8);
53
dplaces = (places + 8) - xx->m_apm_exponent;
54
tolerance = xx->m_apm_exponent - (places + 4);
61
m_apm_multiply(tmp8, term, tmp2);
63
if ((tmp8->m_apm_exponent < tolerance) || (tmp8->m_apm_sign == 0))
66
local_precision = dplaces + term->m_apm_exponent;
68
if (local_precision < 20)
72
m_apm_set_long(tmp7, m2);
74
m_apm_divide(term, local_precision, tmp8, tmp7);
78
m_apm_subtract(tmp7, sum, term);
79
m_apm_copy(sum, tmp7);
83
m_apm_add(tmp7, sum, term);
84
m_apm_copy(sum, tmp7);
91
m_apm_round(rr, places, sum);
94
/****************************************************************************/
97
cos(x) = 1 - --- + --- - --- + --- ...
100
void M_raw_cos(M_APM rr, int places, M_APM xx)
102
M_APM sum, term, tmp7, tmp8, tmp9;
103
int tolerance, flag, local_precision, prev_exp;
106
sum = M_get_stack_var();
107
term = M_get_stack_var();
108
tmp7 = M_get_stack_var();
109
tmp8 = M_get_stack_var();
110
tmp9 = M_get_stack_var();
112
m_apm_copy(sum, MM_One);
113
m_apm_copy(term, MM_One);
115
m_apm_multiply(tmp8, xx, xx);
116
m_apm_round(tmp9, (places + 6), tmp8);
118
local_precision = places + 8;
119
tolerance = -(places + 4);
128
m_apm_set_long(tmp7, m2);
130
m_apm_multiply(tmp8, term, tmp9);
131
m_apm_divide(term, local_precision, tmp8, tmp7);
135
m_apm_subtract(tmp7, sum, term);
136
m_apm_copy(sum, tmp7);
140
m_apm_add(tmp7, sum, term);
141
m_apm_copy(sum, tmp7);
144
if ((term->m_apm_exponent < tolerance) || (term->m_apm_sign == 0))
149
local_precision = local_precision + term->m_apm_exponent - prev_exp;
151
if (local_precision < 20)
152
local_precision = 20;
155
prev_exp = term->m_apm_exponent;
161
m_apm_round(rr, places, sum);
164
/****************************************************************************/