[ba11ebb] | 1 | /*
|
---|
[9adb61d] | 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
[ba11ebb] | 3 | * Copyright (c) 2014 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libmath
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <math.h>
|
---|
| 37 |
|
---|
[992ffa6] | 38 | #define TAYLOR_DEGREE_32 13
|
---|
| 39 | #define TAYLOR_DEGREE_64 21
|
---|
[ba11ebb] | 40 |
|
---|
| 41 | /** Precomputed values for factorial (starting from 1!) */
|
---|
[516e780] | 42 | static double factorials[TAYLOR_DEGREE_64] = {
|
---|
[ba11ebb] | 43 | 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800,
|
---|
[992ffa6] | 44 | 479001600, 6227020800.0L, 87178291200.0L, 1307674368000.0L,
|
---|
| 45 | 20922789888000.0L, 355687428096000.0L, 6402373705728000.0L,
|
---|
| 46 | 121645100408832000.0L, 2432902008176640000.0L, 51090942171709440000.0L
|
---|
[ba11ebb] | 47 | };
|
---|
| 48 |
|
---|
[9adb61d] | 49 | /** Sine approximation by Taylor series (32-bit floating point)
|
---|
[ba11ebb] | 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 | */
|
---|
[516e780] | 61 | static float taylor_sin_32(float arg)
|
---|
[9adb61d] | 62 | {
|
---|
[516e780] | 63 | float ret = 0;
|
---|
| 64 | float nom = 1;
|
---|
[a35b458] | 65 |
|
---|
[9adb61d] | 66 | for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) {
|
---|
| 67 | nom *= arg;
|
---|
[a35b458] | 68 |
|
---|
[9adb61d] | 69 | if ((i % 4) == 0)
|
---|
| 70 | ret += nom / factorials[i];
|
---|
| 71 | else if ((i % 4) == 2)
|
---|
| 72 | ret -= nom / factorials[i];
|
---|
| 73 | }
|
---|
[a35b458] | 74 |
|
---|
[9adb61d] | 75 | return ret;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /** Sine approximation by Taylor series (64-bit floating point)
|
---|
| 79 | *
|
---|
| 80 | * Compute the approximation of sine 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 Sine argument.
|
---|
| 86 | *
|
---|
| 87 | * @return Sine value approximation.
|
---|
| 88 | *
|
---|
| 89 | */
|
---|
[516e780] | 90 | static double taylor_sin_64(double arg)
|
---|
[ba11ebb] | 91 | {
|
---|
[516e780] | 92 | double ret = 0;
|
---|
| 93 | double nom = 1;
|
---|
[a35b458] | 94 |
|
---|
[992ffa6] | 95 | for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) {
|
---|
[ba11ebb] | 96 | nom *= arg;
|
---|
[a35b458] | 97 |
|
---|
[ba11ebb] | 98 | if ((i % 4) == 0)
|
---|
| 99 | ret += nom / factorials[i];
|
---|
| 100 | else if ((i % 4) == 2)
|
---|
| 101 | ret -= nom / factorials[i];
|
---|
| 102 | }
|
---|
[a35b458] | 103 |
|
---|
[ba11ebb] | 104 | return ret;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[9adb61d] | 107 | /** Cosine approximation by Taylor series (32-bit floating point)
|
---|
| 108 | *
|
---|
| 109 | * Compute the approximation of cosine by a Taylor
|
---|
| 110 | * series (using the first TAYLOR_DEGREE terms).
|
---|
| 111 | * The approximation is reasonably accurate for
|
---|
| 112 | * arguments within the interval [-pi/4, pi/4].
|
---|
| 113 | *
|
---|
| 114 | * @param arg Cosine argument.
|
---|
| 115 | *
|
---|
| 116 | * @return Cosine value approximation.
|
---|
| 117 | *
|
---|
| 118 | */
|
---|
[516e780] | 119 | static float taylor_cos_32(float arg)
|
---|
[9adb61d] | 120 | {
|
---|
[516e780] | 121 | float ret = 1;
|
---|
| 122 | float nom = 1;
|
---|
[a35b458] | 123 |
|
---|
[9adb61d] | 124 | for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) {
|
---|
| 125 | nom *= arg;
|
---|
[a35b458] | 126 |
|
---|
[9adb61d] | 127 | if ((i % 4) == 1)
|
---|
| 128 | ret -= nom / factorials[i];
|
---|
| 129 | else if ((i % 4) == 3)
|
---|
| 130 | ret += nom / factorials[i];
|
---|
| 131 | }
|
---|
[a35b458] | 132 |
|
---|
[9adb61d] | 133 | return ret;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /** Cosine approximation by Taylor series (64-bit floating point)
|
---|
[ba11ebb] | 137 | *
|
---|
| 138 | * Compute the approximation of cosine by a Taylor
|
---|
| 139 | * series (using the first TAYLOR_DEGREE terms).
|
---|
| 140 | * The approximation is reasonably accurate for
|
---|
| 141 | * arguments within the interval [-pi/4, pi/4].
|
---|
| 142 | *
|
---|
| 143 | * @param arg Cosine argument.
|
---|
| 144 | *
|
---|
| 145 | * @return Cosine value approximation.
|
---|
| 146 | *
|
---|
| 147 | */
|
---|
[516e780] | 148 | static double taylor_cos_64(double arg)
|
---|
[ba11ebb] | 149 | {
|
---|
[516e780] | 150 | double ret = 1;
|
---|
| 151 | double nom = 1;
|
---|
[a35b458] | 152 |
|
---|
[992ffa6] | 153 | for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) {
|
---|
[ba11ebb] | 154 | nom *= arg;
|
---|
[a35b458] | 155 |
|
---|
[ba11ebb] | 156 | if ((i % 4) == 1)
|
---|
| 157 | ret -= nom / factorials[i];
|
---|
| 158 | else if ((i % 4) == 3)
|
---|
| 159 | ret += nom / factorials[i];
|
---|
| 160 | }
|
---|
[a35b458] | 161 |
|
---|
[ba11ebb] | 162 | return ret;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[9adb61d] | 165 | /** Sine value for values within base period (32-bit floating point)
|
---|
| 166 | *
|
---|
| 167 | * Compute the value of sine for arguments within
|
---|
| 168 | * the base period [0, 2pi]. For arguments outside
|
---|
| 169 | * the base period the returned values can be
|
---|
| 170 | * very inaccurate or even completely wrong.
|
---|
| 171 | *
|
---|
| 172 | * @param arg Sine argument.
|
---|
| 173 | *
|
---|
| 174 | * @return Sine value.
|
---|
| 175 | *
|
---|
| 176 | */
|
---|
[516e780] | 177 | static float base_sin_32(float arg)
|
---|
[9adb61d] | 178 | {
|
---|
| 179 | unsigned int period = arg / (M_PI / 4);
|
---|
[a35b458] | 180 |
|
---|
[9adb61d] | 181 | switch (period) {
|
---|
| 182 | case 0:
|
---|
| 183 | return taylor_sin_32(arg);
|
---|
| 184 | case 1:
|
---|
| 185 | case 2:
|
---|
| 186 | return taylor_cos_32(arg - M_PI / 2);
|
---|
| 187 | case 3:
|
---|
| 188 | case 4:
|
---|
| 189 | return -taylor_sin_32(arg - M_PI);
|
---|
| 190 | case 5:
|
---|
| 191 | case 6:
|
---|
| 192 | return -taylor_cos_32(arg - 3 * M_PI / 2);
|
---|
| 193 | default:
|
---|
| 194 | return taylor_sin_32(arg - 2 * M_PI);
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /** Sine value for values within base period (64-bit floating point)
|
---|
[ba11ebb] | 199 | *
|
---|
| 200 | * Compute the value of sine for arguments within
|
---|
| 201 | * the base period [0, 2pi]. For arguments outside
|
---|
| 202 | * the base period the returned values can be
|
---|
| 203 | * very inaccurate or even completely wrong.
|
---|
| 204 | *
|
---|
| 205 | * @param arg Sine argument.
|
---|
| 206 | *
|
---|
| 207 | * @return Sine value.
|
---|
| 208 | *
|
---|
| 209 | */
|
---|
[516e780] | 210 | static double base_sin_64(double arg)
|
---|
[9adb61d] | 211 | {
|
---|
| 212 | unsigned int period = arg / (M_PI / 4);
|
---|
[a35b458] | 213 |
|
---|
[9adb61d] | 214 | switch (period) {
|
---|
| 215 | case 0:
|
---|
| 216 | return taylor_sin_64(arg);
|
---|
| 217 | case 1:
|
---|
| 218 | case 2:
|
---|
| 219 | return taylor_cos_64(arg - M_PI / 2);
|
---|
| 220 | case 3:
|
---|
| 221 | case 4:
|
---|
| 222 | return -taylor_sin_64(arg - M_PI);
|
---|
| 223 | case 5:
|
---|
| 224 | case 6:
|
---|
| 225 | return -taylor_cos_64(arg - 3 * M_PI / 2);
|
---|
| 226 | default:
|
---|
| 227 | return taylor_sin_64(arg - 2 * M_PI);
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /** Cosine value for values within base period (32-bit floating point)
|
---|
| 232 | *
|
---|
| 233 | * Compute the value of cosine for arguments within
|
---|
| 234 | * the base period [0, 2pi]. For arguments outside
|
---|
| 235 | * the base period the returned values can be
|
---|
| 236 | * very inaccurate or even completely wrong.
|
---|
| 237 | *
|
---|
| 238 | * @param arg Cosine argument.
|
---|
| 239 | *
|
---|
| 240 | * @return Cosine value.
|
---|
| 241 | *
|
---|
| 242 | */
|
---|
[516e780] | 243 | static float base_cos_32(float arg)
|
---|
[ba11ebb] | 244 | {
|
---|
| 245 | unsigned int period = arg / (M_PI / 4);
|
---|
[a35b458] | 246 |
|
---|
[ba11ebb] | 247 | switch (period) {
|
---|
| 248 | case 0:
|
---|
[9adb61d] | 249 | return taylor_cos_32(arg);
|
---|
[ba11ebb] | 250 | case 1:
|
---|
| 251 | case 2:
|
---|
[9adb61d] | 252 | return -taylor_sin_32(arg - M_PI / 2);
|
---|
[ba11ebb] | 253 | case 3:
|
---|
| 254 | case 4:
|
---|
[9adb61d] | 255 | return -taylor_cos_32(arg - M_PI);
|
---|
[ba11ebb] | 256 | case 5:
|
---|
| 257 | case 6:
|
---|
[9adb61d] | 258 | return taylor_sin_32(arg - 3 * M_PI / 2);
|
---|
[ba11ebb] | 259 | default:
|
---|
[9adb61d] | 260 | return taylor_cos_32(arg - 2 * M_PI);
|
---|
[ba11ebb] | 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[9adb61d] | 264 | /** Cosine value for values within base period (64-bit floating point)
|
---|
[ba11ebb] | 265 | *
|
---|
| 266 | * Compute the value of cosine for arguments within
|
---|
| 267 | * the base period [0, 2pi]. For arguments outside
|
---|
| 268 | * the base period the returned values can be
|
---|
| 269 | * very inaccurate or even completely wrong.
|
---|
| 270 | *
|
---|
| 271 | * @param arg Cosine argument.
|
---|
| 272 | *
|
---|
| 273 | * @return Cosine value.
|
---|
| 274 | *
|
---|
| 275 | */
|
---|
[516e780] | 276 | static double base_cos_64(double arg)
|
---|
[ba11ebb] | 277 | {
|
---|
| 278 | unsigned int period = arg / (M_PI / 4);
|
---|
[a35b458] | 279 |
|
---|
[ba11ebb] | 280 | switch (period) {
|
---|
| 281 | case 0:
|
---|
[9adb61d] | 282 | return taylor_cos_64(arg);
|
---|
[ba11ebb] | 283 | case 1:
|
---|
| 284 | case 2:
|
---|
[9adb61d] | 285 | return -taylor_sin_64(arg - M_PI / 2);
|
---|
[ba11ebb] | 286 | case 3:
|
---|
| 287 | case 4:
|
---|
[9adb61d] | 288 | return -taylor_cos_64(arg - M_PI);
|
---|
[ba11ebb] | 289 | case 5:
|
---|
| 290 | case 6:
|
---|
[9adb61d] | 291 | return taylor_sin_64(arg - 3 * M_PI / 2);
|
---|
[ba11ebb] | 292 | default:
|
---|
[9adb61d] | 293 | return taylor_cos_64(arg - 2 * M_PI);
|
---|
[ba11ebb] | 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[9adb61d] | 297 | /** Sine (32-bit floating point)
|
---|
| 298 | *
|
---|
| 299 | * Compute sine value.
|
---|
| 300 | *
|
---|
| 301 | * @param arg Sine argument.
|
---|
| 302 | *
|
---|
| 303 | * @return Sine value.
|
---|
| 304 | *
|
---|
| 305 | */
|
---|
[516e780] | 306 | float sinf(float arg)
|
---|
[9adb61d] | 307 | {
|
---|
[516e780] | 308 | float base_arg = fmodf(arg, 2 * M_PI);
|
---|
[a35b458] | 309 |
|
---|
[9adb61d] | 310 | if (base_arg < 0)
|
---|
| 311 | return -base_sin_32(-base_arg);
|
---|
[a35b458] | 312 |
|
---|
[9adb61d] | 313 | return base_sin_32(base_arg);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | /** Sine (64-bit floating point)
|
---|
[ba11ebb] | 317 | *
|
---|
| 318 | * Compute sine value.
|
---|
| 319 | *
|
---|
| 320 | * @param arg Sine argument.
|
---|
| 321 | *
|
---|
| 322 | * @return Sine value.
|
---|
| 323 | *
|
---|
| 324 | */
|
---|
[516e780] | 325 | double sin(double arg)
|
---|
[ba11ebb] | 326 | {
|
---|
[516e780] | 327 | double base_arg = fmod(arg, 2 * M_PI);
|
---|
[a35b458] | 328 |
|
---|
[9adb61d] | 329 | if (base_arg < 0)
|
---|
| 330 | return -base_sin_64(-base_arg);
|
---|
[a35b458] | 331 |
|
---|
[9adb61d] | 332 | return base_sin_64(base_arg);
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | /** Cosine (32-bit floating point)
|
---|
| 336 | *
|
---|
| 337 | * Compute cosine value.
|
---|
| 338 | *
|
---|
| 339 | * @param arg Cosine argument.
|
---|
| 340 | *
|
---|
| 341 | * @return Cosine value.
|
---|
| 342 | *
|
---|
| 343 | */
|
---|
[516e780] | 344 | float cosf(float arg)
|
---|
[9adb61d] | 345 | {
|
---|
[516e780] | 346 | float base_arg = fmodf(arg, 2 * M_PI);
|
---|
[a35b458] | 347 |
|
---|
[ba11ebb] | 348 | if (base_arg < 0)
|
---|
[9adb61d] | 349 | return base_cos_32(-base_arg);
|
---|
[a35b458] | 350 |
|
---|
[9adb61d] | 351 | return base_cos_32(base_arg);
|
---|
[ba11ebb] | 352 | }
|
---|
| 353 |
|
---|
[9adb61d] | 354 | /** Cosine (64-bit floating point)
|
---|
[ba11ebb] | 355 | *
|
---|
| 356 | * Compute cosine value.
|
---|
| 357 | *
|
---|
| 358 | * @param arg Cosine argument.
|
---|
| 359 | *
|
---|
| 360 | * @return Cosine value.
|
---|
| 361 | *
|
---|
| 362 | */
|
---|
[516e780] | 363 | double cos(double arg)
|
---|
[ba11ebb] | 364 | {
|
---|
[516e780] | 365 | double base_arg = fmod(arg, 2 * M_PI);
|
---|
[a35b458] | 366 |
|
---|
[ba11ebb] | 367 | if (base_arg < 0)
|
---|
[9adb61d] | 368 | return base_cos_64(-base_arg);
|
---|
[a35b458] | 369 |
|
---|
[9adb61d] | 370 | return base_cos_64(base_arg);
|
---|
[ba11ebb] | 371 | }
|
---|
| 372 |
|
---|
[a3c6a85] | 373 | /**
|
---|
| 374 | * Computes sine and cosine at the same time, which might be more efficient than
|
---|
| 375 | * computing each separately.
|
---|
| 376 | *
|
---|
| 377 | * @param x Input value.
|
---|
| 378 | * @param s Output sine value, *s = sinf(x).
|
---|
| 379 | * @param c Output cosine value, *c = cosf(x).
|
---|
| 380 | */
|
---|
[58e7b26] | 381 | void sincosf(float x, float *s, float *c)
|
---|
| 382 | {
|
---|
| 383 | float base_arg = fmodf(x, 2 * M_PI);
|
---|
| 384 |
|
---|
| 385 | if (base_arg < 0) {
|
---|
| 386 | *s = -base_sin_32(-base_arg);
|
---|
| 387 | *c = base_cos_32(-base_arg);
|
---|
| 388 | } else {
|
---|
| 389 | *s = base_sin_32(base_arg);
|
---|
| 390 | *c = base_cos_32(base_arg);
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
| 393 |
|
---|
[a3c6a85] | 394 | /**
|
---|
| 395 | * Computes sine and cosine at the same time, which might be more efficient than
|
---|
| 396 | * computing each separately.
|
---|
| 397 | *
|
---|
| 398 | * @param x Input value.
|
---|
| 399 | * @param s Output sine value, *s = sin(x).
|
---|
| 400 | * @param c Output cosine value, *c = cos(x).
|
---|
| 401 | */
|
---|
[58e7b26] | 402 | void sincos(double x, double *s, double *c)
|
---|
| 403 | {
|
---|
| 404 | double base_arg = fmod(x, 2 * M_PI);
|
---|
| 405 |
|
---|
| 406 | if (base_arg < 0) {
|
---|
| 407 | *s = -base_sin_64(-base_arg);
|
---|
| 408 | *c = base_cos_64(-base_arg);
|
---|
| 409 | } else {
|
---|
| 410 | *s = base_sin_64(base_arg);
|
---|
| 411 | *c = base_cos_64(base_arg);
|
---|
| 412 | }
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[ba11ebb] | 415 | /** @}
|
---|
| 416 | */
|
---|