TanEstimate.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 tan(x). The polynomial p(x) of
  10. // degree D has only odd-power terms, is required to have linear term x,
  11. // and p(pi/4) = tan(pi/4) = 1. It minimizes the quantity
  12. // maximum{|tan(x) - p(x)| : x in [-pi/4,pi/4]} over all polynomials of
  13. // degree D subject to the constraints mentioned.
  14. namespace WwiseGTE
  15. {
  16. template <typename Real>
  17. class TanEstimate
  18. {
  19. public:
  20. // The input constraint is x in [-pi/4,pi/4]. For example,
  21. // float x; // in [-pi/4,pi/4]
  22. // float result = TanEstimate<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 to
  29. // generate a value y in [-pi/2,pi/2]. If |y| <= pi/4, then the
  30. // polynomial is evaluated. If y in (pi/4,pi/2), set z = y - pi/4
  31. // and use the identity
  32. // tan(y) = tan(z + pi/4) = [1 + tan(z)]/[1 - tan(z)]
  33. // Be careful when evaluating at y nearly pi/2, because tan(y)
  34. // becomes infinite. For example,
  35. // float x; // x any real number
  36. // float result = TanEstimate<float>::DegreeRR<3>(x);
  37. template <int D>
  38. inline static Real DegreeRR(Real x)
  39. {
  40. Real y;
  41. Reduce(x, y);
  42. if (std::fabs(y) <= (Real)GTE_C_QUARTER_PI)
  43. {
  44. return Degree<D>(y);
  45. }
  46. else if (y > (Real)GTE_C_QUARTER_PI)
  47. {
  48. Real poly = Degree<D>(y - (Real)GTE_C_QUARTER_PI);
  49. return ((Real)1 + poly) / ((Real)1 - poly);
  50. }
  51. else
  52. {
  53. Real poly = Degree<D>(y + (Real)GTE_C_QUARTER_PI);
  54. return -((Real)1 - poly) / ((Real)1 + poly);
  55. }
  56. }
  57. private:
  58. // Metaprogramming and private implementation to allow specialization
  59. // of a template member function.
  60. template <int D> struct degree {};
  61. inline static Real Evaluate(degree<3>, Real x)
  62. {
  63. Real xsqr = x * x;
  64. Real poly;
  65. poly = (Real)GTE_C_TAN_DEG3_C1;
  66. poly = (Real)GTE_C_TAN_DEG3_C0 + poly * xsqr;
  67. poly = poly * x;
  68. return poly;
  69. }
  70. inline static Real Evaluate(degree<5>, Real x)
  71. {
  72. Real xsqr = x * x;
  73. Real poly;
  74. poly = (Real)GTE_C_TAN_DEG5_C2;
  75. poly = (Real)GTE_C_TAN_DEG5_C1 + poly * xsqr;
  76. poly = (Real)GTE_C_TAN_DEG5_C0 + poly * xsqr;
  77. poly = poly * x;
  78. return poly;
  79. }
  80. inline static Real Evaluate(degree<7>, Real x)
  81. {
  82. Real xsqr = x * x;
  83. Real poly;
  84. poly = (Real)GTE_C_TAN_DEG7_C3;
  85. poly = (Real)GTE_C_TAN_DEG7_C2 + poly * xsqr;
  86. poly = (Real)GTE_C_TAN_DEG7_C1 + poly * xsqr;
  87. poly = (Real)GTE_C_TAN_DEG7_C0 + poly * xsqr;
  88. poly = poly * x;
  89. return poly;
  90. }
  91. inline static Real Evaluate(degree<9>, Real x)
  92. {
  93. Real xsqr = x * x;
  94. Real poly;
  95. poly = (Real)GTE_C_TAN_DEG9_C4;
  96. poly = (Real)GTE_C_TAN_DEG9_C3 + poly * xsqr;
  97. poly = (Real)GTE_C_TAN_DEG9_C2 + poly * xsqr;
  98. poly = (Real)GTE_C_TAN_DEG9_C1 + poly * xsqr;
  99. poly = (Real)GTE_C_TAN_DEG9_C0 + poly * xsqr;
  100. poly = poly * x;
  101. return poly;
  102. }
  103. inline static Real Evaluate(degree<11>, Real x)
  104. {
  105. Real xsqr = x * x;
  106. Real poly;
  107. poly = (Real)GTE_C_TAN_DEG11_C5;
  108. poly = (Real)GTE_C_TAN_DEG11_C4 + poly * xsqr;
  109. poly = (Real)GTE_C_TAN_DEG11_C3 + poly * xsqr;
  110. poly = (Real)GTE_C_TAN_DEG11_C2 + poly * xsqr;
  111. poly = (Real)GTE_C_TAN_DEG11_C1 + poly * xsqr;
  112. poly = (Real)GTE_C_TAN_DEG11_C0 + poly * xsqr;
  113. poly = poly * x;
  114. return poly;
  115. }
  116. inline static Real Evaluate(degree<13>, Real x)
  117. {
  118. Real xsqr = x * x;
  119. Real poly;
  120. poly = (Real)GTE_C_TAN_DEG13_C6;
  121. poly = (Real)GTE_C_TAN_DEG13_C5 + poly * xsqr;
  122. poly = (Real)GTE_C_TAN_DEG13_C4 + poly * xsqr;
  123. poly = (Real)GTE_C_TAN_DEG13_C3 + poly * xsqr;
  124. poly = (Real)GTE_C_TAN_DEG13_C2 + poly * xsqr;
  125. poly = (Real)GTE_C_TAN_DEG13_C1 + poly * xsqr;
  126. poly = (Real)GTE_C_TAN_DEG13_C0 + poly * xsqr;
  127. poly = poly * x;
  128. return poly;
  129. }
  130. // Support for range reduction.
  131. inline static void Reduce(Real x, Real& y)
  132. {
  133. // Map x to y in [-pi,pi], x = pi*quotient + remainder.
  134. y = std::fmod(x, (Real)GTE_C_PI);
  135. // Map y to [-pi/2,pi/2] with tan(y) = tan(x).
  136. if (y > (Real)GTE_C_HALF_PI)
  137. {
  138. y -= (Real)GTE_C_PI;
  139. }
  140. else if (y < (Real)-GTE_C_HALF_PI)
  141. {
  142. y += (Real)GTE_C_PI;
  143. }
  144. }
  145. };
  146. }