| | | 1 | | using System; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | |
| | | 4 | | namespace FixedMathSharp |
| | | 5 | | { |
| | | 6 | | public static partial class FixedMath |
| | | 7 | | { |
| | | 8 | | #region Fields and Constants |
| | | 9 | | |
| | 1 | 10 | | public static readonly int[] Pow10Lookup = { |
| | 1 | 11 | | 1, // 10^0 = 1 |
| | 1 | 12 | | 10, // 10^1 = 10 |
| | 1 | 13 | | 100, // 10^2 = 100 |
| | 1 | 14 | | 1000, // 10^3 = 1000 |
| | 1 | 15 | | 10000, // 10^4 = 10000 |
| | 1 | 16 | | 100000, // 10^5 = 100000 |
| | 1 | 17 | | 1000000, // 10^6 = 1000000 |
| | 1 | 18 | | 10000000, // 10^7 = 1000000 |
| | 1 | 19 | | 100000000, // 10^8 = 1000000 |
| | 1 | 20 | | 1000000000, // 10^9 = 1000000 |
| | 1 | 21 | | }; |
| | | 22 | | |
| | | 23 | | // Trigonometric and logarithmic constants |
| | | 24 | | internal const double PI_D = 3.14159265358979323846; |
| | 1 | 25 | | public static readonly Fixed64 PI = (Fixed64)PI_D; |
| | 1 | 26 | | public static readonly Fixed64 TwoPI = PI * 2; |
| | 1 | 27 | | public static readonly Fixed64 PiOver2 = PI / 2; |
| | 1 | 28 | | public static readonly Fixed64 PiOver3 = PI / 3; |
| | 1 | 29 | | public static readonly Fixed64 PiOver4 = PI / 4; |
| | 1 | 30 | | public static readonly Fixed64 PiOver6 = PI / 6; |
| | 1 | 31 | | public static readonly Fixed64 Ln2 = (Fixed64)0.6931471805599453; // Natural logarithm of 2 |
| | | 32 | | |
| | 1 | 33 | | public static readonly Fixed64 LOG_2_MAX = new Fixed64(63L * ONE_L); |
| | 1 | 34 | | public static readonly Fixed64 LOG_2_MIN = new Fixed64(-64L * ONE_L); |
| | | 35 | | |
| | | 36 | | internal const double DEG2RAD_D = 0.01745329251994329576; // π / 180 |
| | 1 | 37 | | public static readonly Fixed64 Deg2Rad = (Fixed64)DEG2RAD_D; // Degrees to radians conversion factor |
| | | 38 | | internal const double RAD2DEG_D = 57.2957795130823208767; // 180 / π |
| | 1 | 39 | | public static readonly Fixed64 Rad2Deg = (Fixed64)RAD2DEG_D; // Radians to degrees conversion factor |
| | | 40 | | |
| | | 41 | | // Asin Padé approximations |
| | 1 | 42 | | private static readonly Fixed64 PADE_A1 = (Fixed64)0.183320102; |
| | 1 | 43 | | private static readonly Fixed64 PADE_A2 = (Fixed64)0.0218804099; |
| | | 44 | | |
| | | 45 | | // Carefully optimized polynomial coefficients for sin(x), ensuring maximum precision in Fixed64 math. |
| | 1 | 46 | | private static readonly Fixed64 SIN_COEFF_3 = (Fixed64)0.16666667605750262737274169921875d; // 1/3! |
| | 1 | 47 | | private static readonly Fixed64 SIN_COEFF_5 = (Fixed64)0.0083328341133892536163330078125d; // 1/5! |
| | 1 | 48 | | private static readonly Fixed64 SIN_COEFF_7 = (Fixed64)0.00019588856957852840423583984375d; // 1/7! |
| | | 49 | | |
| | | 50 | | #endregion |
| | | 51 | | |
| | | 52 | | #region FixedTrigonometry Operations |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Raises the base number b to the power of exp. |
| | | 56 | | /// Uses logarithms to compute power efficiently for fixed-point values. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <exception cref="DivideByZeroException"> |
| | | 59 | | /// The base was Fixed64.Zero, with a negative expFixed64.Onent |
| | | 60 | | /// </exception> |
| | | 61 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 62 | | /// The base was negative, with a non-Fixed64.Zero expFixed64.Onent |
| | | 63 | | /// </exception> |
| | | 64 | | public static Fixed64 Pow(Fixed64 b, Fixed64 exp) |
| | 6 | 65 | | { |
| | 6 | 66 | | if (b == Fixed64.One) |
| | 1 | 67 | | return Fixed64.One; |
| | | 68 | | |
| | 5 | 69 | | if (exp.m_rawValue == 0) |
| | 1 | 70 | | return Fixed64.One; |
| | | 71 | | |
| | 4 | 72 | | if (b.m_rawValue == 0) |
| | 2 | 73 | | { |
| | 2 | 74 | | if (exp.m_rawValue < 0) |
| | 1 | 75 | | throw new DivideByZeroException("Cannot raise 0 to a negative power."); |
| | | 76 | | |
| | 1 | 77 | | return Fixed64.Zero; |
| | | 78 | | } |
| | | 79 | | |
| | 2 | 80 | | Fixed64 log2 = Log2(b); // Calculate logarithm base 2 |
| | 2 | 81 | | return Pow2(exp * log2); // Raise 2 to the power of log2 result |
| | 5 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Raises 2 to the power of x. |
| | | 86 | | /// Provides high accuracy for small values of x. |
| | | 87 | | /// </summary> |
| | | 88 | | public static Fixed64 Pow2(Fixed64 x) |
| | 8 | 89 | | { |
| | 8 | 90 | | if (x.m_rawValue == 0) |
| | 1 | 91 | | return Fixed64.One; |
| | | 92 | | |
| | | 93 | | // Handle negative expFixed64.Onents by using the reciprocal |
| | 7 | 94 | | bool neg = x.m_rawValue < 0; |
| | 7 | 95 | | if (neg) |
| | 4 | 96 | | x = -x; |
| | | 97 | | |
| | 7 | 98 | | if (x == Fixed64.One) |
| | 3 | 99 | | return neg ? Fixed64.One / Fixed64.Two : Fixed64.Two; |
| | | 100 | | |
| | 4 | 101 | | if (x >= LOG_2_MAX) |
| | 2 | 102 | | return neg ? Fixed64.One / Fixed64.MAX_VALUE : Fixed64.MAX_VALUE; |
| | | 103 | | |
| | | 104 | | /* |
| | | 105 | | * Taylor series expansion for exp(x) |
| | | 106 | | * From term n, we get term n+1 by multiplying with x/n. |
| | | 107 | | * When the sum term drops to Fixed64.Zero, we can stop summing. |
| | | 108 | | */ |
| | 2 | 109 | | int integerPart = (int)x.Floor(); |
| | 2 | 110 | | x = Fixed64.FromRaw(x.m_rawValue & MAX_SHIFTED_AMOUNT_UI); // Fractional part |
| | | 111 | | |
| | 2 | 112 | | var result = Fixed64.One; |
| | 2 | 113 | | var term = Fixed64.One; |
| | 2 | 114 | | int i = 1; |
| | 13 | 115 | | while (term.m_rawValue != 0) |
| | 11 | 116 | | { |
| | 11 | 117 | | term = FastMul(FastMul(x, term), Ln2) / (Fixed64)i; |
| | 11 | 118 | | result += term; |
| | 11 | 119 | | i++; |
| | 11 | 120 | | } |
| | | 121 | | |
| | 2 | 122 | | result = Fixed64.FromRaw(result.m_rawValue << integerPart); |
| | 2 | 123 | | if (neg) |
| | 1 | 124 | | result = Fixed64.One / result; |
| | | 125 | | |
| | 2 | 126 | | return result; |
| | 8 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Returns the base-2 logarithm of a specified number. |
| | | 131 | | /// Provides at least 9 decimals of accuracy. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <remarks> |
| | | 134 | | /// This implementation is based on Clay. S. Turner's fast binary logarithm algorithm |
| | | 135 | | /// (C. S. Turner, "A Fast Binary Logarithm Algorithm", IEEE Signal Processing Mag., pp. 124,140, Sep. 2010.) |
| | | 136 | | /// </remarks> |
| | | 137 | | public static Fixed64 Log2(Fixed64 x) |
| | 6 | 138 | | { |
| | 6 | 139 | | if (x.m_rawValue <= 0) |
| | 1 | 140 | | throw new ArgumentOutOfRangeException("Cannot compute logarithm of non-positive number."); |
| | | 141 | | |
| | 5 | 142 | | long b = 1U << (SHIFT_AMOUNT_I - 1); // Initial value for binary logarithm |
| | 5 | 143 | | long y = 0; // Result accumulator |
| | 5 | 144 | | long rawX = x.m_rawValue; |
| | | 145 | | |
| | | 146 | | // Adjust rawX to the correct range [1, 2) |
| | 6 | 147 | | while (rawX < ONE_L) |
| | 1 | 148 | | { |
| | 1 | 149 | | rawX <<= 1; |
| | 1 | 150 | | y -= ONE_L; |
| | 1 | 151 | | } |
| | | 152 | | |
| | 11 | 153 | | while (rawX >= (ONE_L << 1)) |
| | 6 | 154 | | { |
| | 6 | 155 | | rawX >>= 1; |
| | 6 | 156 | | y += ONE_L; |
| | 6 | 157 | | } |
| | | 158 | | |
| | 5 | 159 | | Fixed64 z = Fixed64.FromRaw(rawX); // Remaining fraction |
| | | 160 | | |
| | 330 | 161 | | for (int i = 0; i < SHIFT_AMOUNT_I; i++) |
| | 160 | 162 | | { |
| | 160 | 163 | | z = FastMul(z, z); |
| | 160 | 164 | | if (z.m_rawValue >= (ONE_L << 1)) |
| | 15 | 165 | | { |
| | 15 | 166 | | z = Fixed64.FromRaw(z.m_rawValue >> 1); |
| | 15 | 167 | | y += b; |
| | 15 | 168 | | } |
| | 160 | 169 | | b >>= 1; |
| | 160 | 170 | | } |
| | | 171 | | |
| | 5 | 172 | | return Fixed64.FromRaw(y); |
| | 5 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Returns the natural logarithm of a specified fixed-point number. |
| | | 177 | | /// Provides at least 7 decimals of accuracy. |
| | | 178 | | /// </summary> |
| | | 179 | | public static Fixed64 Ln(Fixed64 x) |
| | 2 | 180 | | { |
| | 2 | 181 | | if (x.m_rawValue <= 0) |
| | 1 | 182 | | throw new ArgumentOutOfRangeException("Cannot compute logarithm of non-positive number."); |
| | | 183 | | |
| | 1 | 184 | | return FastMul(Log2(x), Ln2).Round(); |
| | 1 | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Returns the square root of a specified fixed-point number. |
| | | 189 | | /// </summary> |
| | | 190 | | public static Fixed64 Sqrt(Fixed64 x) |
| | 454 | 191 | | { |
| | 454 | 192 | | if (x.m_rawValue < 0) |
| | 1 | 193 | | throw new ArgumentOutOfRangeException("Cannot compute square root of a negative number."); |
| | | 194 | | |
| | 453 | 195 | | ulong num = (ulong)x.m_rawValue; |
| | 453 | 196 | | ulong result = 0UL; |
| | 453 | 197 | | ulong bit = 1UL << (sizeof(long) * 8) - 2; // second-to-top bit of a 64-bit integer |
| | | 198 | | |
| | | 199 | | // Adjust the bit position to a suitable starting point |
| | 7129 | 200 | | while (bit > num) |
| | 6676 | 201 | | bit >>= 2; |
| | | 202 | | |
| | | 203 | | // Perform the square root calculation using bitwise shifts |
| | 2718 | 204 | | for (int i = 0; i < 2; ++i) |
| | 906 | 205 | | { |
| | | 206 | | // Calculate the top bits of the square root result |
| | 15974 | 207 | | while (bit != 0) |
| | 15068 | 208 | | { |
| | 15068 | 209 | | if (num >= result + bit) |
| | 1874 | 210 | | { |
| | 1874 | 211 | | num -= result + bit; |
| | 1874 | 212 | | result = (result >> 1) + bit; |
| | 1874 | 213 | | } |
| | | 214 | | else |
| | 13194 | 215 | | { |
| | 13194 | 216 | | result >>= 1; |
| | 13194 | 217 | | } |
| | | 218 | | |
| | 15068 | 219 | | bit >>= 2; |
| | 15068 | 220 | | } |
| | | 221 | | |
| | 906 | 222 | | if (i == 0) |
| | 453 | 223 | | { |
| | | 224 | | // Process it again to get the remaining bits |
| | 453 | 225 | | if (num > ((1UL << SHIFT_AMOUNT_I) - 1)) |
| | 1 | 226 | | { |
| | | 227 | | // Handle large remainders by adjusting the result |
| | 1 | 228 | | num -= result; |
| | 1 | 229 | | num = (num << SHIFT_AMOUNT_I) - (ulong)Fixed64.Half.m_rawValue; |
| | 1 | 230 | | result = (result << SHIFT_AMOUNT_I) + (ulong)Fixed64.Half.m_rawValue; |
| | 1 | 231 | | } |
| | | 232 | | else |
| | 452 | 233 | | { |
| | 452 | 234 | | num <<= SHIFT_AMOUNT_I; |
| | 452 | 235 | | result <<= SHIFT_AMOUNT_I; |
| | 452 | 236 | | } |
| | | 237 | | |
| | 453 | 238 | | bit = 1UL << (SHIFT_AMOUNT_I - 2); |
| | 453 | 239 | | } |
| | 906 | 240 | | } |
| | | 241 | | |
| | | 242 | | // Rounding: round up if necessary |
| | 453 | 243 | | if (num > result && (num - result) > (result >> 1)) |
| | 29 | 244 | | ++result; |
| | | 245 | | |
| | 453 | 246 | | return Fixed64.FromRaw((long)result); |
| | 453 | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// Converts a value in radians to degrees. |
| | | 251 | | /// </summary> |
| | | 252 | | /// <remarks> |
| | | 253 | | /// Uses double precision to avoid precision loss |
| | | 254 | | /// </remarks> |
| | | 255 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 256 | | public static Fixed64 RadToDeg(Fixed64 rad) |
| | 20 | 257 | | { |
| | 20 | 258 | | return new Fixed64((double)rad * RAD2DEG_D); |
| | 20 | 259 | | } |
| | | 260 | | |
| | | 261 | | /// <summary> |
| | | 262 | | /// Converts a value in degrees to radians. |
| | | 263 | | /// </summary> |
| | | 264 | | /// <remarks> |
| | | 265 | | /// Uses double precision to avoid precision loss |
| | | 266 | | /// </remarks> |
| | | 267 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 268 | | public static Fixed64 DegToRad(Fixed64 deg) |
| | 91 | 269 | | { |
| | 91 | 270 | | return new Fixed64((double)deg * DEG2RAD_D); |
| | 91 | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// Computes the sine of a given angle in radians using an optimized |
| | | 275 | | /// minimax polynomial approximation. |
| | | 276 | | /// </summary> |
| | | 277 | | /// <param name="x">The angle in radians.</param> |
| | | 278 | | /// <returns>The sine of the given angle, in fixed-point format.</returns> |
| | | 279 | | /// <remarks> |
| | | 280 | | /// - This function uses a Chebyshev-polynomial-based approximation to ensure high accuracy |
| | | 281 | | /// while maintaining performance in fixed-point arithmetic. |
| | | 282 | | /// - The coefficients have been carefully tuned to minimize fixed-point truncation errors. |
| | | 283 | | /// - The error is less than 1 ULP (unit in the last place) at key reference points, |
| | | 284 | | /// ensuring <c>Sin(π/4) = 0.707106781192124</c> exactly within Fixed64 precision. |
| | | 285 | | /// - The function automatically normalizes input values to the range [-π, π] for stability. |
| | | 286 | | /// </remarks> |
| | | 287 | | public static Fixed64 Sin(Fixed64 x) |
| | 315 | 288 | | { |
| | | 289 | | // Check for special cases |
| | 370 | 290 | | if (x == Fixed64.Zero) return Fixed64.Zero; // sin(0) = 0 |
| | 333 | 291 | | if (x == PiOver2) return Fixed64.One; // sin(π/2) = 1 |
| | 189 | 292 | | if (x == -PiOver2) return -Fixed64.One; // sin(-π/2) = -1 |
| | 186 | 293 | | if (x == PI) return Fixed64.Zero; // sin(π) = 0 |
| | 202 | 294 | | if (x == -PI) return Fixed64.Zero; // sin(-π) = 0 |
| | 168 | 295 | | if (x == TwoPI || x == -TwoPI) return Fixed64.Zero; // sin(2π) = 0 |
| | | 296 | | |
| | | 297 | | // Normalize x to [-π, π] |
| | 164 | 298 | | x %= TwoPI; |
| | 164 | 299 | | if (x < -PI) |
| | 75 | 300 | | x += TwoPI; |
| | 89 | 301 | | else if (x > PI) |
| | 1 | 302 | | x -= TwoPI; |
| | | 303 | | |
| | 164 | 304 | | bool flip = false; |
| | 164 | 305 | | if (x < Fixed64.Zero) |
| | 7 | 306 | | { |
| | 7 | 307 | | x = -x; |
| | 7 | 308 | | flip = true; |
| | 7 | 309 | | } |
| | | 310 | | |
| | 164 | 311 | | if (x > PiOver2) |
| | 82 | 312 | | x = PI - x; |
| | | 313 | | |
| | | 314 | | // Precompute x^2 |
| | 164 | 315 | | Fixed64 x2 = x * x; |
| | | 316 | | |
| | | 317 | | // Optimized Chebyshev Polynomial for Sin(x) |
| | 164 | 318 | | Fixed64 result = x * (Fixed64.One |
| | 164 | 319 | | - x2 * SIN_COEFF_3 |
| | 164 | 320 | | + (x2 * x2) * SIN_COEFF_5 |
| | 164 | 321 | | - (x2 * x2 * x2) * SIN_COEFF_7); |
| | | 322 | | |
| | 164 | 323 | | return flip ? -result : result; |
| | 315 | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Computes the cosine of a given angle in radians using a sine-based identity transformation. |
| | | 328 | | /// </summary> |
| | | 329 | | /// <param name="x">The angle in radians.</param> |
| | | 330 | | /// <returns>The cosine of the given angle, in fixed-point format.</returns> |
| | | 331 | | /// <remarks> |
| | | 332 | | /// - Instead of directly approximating cosine, this function derives <c>cos(x)</c> using |
| | | 333 | | /// the identity <c>cos(x) = sin(x + π/2)</c>. This ensures maximum accuracy. |
| | | 334 | | /// - The underlying sine function is computed using a highly optimized minimax polynomial approximation. |
| | | 335 | | /// - By leveraging this transformation, cosine achieves the same precision guarantees |
| | | 336 | | /// as sine, including <c>Cos(π/4) = 0.707106781192124</c> exactly within Fixed64 precision. |
| | | 337 | | /// - The function automatically normalizes input values to the range [-π, π] for stability. |
| | | 338 | | /// </remarks> |
| | | 339 | | public static Fixed64 Cos(Fixed64 x) |
| | 154 | 340 | | { |
| | 154 | 341 | | long xl = x.m_rawValue; |
| | 154 | 342 | | long rawAngle = xl + (xl > 0 ? -PI.m_rawValue - PiOver2.m_rawValue : PiOver2.m_rawValue); |
| | 154 | 343 | | return Sin(Fixed64.FromRaw(rawAngle)); |
| | 154 | 344 | | } |
| | | 345 | | |
| | | 346 | | public static Fixed64 SinToCos(Fixed64 sin) |
| | 1 | 347 | | { |
| | 1 | 348 | | return Sqrt(Fixed64.One - sin * sin); |
| | 1 | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// Returns the tangent of x. |
| | | 353 | | /// </summary> |
| | | 354 | | /// <remarks> |
| | | 355 | | /// This function is not well-tested. It may be wildly inaccurate. |
| | | 356 | | /// </remarks> |
| | | 357 | | public static Fixed64 Tan(Fixed64 x) |
| | 10 | 358 | | { |
| | | 359 | | // Check for special cases |
| | 11 | 360 | | if (x == Fixed64.Zero) return Fixed64.Zero; |
| | 11 | 361 | | if (x == PiOver4) return Fixed64.One; |
| | 8 | 362 | | if (x == -PiOver4) return -Fixed64.One; |
| | | 363 | | |
| | | 364 | | // Normalize x to [-π/2, π/2] |
| | 6 | 365 | | x %= PI; |
| | 6 | 366 | | if (x < -PiOver2) |
| | 1 | 367 | | x += PI; |
| | 5 | 368 | | else if (x > PiOver2) |
| | 1 | 369 | | x -= PI; |
| | | 370 | | |
| | | 371 | | // Use continued fraction to approximate tan(x) |
| | 6 | 372 | | Fixed64 x2 = x * x; |
| | 6 | 373 | | Fixed64 numerator = x; |
| | 6 | 374 | | Fixed64 denominator = Fixed64.One; |
| | | 375 | | |
| | | 376 | | // Iterate over the continued fraction terms |
| | 6 | 377 | | Fixed64 prevDenominator = denominator; |
| | 6 | 378 | | int start = x.Abs() > PiOver6 ? 19 : 13; |
| | 114 | 379 | | for (int i = start; i >= 1; i -= 2) |
| | 51 | 380 | | { |
| | 51 | 381 | | denominator = (Fixed64)i - (x2 / denominator); |
| | 51 | 382 | | if ((denominator - prevDenominator).Abs() < Fixed64.Precision) |
| | 0 | 383 | | break; |
| | 51 | 384 | | prevDenominator = denominator; |
| | 51 | 385 | | } |
| | | 386 | | |
| | 6 | 387 | | return numerator / denominator; |
| | 10 | 388 | | } |
| | | 389 | | |
| | | 390 | | /// <summary> |
| | | 391 | | /// Returns the arc-sine of a fixed-point number x, which is the angle in radians |
| | | 392 | | /// whose sine is x, using a combination of a Taylor series expansion and trigonometric identities. |
| | | 393 | | /// |
| | | 394 | | /// For values of x near ±1, the identity asin(x) = π/2 - acos(x) is used for stability. |
| | | 395 | | /// For values of x near 0, a Taylor series expansion is used. |
| | | 396 | | /// </summary> |
| | | 397 | | /// <param name="x">The input value (sine) whose arcsine is to be computed. Should be in the range [-1, 1].</par |
| | | 398 | | /// <returns>The arc-sine of x in radians.</returns> |
| | | 399 | | /// <exception cref="ArithmeticException">Thrown if x is outside the domain [-1, 1].</exception> |
| | | 400 | | public static Fixed64 Asin(Fixed64 x) |
| | 12 | 401 | | { |
| | | 402 | | // Ensure x is within the domain [-1, 1] |
| | 12 | 403 | | if (x < -Fixed64.One || x > Fixed64.One) |
| | 2 | 404 | | throw new ArithmeticException("Input out of domain for Asin: " + x); |
| | | 405 | | |
| | | 406 | | // Handle boundary cases for -1 and 1 |
| | 11 | 407 | | if (x == Fixed64.One) return PiOver2; // asin(1) = π/2 |
| | 10 | 408 | | if (x == -Fixed64.One) return -PiOver2; // asin(-1) = -π/2 |
| | | 409 | | |
| | | 410 | | // Special case handling for asin(0.5) -> π/6 and asin(-0.5) -> -π/6 |
| | 9 | 411 | | if (x == Fixed64.Half) return PiOver6; |
| | 8 | 412 | | if (x == -Fixed64.Half) return -PiOver6; |
| | | 413 | | |
| | | 414 | | // For values close to 0, use a Padé approximation for better precision |
| | 6 | 415 | | if (x.Abs() < Fixed64.Half) |
| | 4 | 416 | | { |
| | | 417 | | // Padé approximation of asin(x) for |x| < 0.5 |
| | 4 | 418 | | Fixed64 xSquared = x * x; |
| | 4 | 419 | | Fixed64 numerator = x * (Fixed64.One + (xSquared * (PADE_A1 + (xSquared * PADE_A2)))); |
| | 4 | 420 | | return numerator; |
| | | 421 | | } |
| | | 422 | | |
| | | 423 | | // For values closer to ±1, use the identity: asin(x) = π/2 - acos(x) for stability |
| | 2 | 424 | | return x > Fixed64.Zero |
| | 2 | 425 | | ? PiOver2 - Acos(x) |
| | 2 | 426 | | : -PiOver2 + Acos(-x); |
| | 10 | 427 | | } |
| | | 428 | | |
| | | 429 | | /// <summary> |
| | | 430 | | /// Returns the arccosine of the specified number x, calculated using a combination of the atan and sqrt functio |
| | | 431 | | /// </summary> |
| | | 432 | | /// <param name="x">The input value whose arccosine is to be computed. Should be in the range [-1, 1].</param> |
| | | 433 | | /// <returns>The arccosine of x in radians.</returns> |
| | | 434 | | /// <exception cref="ArgumentOutOfRangeException">Thrown if x is outside the domain [-1, 1].</exception> |
| | | 435 | | public static Fixed64 Acos(Fixed64 x) |
| | 19 | 436 | | { |
| | 19 | 437 | | if (x < -Fixed64.One || x > Fixed64.One) |
| | 2 | 438 | | throw new ArgumentOutOfRangeException(nameof(x), "Input out of domain for Acos: " + x); |
| | | 439 | | |
| | | 440 | | // For values near 1 or -1, the result is directly known. |
| | 18 | 441 | | if (x == Fixed64.One) return Fixed64.Zero; // acos(1) = 0 |
| | 17 | 442 | | if (x == -Fixed64.One) return PI; // acos(-1) = π |
| | 21 | 443 | | if (x == Fixed64.Zero) return PiOver2; // acos(0) = π/2 |
| | | 444 | | |
| | | 445 | | // Compute using the relationship acos(x) = atan(sqrt(1 - x^2) / x) + π/2 when x is negative |
| | 9 | 446 | | var sqrtTerm = Sqrt(Fixed64.One - x * x); // sqrt(1 - x^2) |
| | 9 | 447 | | var atanTerm = Atan(sqrtTerm / x); |
| | | 448 | | |
| | 9 | 449 | | return x < Fixed64.Zero |
| | 9 | 450 | | ? atanTerm + PI // acos(-x) = atan(...) + π |
| | 9 | 451 | | : atanTerm; // Otherwise, return just atan(sqrt(...)) |
| | 17 | 452 | | } |
| | | 453 | | |
| | | 454 | | /// <summary> |
| | | 455 | | /// Returns the arctangent of the specified number, using a more accurate approximation for larger values. |
| | | 456 | | /// This function has at least 7 decimals of accuracy. |
| | | 457 | | /// </summary> |
| | | 458 | | public static Fixed64 Atan(Fixed64 z) |
| | 70 | 459 | | { |
| | 76 | 460 | | if (z == Fixed64.Zero) return Fixed64.Zero; |
| | 82 | 461 | | if (z == Fixed64.One) return PiOver4; |
| | 52 | 462 | | if (z == -Fixed64.One) return -PiOver4; |
| | | 463 | | |
| | 40 | 464 | | bool neg = z < Fixed64.Zero; |
| | 54 | 465 | | if (neg) z = -z; |
| | | 466 | | |
| | | 467 | | // Adjust series for z > 0.5 using the identity. |
| | | 468 | | Fixed64 adjustedResult; |
| | 40 | 469 | | if (z > Fixed64.Half) |
| | 18 | 470 | | { |
| | | 471 | | // Apply the identity: atan(z) = π/4 - atan((1 - z) / (1 + z)) |
| | 18 | 472 | | Fixed64 transformedZ = (Fixed64.One - z) / (Fixed64.One + z); |
| | 18 | 473 | | adjustedResult = PiOver4 - Atan(transformedZ); |
| | 18 | 474 | | } |
| | | 475 | | else |
| | 22 | 476 | | { |
| | | 477 | | // Use extended Taylor series directly for better precision on small z. |
| | 22 | 478 | | Fixed64 zSq = z * z; |
| | | 479 | | |
| | 22 | 480 | | Fixed64 result = z; |
| | 22 | 481 | | Fixed64 term = z; |
| | 22 | 482 | | int sign = -1; |
| | | 483 | | |
| | 198 | 484 | | for (int i = 3; i < 15; i += 2) |
| | 89 | 485 | | { |
| | 89 | 486 | | term *= zSq; |
| | 89 | 487 | | Fixed64 nextTerm = term / i; |
| | 89 | 488 | | if (nextTerm.Abs() < Fixed64.Precision) |
| | 12 | 489 | | break; |
| | | 490 | | |
| | 77 | 491 | | result += nextTerm * sign; |
| | 77 | 492 | | sign = -sign; |
| | 77 | 493 | | } |
| | | 494 | | |
| | 22 | 495 | | adjustedResult = result; |
| | 22 | 496 | | } |
| | | 497 | | |
| | 40 | 498 | | return neg ? -adjustedResult : adjustedResult; |
| | 70 | 499 | | } |
| | | 500 | | |
| | | 501 | | /// <summary> |
| | | 502 | | /// Computes the angle whose tangent is the quotient of two specified numbers. |
| | | 503 | | /// </summary> |
| | | 504 | | /// <remarks> |
| | | 505 | | /// Uses a fixed-point arithmetic approximation for the arc tangent function, which is more efficient than using |
| | | 506 | | /// especially on systems where floating-point operations are expensive. |
| | | 507 | | /// </remarks> |
| | | 508 | | /// <param name="y">The y-coordinate of the point to which the angle is measured.</param> |
| | | 509 | | /// <param name="x">The x-coordinate of the point to which the angle is measured.</param> |
| | | 510 | | /// <returns>An angle, θ, measured in radians, such that -π ≤ θ ≤ π, and tan(θ) = y / x, |
| | | 511 | | /// taking into account the quadrants of the inputs to determine the sign of the result.</returns> |
| | | 512 | | public static Fixed64 Atan2(Fixed64 y, Fixed64 x) |
| | 29 | 513 | | { |
| | 29 | 514 | | if (x == Fixed64.Zero) |
| | 7 | 515 | | { |
| | 7 | 516 | | if (y > Fixed64.Zero) |
| | 3 | 517 | | return PiOver2; |
| | 4 | 518 | | if (y == Fixed64.Zero) |
| | 3 | 519 | | return Fixed64.Zero; |
| | 1 | 520 | | return -PiOver2; |
| | | 521 | | } |
| | | 522 | | |
| | 22 | 523 | | Fixed64 atan = Atan(y / x); |
| | | 524 | | |
| | | 525 | | // Adjust based on the quadrant |
| | 22 | 526 | | if (x < Fixed64.Zero) |
| | 7 | 527 | | { |
| | 7 | 528 | | if (y >= Fixed64.Zero) |
| | 3 | 529 | | { |
| | | 530 | | // Second quadrant |
| | 3 | 531 | | return atan + PI; |
| | | 532 | | } |
| | | 533 | | else |
| | 4 | 534 | | { |
| | | 535 | | // Third quadrant |
| | 4 | 536 | | return atan - PI; |
| | | 537 | | } |
| | | 538 | | } |
| | | 539 | | |
| | | 540 | | // First or fourth quadrant |
| | 15 | 541 | | return atan; |
| | 29 | 542 | | } |
| | | 543 | | |
| | | 544 | | #endregion |
| | | 545 | | } |
| | | 546 | | } |