source: mainline/uspace/lib/math/generic/trig.c@ e6f5766

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e6f5766 was 992ffa6, checked in by Jiri Svoboda <jiri@…>, 10 years ago

Add exp(f), log(f), pow(f). Improve precision of sin, cos.

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2014 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libmath
30 * @{
31 */
32/** @file
33 */
34
35#include <math.h>
36#include <trig.h>
37
38#define TAYLOR_DEGREE_32 13
39#define TAYLOR_DEGREE_64 21
40
41/** Precomputed values for factorial (starting from 1!) */
42static float64_t factorials[TAYLOR_DEGREE_64] = {
43 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800,
44 479001600, 6227020800.0L, 87178291200.0L, 1307674368000.0L,
45 20922789888000.0L, 355687428096000.0L, 6402373705728000.0L,
46 121645100408832000.0L, 2432902008176640000.0L, 51090942171709440000.0L
47};
48
49/** Sine approximation by Taylor series
50 *
51 * Compute the approximation of sine by a Taylor
52 * series (using the first TAYLOR_DEGREE terms).
53 * The approximation is reasonably accurate for
54 * arguments within the interval [-pi/4, pi/4].
55 *
56 * @param arg Sine argument.
57 *
58 * @return Sine value approximation.
59 *
60 */
61static float64_t taylor_sin(float64_t arg)
62{
63 float64_t ret = 0;
64 float64_t nom = 1;
65
66 for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) {
67 nom *= arg;
68
69 if ((i % 4) == 0)
70 ret += nom / factorials[i];
71 else if ((i % 4) == 2)
72 ret -= nom / factorials[i];
73 }
74
75 return ret;
76}
77
78/** Cosine approximation by Taylor series
79 *
80 * Compute the approximation of cosine by a Taylor
81 * series (using the first TAYLOR_DEGREE terms).
82 * The approximation is reasonably accurate for
83 * arguments within the interval [-pi/4, pi/4].
84 *
85 * @param arg Cosine argument.
86 *
87 * @return Cosine value approximation.
88 *
89 */
90static float64_t taylor_cos(float64_t arg)
91{
92 float64_t ret = 1;
93 float64_t nom = 1;
94
95 for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) {
96 nom *= arg;
97
98 if ((i % 4) == 1)
99 ret -= nom / factorials[i];
100 else if ((i % 4) == 3)
101 ret += nom / factorials[i];
102 }
103
104 return ret;
105}
106
107/** Sine value for values within base period
108 *
109 * Compute the value of sine for arguments within
110 * the base period [0, 2pi]. For arguments outside
111 * the base period the returned values can be
112 * very inaccurate or even completely wrong.
113 *
114 * @param arg Sine argument.
115 *
116 * @return Sine value.
117 *
118 */
119static float64_t base_sin(float64_t arg)
120{
121 unsigned int period = arg / (M_PI / 4);
122
123 switch (period) {
124 case 0:
125 return taylor_sin(arg);
126 case 1:
127 case 2:
128 return taylor_cos(arg - M_PI / 2);
129 case 3:
130 case 4:
131 return -taylor_sin(arg - M_PI);
132 case 5:
133 case 6:
134 return -taylor_cos(arg - 3 * M_PI / 2);
135 default:
136 return taylor_sin(arg - 2 * M_PI);
137 }
138}
139
140/** Cosine value for values within base period
141 *
142 * Compute the value of cosine for arguments within
143 * the base period [0, 2pi]. For arguments outside
144 * the base period the returned values can be
145 * very inaccurate or even completely wrong.
146 *
147 * @param arg Cosine argument.
148 *
149 * @return Cosine value.
150 *
151 */
152static float64_t base_cos(float64_t arg)
153{
154 unsigned int period = arg / (M_PI / 4);
155
156 switch (period) {
157 case 0:
158 return taylor_cos(arg);
159 case 1:
160 case 2:
161 return -taylor_sin(arg - M_PI / 2);
162 case 3:
163 case 4:
164 return -taylor_cos(arg - M_PI);
165 case 5:
166 case 6:
167 return taylor_sin(arg - 3 * M_PI / 2);
168 default:
169 return taylor_cos(arg - 2 * M_PI);
170 }
171}
172
173/** Double precision sine
174 *
175 * Compute sine value.
176 *
177 * @param arg Sine argument.
178 *
179 * @return Sine value.
180 *
181 */
182float64_t float64_sin(float64_t arg)
183{
184 float64_t base_arg = fmod(arg, 2 * M_PI);
185
186 if (base_arg < 0)
187 return -base_sin(-base_arg);
188
189 return base_sin(base_arg);
190}
191
192/** Double precision cosine
193 *
194 * Compute cosine value.
195 *
196 * @param arg Cosine argument.
197 *
198 * @return Cosine value.
199 *
200 */
201float64_t float64_cos(float64_t arg)
202{
203 float64_t base_arg = fmod(arg, 2 * M_PI);
204
205 if (base_arg < 0)
206 return base_cos(-base_arg);
207
208 return base_cos(base_arg);
209}
210
211/** @}
212 */
Note: See TracBrowser for help on using the repository browser.