ATanEstimate.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Math.h>
  9. // Minimax polynomial approximations to atan(x). The polynomial p(x) of
  10. // degree D has only odd-power terms, is required to have linear term x,
  11. // and p(1) = atan(1) = pi/4. It minimizes the quantity
  12. // maximum{|atan(x) - p(x)| : x in [-1,1]} over all polynomials of
  13. // degree D subject to the constraints mentioned.
  14. namespace WwiseGTE
  15. {
  16. template <typename Real>
  17. class ATanEstimate
  18. {
  19. public:
  20. // The input constraint is x in [-1,1]. For example,
  21. // float x; // in [-1,1]
  22. // float result = ATanEstimate<float>::Degree<3>(x);
  23. template <int D>
  24. inline static Real Degree(Real x)
  25. {
  26. return Evaluate(degree<D>(), x);
  27. }
  28. // The input x can be any real number. Range reduction is used via
  29. // the identities atan(x) = pi/2 - atan(1/x) for x > 0, and
  30. // atan(x) = -pi/2 - atan(1/x) for x < 0. For example,
  31. // float x; // x any real number
  32. // float result = ATanEstimate<float>::DegreeRR<3>(x);
  33. template <int D>
  34. inline static Real DegreeRR(Real x)
  35. {
  36. if (std::fabs(x) <= (Real)1)
  37. {
  38. return Degree<D>(x);
  39. }
  40. else if (x > (Real)1)
  41. {
  42. return (Real)GTE_C_HALF_PI - Degree<D>((Real)1 / x);
  43. }
  44. else
  45. {
  46. return (Real)-GTE_C_HALF_PI - Degree<D>((Real)1 / x);
  47. }
  48. }
  49. private:
  50. // Metaprogramming and private implementation to allow specialization
  51. // of a template member function.
  52. template <int D> struct degree {};
  53. inline static Real Evaluate(degree<3>, Real x)
  54. {
  55. Real xsqr = x * x;
  56. Real poly;
  57. poly = (Real)GTE_C_ATAN_DEG3_C1;
  58. poly = (Real)GTE_C_ATAN_DEG3_C0 + poly * xsqr;
  59. poly = poly * x;
  60. return poly;
  61. }
  62. inline static Real Evaluate(degree<5>, Real x)
  63. {
  64. Real xsqr = x * x;
  65. Real poly;
  66. poly = (Real)GTE_C_ATAN_DEG5_C2;
  67. poly = (Real)GTE_C_ATAN_DEG5_C1 + poly * xsqr;
  68. poly = (Real)GTE_C_ATAN_DEG5_C0 + poly * xsqr;
  69. poly = poly * x;
  70. return poly;
  71. }
  72. inline static Real Evaluate(degree<7>, Real x)
  73. {
  74. Real xsqr = x * x;
  75. Real poly;
  76. poly = (Real)GTE_C_ATAN_DEG7_C3;
  77. poly = (Real)GTE_C_ATAN_DEG7_C2 + poly * xsqr;
  78. poly = (Real)GTE_C_ATAN_DEG7_C1 + poly * xsqr;
  79. poly = (Real)GTE_C_ATAN_DEG7_C0 + poly * xsqr;
  80. poly = poly * x;
  81. return poly;
  82. }
  83. inline static Real Evaluate(degree<9>, Real x)
  84. {
  85. Real xsqr = x * x;
  86. Real poly;
  87. poly = (Real)GTE_C_ATAN_DEG9_C4;
  88. poly = (Real)GTE_C_ATAN_DEG9_C3 + poly * xsqr;
  89. poly = (Real)GTE_C_ATAN_DEG9_C2 + poly * xsqr;
  90. poly = (Real)GTE_C_ATAN_DEG9_C1 + poly * xsqr;
  91. poly = (Real)GTE_C_ATAN_DEG9_C0 + poly * xsqr;
  92. poly = poly * x;
  93. return poly;
  94. }
  95. inline static Real Evaluate(degree<11>, Real x)
  96. {
  97. Real xsqr = x * x;
  98. Real poly;
  99. poly = (Real)GTE_C_ATAN_DEG11_C5;
  100. poly = (Real)GTE_C_ATAN_DEG11_C4 + poly * xsqr;
  101. poly = (Real)GTE_C_ATAN_DEG11_C3 + poly * xsqr;
  102. poly = (Real)GTE_C_ATAN_DEG11_C2 + poly * xsqr;
  103. poly = (Real)GTE_C_ATAN_DEG11_C1 + poly * xsqr;
  104. poly = (Real)GTE_C_ATAN_DEG11_C0 + poly * xsqr;
  105. poly = poly * x;
  106. return poly;
  107. }
  108. inline static Real Evaluate(degree<13>, Real x)
  109. {
  110. Real xsqr = x * x;
  111. Real poly;
  112. poly = (Real)GTE_C_ATAN_DEG13_C6;
  113. poly = (Real)GTE_C_ATAN_DEG13_C5 + poly * xsqr;
  114. poly = (Real)GTE_C_ATAN_DEG13_C4 + poly * xsqr;
  115. poly = (Real)GTE_C_ATAN_DEG13_C3 + poly * xsqr;
  116. poly = (Real)GTE_C_ATAN_DEG13_C2 + poly * xsqr;
  117. poly = (Real)GTE_C_ATAN_DEG13_C1 + poly * xsqr;
  118. poly = (Real)GTE_C_ATAN_DEG13_C0 + poly * xsqr;
  119. poly = poly * x;
  120. return poly;
  121. }
  122. };
  123. }