[ba11ebb] | 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 |
|
---|
[992ffa6] | 38 | #define TAYLOR_DEGREE_32 13
|
---|
| 39 | #define TAYLOR_DEGREE_64 21
|
---|
[ba11ebb] | 40 |
|
---|
| 41 | /** Precomputed values for factorial (starting from 1!) */
|
---|
[992ffa6] | 42 | static float64_t 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 |
|
---|
| 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 | */
|
---|
[c0c38c7c] | 61 | static float64_t taylor_sin(float64_t arg)
|
---|
[ba11ebb] | 62 | {
|
---|
[c0c38c7c] | 63 | float64_t ret = 0;
|
---|
| 64 | float64_t nom = 1;
|
---|
[ba11ebb] | 65 |
|
---|
[992ffa6] | 66 | for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) {
|
---|
[ba11ebb] | 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 | */
|
---|
[c0c38c7c] | 90 | static float64_t taylor_cos(float64_t arg)
|
---|
[ba11ebb] | 91 | {
|
---|
[c0c38c7c] | 92 | float64_t ret = 1;
|
---|
| 93 | float64_t nom = 1;
|
---|
[ba11ebb] | 94 |
|
---|
[992ffa6] | 95 | for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) {
|
---|
[ba11ebb] | 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 | */
|
---|
[c0c38c7c] | 119 | static float64_t base_sin(float64_t arg)
|
---|
[ba11ebb] | 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 | */
|
---|
[c0c38c7c] | 152 | static float64_t base_cos(float64_t arg)
|
---|
[ba11ebb] | 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:
|
---|
[c0c38c7c] | 161 | return -taylor_sin(arg - M_PI / 2);
|
---|
[ba11ebb] | 162 | case 3:
|
---|
| 163 | case 4:
|
---|
| 164 | return -taylor_cos(arg - M_PI);
|
---|
| 165 | case 5:
|
---|
| 166 | case 6:
|
---|
[c0c38c7c] | 167 | return taylor_sin(arg - 3 * M_PI / 2);
|
---|
[ba11ebb] | 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 | */
|
---|
[c0c38c7c] | 182 | float64_t float64_sin(float64_t arg)
|
---|
[ba11ebb] | 183 | {
|
---|
[c0c38c7c] | 184 | float64_t base_arg = fmod(arg, 2 * M_PI);
|
---|
[ba11ebb] | 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 | */
|
---|
[c0c38c7c] | 201 | float64_t float64_cos(float64_t arg)
|
---|
[ba11ebb] | 202 | {
|
---|
[c0c38c7c] | 203 | float64_t base_arg = fmod(arg, 2 * M_PI);
|
---|
[ba11ebb] | 204 |
|
---|
| 205 | if (base_arg < 0)
|
---|
| 206 | return base_cos(-base_arg);
|
---|
| 207 |
|
---|
| 208 | return base_cos(base_arg);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /** @}
|
---|
| 212 | */
|
---|