xref: /haiku/src/libs/mapm/mapm_sin.c (revision 239222b2369c39dc52df52b0a7cdd6cc0a91bc92)
1 
2 /*
3  *  M_APM  -  mapm_sin.c
4  *
5  *  Copyright (C) 1999 - 2007   Michael C. Ring
6  *
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.
12  *
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.
17  *
18  *  This software is provided "as is" without express or implied warranty.
19  */
20 
21 /*
22  *      $Id: mapm_sin.c,v 1.17 2007/12/03 01:48:31 mike Exp $
23  *
24  *      This file contains the top level (user callable) SIN / COS / TAN
25  *	functions.
26  *
27  *      $Log: mapm_sin.c,v $
28  *      Revision 1.17  2007/12/03 01:48:31  mike
29  *      Update license
30  *
31  *      Revision 1.16  2002/11/03 21:53:47  mike
32  *      Updated function parameters to use the modern style
33  *
34  *      Revision 1.15  2001/03/25 20:59:46  mike
35  *      move PI stuff to new file
36  *
37  *      Revision 1.14  2001/02/07 19:12:14  mike
38  *      eliminate MM_skip_limit_PI_check
39  *
40  *      Revision 1.13  2000/11/18 11:05:36  mike
41  *      minor code re-arrangement in PI AGM
42  *
43  *      Revision 1.12  2000/07/11 23:34:42  mike
44  *      adjust loop break-out for AGM PI algorithm
45  *
46  *      Revision 1.11  2000/07/11 20:19:47  mike
47  *      use new algorithm to compute PI (AGM)
48  *
49  *      Revision 1.10  2000/05/21 01:07:57  mike
50  *      use _sin_cos in _tan function
51  *
52  *      Revision 1.9  2000/05/19 17:13:56  mike
53  *      use local copies of PI variables & recompute
54  *      on the fly as needed
55  *
56  *      Revision 1.8  1999/09/21 21:03:06  mike
57  *      make sure the sign of 'sin' from M_cos_to_sin is non-zero
58  *      before assigning it from the original angle.
59  *
60  *      Revision 1.7  1999/09/18 03:27:27  mike
61  *      added m_apm_sin_cos
62  *
63  *      Revision 1.6  1999/07/09 22:50:33  mike
64  *      skip limit to PI when not needed
65  *
66  *      Revision 1.5  1999/06/20 23:42:29  mike
67  *      use new function for COS function
68  *
69  *      Revision 1.4  1999/06/20 19:27:12  mike
70  *      changed local static variables to MAPM stack variables
71  *
72  *      Revision 1.3  1999/05/17 03:54:56  mike
73  *      init globals in TAN function also
74  *
75  *      Revision 1.2  1999/05/15 02:18:31  mike
76  *      add check for number of decimal places
77  *
78  *      Revision 1.1  1999/05/10 20:56:31  mike
79  *      Initial revision
80  */
81 
82 #include "m_apm_lc.h"
83 
84 /****************************************************************************/
85 void	m_apm_sin(M_APM r, int places, M_APM a)
86 {
87 M_APM	tmp3;
88 
89 tmp3 = M_get_stack_var();
90 M_limit_angle_to_pi(tmp3, (places + 6), a);
91 M_5x_sin(r, places, tmp3);
92 M_restore_stack(1);
93 }
94 /****************************************************************************/
95 void	m_apm_cos(M_APM r, int places, M_APM a)
96 {
97 M_APM	tmp3;
98 
99 tmp3 = M_get_stack_var();
100 M_limit_angle_to_pi(tmp3, (places + 6), a);
101 M_4x_cos(r, places, tmp3);
102 M_restore_stack(1);
103 }
104 /****************************************************************************/
105 void	m_apm_sin_cos(M_APM sinv, M_APM cosv, int places, M_APM aa)
106 {
107 M_APM	tmp5, tmp6, tmp7;
108 
109 tmp5 = M_get_stack_var();
110 tmp6 = M_get_stack_var();
111 tmp7 = M_get_stack_var();
112 
113 M_limit_angle_to_pi(tmp5, (places + 6), aa);
114 M_4x_cos(tmp7, (places + 6), tmp5);
115 
116 /*
117  *   compute sin(x) = sqrt(1 - cos(x) ^ 2).
118  *
119  *   note that the sign of 'sin' will always be positive after the
120  *   sqrt call. we need to adjust the sign based on what quadrant
121  *   the original angle is in.
122  */
123 
124 M_cos_to_sin(tmp6, (places + 6), tmp7);
125 if (tmp6->m_apm_sign != 0)
126   tmp6->m_apm_sign = tmp5->m_apm_sign;
127 
128 m_apm_round(sinv, places, tmp6);
129 m_apm_round(cosv, places, tmp7);
130 M_restore_stack(3);
131 }
132 /****************************************************************************/
133 void	m_apm_tan(M_APM r, int places, M_APM a)
134 {
135 M_APM	tmps, tmpc, tmp0;
136 
137 tmps = M_get_stack_var();
138 tmpc = M_get_stack_var();
139 tmp0 = M_get_stack_var();
140 
141 m_apm_sin_cos(tmps, tmpc, (places + 4), a);
142 
143 /* tan(x) = sin(x) / cos(x) */
144 
145 m_apm_divide(tmp0, (places + 4), tmps, tmpc);
146 m_apm_round(r, places, tmp0);
147 M_restore_stack(3);
148 }
149 /****************************************************************************/
150 void	M_limit_angle_to_pi(M_APM rr, int places, M_APM aa)
151 {
152 M_APM	tmp7, tmp8, tmp9;
153 
154 M_check_PI_places(places);
155 
156 tmp9 = M_get_stack_var();
157 m_apm_copy(tmp9, MM_lc_PI);
158 
159 if (m_apm_compare(aa, tmp9) == 1)       /*  > PI  */
160   {
161    tmp7 = M_get_stack_var();
162    tmp8 = M_get_stack_var();
163 
164    m_apm_add(tmp7, aa, tmp9);
165    m_apm_integer_divide(tmp9, tmp7, MM_lc_2_PI);
166    m_apm_multiply(tmp8, tmp9, MM_lc_2_PI);
167    m_apm_subtract(tmp9, aa, tmp8);
168    m_apm_round(rr, places, tmp9);
169 
170    M_restore_stack(3);
171    return;
172   }
173 
174 tmp9->m_apm_sign = -1;
175 if (m_apm_compare(aa, tmp9) == -1)       /*  < -PI  */
176   {
177    tmp7 = M_get_stack_var();
178    tmp8 = M_get_stack_var();
179 
180    m_apm_add(tmp7, aa, tmp9);
181    m_apm_integer_divide(tmp9, tmp7, MM_lc_2_PI);
182    m_apm_multiply(tmp8, tmp9, MM_lc_2_PI);
183    m_apm_subtract(tmp9, aa, tmp8);
184    m_apm_round(rr, places, tmp9);
185 
186    M_restore_stack(3);
187    return;
188   }
189 
190 m_apm_copy(rr, aa);
191 M_restore_stack(1);
192 }
193 /****************************************************************************/
194