Math.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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.2020.01.08
  7. #pragma once
  8. // This file extends the <cmath> support to include convenient constants and
  9. // functions. The shared constants for CPU, Intel SSE and GPU lead to
  10. // correctly rounded approximations of the constants when using 'float' or
  11. // 'double'. The file also includes a type trait, is_arbitrary_precision,
  12. // to support selecting between floating-point arithmetic (float, double,
  13. //long double) or arbitrary-precision arithmetic (BSNumber<T>, BSRational<T>)
  14. // in an implementation using std::enable_if. There is also a type trait,
  15. // has_division_operator, to support selecting between numeric types that
  16. // have a division operator (BSRational<T>) and those that do not have a
  17. // division operator (BSNumber<T>).
  18. #include <cfenv>
  19. #include <cmath>
  20. #include <limits>
  21. #include <type_traits>
  22. // Maximum number of iterations for bisection before a subinterval
  23. // degenerates to a single point. TODO: Verify these. I used the formula:
  24. // 3 + std::numeric_limits<T>::digits - std::numeric_limits<T>::min_exponent.
  25. // IEEEBinary16: digits = 11, min_exponent = -13
  26. // float: digits = 27, min_exponent = -125
  27. // double: digits = 53, min_exponent = -1021
  28. // BSNumber and BSRational use std::numeric_limits<unsigned int>::max(),
  29. // but maybe these should be set to a large number or be user configurable.
  30. // The MAX_BISECTIONS_GENERIC is an arbitrary choice for now and is used
  31. // in template code where Real is the template parameter.
  32. #define GTE_C_MAX_BISECTIONS_FLOAT16 27u
  33. #define GTE_C_MAX_BISECTIONS_FLOAT32 155u
  34. #define GTE_C_MAX_BISECTIONS_FLOAT64 1077u
  35. #define GTE_C_MAX_BISECTIONS_BSNUMBER 0xFFFFFFFFu
  36. #define GTE_C_MAX_BISECTIONS_BSRATIONAL 0xFFFFFFFFu
  37. #define GTE_C_MAX_BISECTIONS_GENERIC 2048u
  38. // Constants involving pi.
  39. #define GTE_C_PI 3.1415926535897931
  40. #define GTE_C_HALF_PI 1.5707963267948966
  41. #define GTE_C_QUARTER_PI 0.7853981633974483
  42. #define GTE_C_TWO_PI 6.2831853071795862
  43. #define GTE_C_INV_PI 0.3183098861837907
  44. #define GTE_C_INV_TWO_PI 0.1591549430918953
  45. #define GTE_C_INV_HALF_PI 0.6366197723675813
  46. // Conversions between degrees and radians.
  47. #define GTE_C_DEG_TO_RAD 0.0174532925199433
  48. #define GTE_C_RAD_TO_DEG 57.295779513082321
  49. // Common constants.
  50. #define GTE_C_SQRT_2 1.4142135623730951
  51. #define GTE_C_INV_SQRT_2 0.7071067811865475
  52. #define GTE_C_LN_2 0.6931471805599453
  53. #define GTE_C_INV_LN_2 1.4426950408889634
  54. #define GTE_C_LN_10 2.3025850929940459
  55. #define GTE_C_INV_LN_10 0.43429448190325176
  56. // Constants for minimax polynomial approximations to sqrt(x).
  57. // The algorithm minimizes the maximum absolute error on [1,2].
  58. #define GTE_C_SQRT_DEG1_C0 +1.0
  59. #define GTE_C_SQRT_DEG1_C1 +4.1421356237309505e-01
  60. #define GTE_C_SQRT_DEG1_MAX_ERROR 1.7766952966368793e-2
  61. #define GTE_C_SQRT_DEG2_C0 +1.0
  62. #define GTE_C_SQRT_DEG2_C1 +4.8563183076125260e-01
  63. #define GTE_C_SQRT_DEG2_C2 -7.1418268388157458e-02
  64. #define GTE_C_SQRT_DEG2_MAX_ERROR 1.1795695163108744e-3
  65. #define GTE_C_SQRT_DEG3_C0 +1.0
  66. #define GTE_C_SQRT_DEG3_C1 +4.9750045320242231e-01
  67. #define GTE_C_SQRT_DEG3_C2 -1.0787308044477850e-01
  68. #define GTE_C_SQRT_DEG3_C3 +2.4586189615451115e-02
  69. #define GTE_C_SQRT_DEG3_MAX_ERROR 1.1309620116468910e-4
  70. #define GTE_C_SQRT_DEG4_C0 +1.0
  71. #define GTE_C_SQRT_DEG4_C1 +4.9955939832918816e-01
  72. #define GTE_C_SQRT_DEG4_C2 -1.2024066151943025e-01
  73. #define GTE_C_SQRT_DEG4_C3 +4.5461507257698486e-02
  74. #define GTE_C_SQRT_DEG4_C4 -1.0566681694362146e-02
  75. #define GTE_C_SQRT_DEG4_MAX_ERROR 1.2741170151556180e-5
  76. #define GTE_C_SQRT_DEG5_C0 +1.0
  77. #define GTE_C_SQRT_DEG5_C1 +4.9992197660031912e-01
  78. #define GTE_C_SQRT_DEG5_C2 -1.2378506719245053e-01
  79. #define GTE_C_SQRT_DEG5_C3 +5.6122776972699739e-02
  80. #define GTE_C_SQRT_DEG5_C4 -2.3128836281145482e-02
  81. #define GTE_C_SQRT_DEG5_C5 +5.0827122737047148e-03
  82. #define GTE_C_SQRT_DEG5_MAX_ERROR 1.5725568940708201e-6
  83. #define GTE_C_SQRT_DEG6_C0 +1.0
  84. #define GTE_C_SQRT_DEG6_C1 +4.9998616695784914e-01
  85. #define GTE_C_SQRT_DEG6_C2 -1.2470733323278438e-01
  86. #define GTE_C_SQRT_DEG6_C3 +6.0388587356982271e-02
  87. #define GTE_C_SQRT_DEG6_C4 -3.1692053551807930e-02
  88. #define GTE_C_SQRT_DEG6_C5 +1.2856590305148075e-02
  89. #define GTE_C_SQRT_DEG6_C6 -2.6183954624343642e-03
  90. #define GTE_C_SQRT_DEG6_MAX_ERROR 2.0584155535630089e-7
  91. #define GTE_C_SQRT_DEG7_C0 +1.0
  92. #define GTE_C_SQRT_DEG7_C1 +4.9999754817809228e-01
  93. #define GTE_C_SQRT_DEG7_C2 -1.2493243476353655e-01
  94. #define GTE_C_SQRT_DEG7_C3 +6.1859954146370910e-02
  95. #define GTE_C_SQRT_DEG7_C4 -3.6091595023208356e-02
  96. #define GTE_C_SQRT_DEG7_C5 +1.9483946523450868e-02
  97. #define GTE_C_SQRT_DEG7_C6 -7.5166134568007692e-03
  98. #define GTE_C_SQRT_DEG7_C7 +1.4127567687864939e-03
  99. #define GTE_C_SQRT_DEG7_MAX_ERROR 2.8072302919734948e-8
  100. #define GTE_C_SQRT_DEG8_C0 +1.0
  101. #define GTE_C_SQRT_DEG8_C1 +4.9999956583056759e-01
  102. #define GTE_C_SQRT_DEG8_C2 -1.2498490369914350e-01
  103. #define GTE_C_SQRT_DEG8_C3 +6.2318494667579216e-02
  104. #define GTE_C_SQRT_DEG8_C4 -3.7982961896432244e-02
  105. #define GTE_C_SQRT_DEG8_C5 +2.3642612312869460e-02
  106. #define GTE_C_SQRT_DEG8_C6 -1.2529377587270574e-02
  107. #define GTE_C_SQRT_DEG8_C7 +4.5382426960713929e-03
  108. #define GTE_C_SQRT_DEG8_C8 -7.8810995273670414e-04
  109. #define GTE_C_SQRT_DEG8_MAX_ERROR 3.9460605685825989e-9
  110. // Constants for minimax polynomial approximations to 1/sqrt(x).
  111. // The algorithm minimizes the maximum absolute error on [1,2].
  112. #define GTE_C_INVSQRT_DEG1_C0 +1.0
  113. #define GTE_C_INVSQRT_DEG1_C1 -2.9289321881345254e-01
  114. #define GTE_C_INVSQRT_DEG1_MAX_ERROR 3.7814314552701983e-2
  115. #define GTE_C_INVSQRT_DEG2_C0 +1.0
  116. #define GTE_C_INVSQRT_DEG2_C1 -4.4539812104566801e-01
  117. #define GTE_C_INVSQRT_DEG2_C2 +1.5250490223221547e-01
  118. #define GTE_C_INVSQRT_DEG2_MAX_ERROR 4.1953446330581234e-3
  119. #define GTE_C_INVSQRT_DEG3_C0 +1.0
  120. #define GTE_C_INVSQRT_DEG3_C1 -4.8703230993068791e-01
  121. #define GTE_C_INVSQRT_DEG3_C2 +2.8163710486669835e-01
  122. #define GTE_C_INVSQRT_DEG3_C3 -8.7498013749463421e-02
  123. #define GTE_C_INVSQRT_DEG3_MAX_ERROR 5.6307702007266786e-4
  124. #define GTE_C_INVSQRT_DEG4_C0 +1.0
  125. #define GTE_C_INVSQRT_DEG4_C1 -4.9710061558048779e-01
  126. #define GTE_C_INVSQRT_DEG4_C2 +3.4266247597676802e-01
  127. #define GTE_C_INVSQRT_DEG4_C3 -1.9106356536293490e-01
  128. #define GTE_C_INVSQRT_DEG4_C4 +5.2608486153198797e-02
  129. #define GTE_C_INVSQRT_DEG4_MAX_ERROR 8.1513919987605266e-5
  130. #define GTE_C_INVSQRT_DEG5_C0 +1.0
  131. #define GTE_C_INVSQRT_DEG5_C1 -4.9937760586004143e-01
  132. #define GTE_C_INVSQRT_DEG5_C2 +3.6508741295133973e-01
  133. #define GTE_C_INVSQRT_DEG5_C3 -2.5884890281853501e-01
  134. #define GTE_C_INVSQRT_DEG5_C4 +1.3275782221320753e-01
  135. #define GTE_C_INVSQRT_DEG5_C5 -3.2511945299404488e-02
  136. #define GTE_C_INVSQRT_DEG5_MAX_ERROR 1.2289367475583346e-5
  137. #define GTE_C_INVSQRT_DEG6_C0 +1.0
  138. #define GTE_C_INVSQRT_DEG6_C1 -4.9987029229547453e-01
  139. #define GTE_C_INVSQRT_DEG6_C2 +3.7220923604495226e-01
  140. #define GTE_C_INVSQRT_DEG6_C3 -2.9193067713256937e-01
  141. #define GTE_C_INVSQRT_DEG6_C4 +1.9937605991094642e-01
  142. #define GTE_C_INVSQRT_DEG6_C5 -9.3135712130901993e-02
  143. #define GTE_C_INVSQRT_DEG6_C6 +2.0458166789566690e-02
  144. #define GTE_C_INVSQRT_DEG6_MAX_ERROR 1.9001451223750465e-6
  145. #define GTE_C_INVSQRT_DEG7_C0 +1.0
  146. #define GTE_C_INVSQRT_DEG7_C1 -4.9997357250704977e-01
  147. #define GTE_C_INVSQRT_DEG7_C2 +3.7426216884998809e-01
  148. #define GTE_C_INVSQRT_DEG7_C3 -3.0539882498248971e-01
  149. #define GTE_C_INVSQRT_DEG7_C4 +2.3976005607005391e-01
  150. #define GTE_C_INVSQRT_DEG7_C5 -1.5410326351684489e-01
  151. #define GTE_C_INVSQRT_DEG7_C6 +6.5598809723041995e-02
  152. #define GTE_C_INVSQRT_DEG7_C7 -1.3038592450470787e-02
  153. #define GTE_C_INVSQRT_DEG7_MAX_ERROR 2.9887724993168940e-7
  154. #define GTE_C_INVSQRT_DEG8_C0 +1.0
  155. #define GTE_C_INVSQRT_DEG8_C1 -4.9999471066120371e-01
  156. #define GTE_C_INVSQRT_DEG8_C2 +3.7481415745794067e-01
  157. #define GTE_C_INVSQRT_DEG8_C3 -3.1023804387422160e-01
  158. #define GTE_C_INVSQRT_DEG8_C4 +2.5977002682930106e-01
  159. #define GTE_C_INVSQRT_DEG8_C5 -1.9818790717727097e-01
  160. #define GTE_C_INVSQRT_DEG8_C6 +1.1882414252613671e-01
  161. #define GTE_C_INVSQRT_DEG8_C7 -4.6270038088550791e-02
  162. #define GTE_C_INVSQRT_DEG8_C8 +8.3891541755747312e-03
  163. #define GTE_C_INVSQRT_DEG8_MAX_ERROR 4.7596926146947771e-8
  164. // Constants for minimax polynomial approximations to sin(x).
  165. // The algorithm minimizes the maximum absolute error on [-pi/2,pi/2].
  166. #define GTE_C_SIN_DEG3_C0 +1.0
  167. #define GTE_C_SIN_DEG3_C1 -1.4727245910375519e-01
  168. #define GTE_C_SIN_DEG3_MAX_ERROR 1.3481903639145865e-2
  169. #define GTE_C_SIN_DEG5_C0 +1.0
  170. #define GTE_C_SIN_DEG5_C1 -1.6600599923812209e-01
  171. #define GTE_C_SIN_DEG5_C2 +7.5924178409012000e-03
  172. #define GTE_C_SIN_DEG5_MAX_ERROR 1.4001209384639779e-4
  173. #define GTE_C_SIN_DEG7_C0 +1.0
  174. #define GTE_C_SIN_DEG7_C1 -1.6665578084732124e-01
  175. #define GTE_C_SIN_DEG7_C2 +8.3109378830028557e-03
  176. #define GTE_C_SIN_DEG7_C3 -1.8447486103462252e-04
  177. #define GTE_C_SIN_DEG7_MAX_ERROR 1.0205878936686563e-6
  178. #define GTE_C_SIN_DEG9_C0 +1.0
  179. #define GTE_C_SIN_DEG9_C1 -1.6666656235308897e-01
  180. #define GTE_C_SIN_DEG9_C2 +8.3329962509886002e-03
  181. #define GTE_C_SIN_DEG9_C3 -1.9805100675274190e-04
  182. #define GTE_C_SIN_DEG9_C4 +2.5967200279475300e-06
  183. #define GTE_C_SIN_DEG9_MAX_ERROR 5.2010746265374053e-9
  184. #define GTE_C_SIN_DEG11_C0 +1.0
  185. #define GTE_C_SIN_DEG11_C1 -1.6666666601721269e-01
  186. #define GTE_C_SIN_DEG11_C2 +8.3333303183525942e-03
  187. #define GTE_C_SIN_DEG11_C3 -1.9840782426250314e-04
  188. #define GTE_C_SIN_DEG11_C4 +2.7521557770526783e-06
  189. #define GTE_C_SIN_DEG11_C5 -2.3828544692960918e-08
  190. #define GTE_C_SIN_DEG11_MAX_ERROR 1.9295870457014530e-11
  191. // Constants for minimax polynomial approximations to cos(x).
  192. // The algorithm minimizes the maximum absolute error on [-pi/2,pi/2].
  193. #define GTE_C_COS_DEG2_C0 +1.0
  194. #define GTE_C_COS_DEG2_C1 -4.0528473456935105e-01
  195. #define GTE_C_COS_DEG2_MAX_ERROR 5.4870946878404048e-2
  196. #define GTE_C_COS_DEG4_C0 +1.0
  197. #define GTE_C_COS_DEG4_C1 -4.9607181958647262e-01
  198. #define GTE_C_COS_DEG4_C2 +3.6794619653489236e-02
  199. #define GTE_C_COS_DEG4_MAX_ERROR 9.1879932449712154e-4
  200. #define GTE_C_COS_DEG6_C0 +1.0
  201. #define GTE_C_COS_DEG6_C1 -4.9992746217057404e-01
  202. #define GTE_C_COS_DEG6_C2 +4.1493920348353308e-02
  203. #define GTE_C_COS_DEG6_C3 -1.2712435011987822e-03
  204. #define GTE_C_COS_DEG6_MAX_ERROR 9.2028470133065365e-6
  205. #define GTE_C_COS_DEG8_C0 +1.0
  206. #define GTE_C_COS_DEG8_C1 -4.9999925121358291e-01
  207. #define GTE_C_COS_DEG8_C2 +4.1663780117805693e-02
  208. #define GTE_C_COS_DEG8_C3 -1.3854239405310942e-03
  209. #define GTE_C_COS_DEG8_C4 +2.3154171575501259e-05
  210. #define GTE_C_COS_DEG8_MAX_ERROR 5.9804533020235695e-8
  211. #define GTE_C_COS_DEG10_C0 +1.0
  212. #define GTE_C_COS_DEG10_C1 -4.9999999508695869e-01
  213. #define GTE_C_COS_DEG10_C2 +4.1666638865338612e-02
  214. #define GTE_C_COS_DEG10_C3 -1.3888377661039897e-03
  215. #define GTE_C_COS_DEG10_C4 +2.4760495088926859e-05
  216. #define GTE_C_COS_DEG10_C5 -2.6051615464872668e-07
  217. #define GTE_C_COS_DEG10_MAX_ERROR 2.7006769043325107e-10
  218. // Constants for minimax polynomial approximations to tan(x).
  219. // The algorithm minimizes the maximum absolute error on [-pi/4,pi/4].
  220. #define GTE_C_TAN_DEG3_C0 1.0
  221. #define GTE_C_TAN_DEG3_C1 4.4295926544736286e-01
  222. #define GTE_C_TAN_DEG3_MAX_ERROR 1.1661892256204731e-2
  223. #define GTE_C_TAN_DEG5_C0 1.0
  224. #define GTE_C_TAN_DEG5_C1 3.1401320403542421e-01
  225. #define GTE_C_TAN_DEG5_C2 2.0903948109240345e-01
  226. #define GTE_C_TAN_DEG5_MAX_ERROR 5.8431854390143118e-4
  227. #define GTE_C_TAN_DEG7_C0 1.0
  228. #define GTE_C_TAN_DEG7_C1 3.3607213284422555e-01
  229. #define GTE_C_TAN_DEG7_C2 1.1261037305184907e-01
  230. #define GTE_C_TAN_DEG7_C3 9.8352099470524479e-02
  231. #define GTE_C_TAN_DEG7_MAX_ERROR 3.5418688397723108e-5
  232. #define GTE_C_TAN_DEG9_C0 1.0
  233. #define GTE_C_TAN_DEG9_C1 3.3299232843941784e-01
  234. #define GTE_C_TAN_DEG9_C2 1.3747843432474838e-01
  235. #define GTE_C_TAN_DEG9_C3 3.7696344813028304e-02
  236. #define GTE_C_TAN_DEG9_C4 4.6097377279281204e-02
  237. #define GTE_C_TAN_DEG9_MAX_ERROR 2.2988173242199927e-6
  238. #define GTE_C_TAN_DEG11_C0 1.0
  239. #define GTE_C_TAN_DEG11_C1 3.3337224456224224e-01
  240. #define GTE_C_TAN_DEG11_C2 1.3264516053824593e-01
  241. #define GTE_C_TAN_DEG11_C3 5.8145237645931047e-02
  242. #define GTE_C_TAN_DEG11_C4 1.0732193237572574e-02
  243. #define GTE_C_TAN_DEG11_C5 2.1558456793513869e-02
  244. #define GTE_C_TAN_DEG11_MAX_ERROR 1.5426257940140409e-7
  245. #define GTE_C_TAN_DEG13_C0 1.0
  246. #define GTE_C_TAN_DEG13_C1 3.3332916426394554e-01
  247. #define GTE_C_TAN_DEG13_C2 1.3343404625112498e-01
  248. #define GTE_C_TAN_DEG13_C3 5.3104565343119248e-02
  249. #define GTE_C_TAN_DEG13_C4 2.5355038312682154e-02
  250. #define GTE_C_TAN_DEG13_C5 1.8253255966556026e-03
  251. #define GTE_C_TAN_DEG13_C6 1.0069407176615641e-02
  252. #define GTE_C_TAN_DEG13_MAX_ERROR 1.0550264249037378e-8
  253. // Constants for minimax polynomial approximations to acos(x), where the
  254. // approximation is of the form acos(x) = sqrt(1 - x)*p(x) with p(x) a
  255. // polynomial. The algorithm minimizes the maximum error
  256. // |acos(x)/sqrt(1-x) - p(x)| on [0,1]. At the same time we get an
  257. // approximation for asin(x) = pi/2 - acos(x).
  258. #define GTE_C_ACOS_DEG1_C0 +1.5707963267948966
  259. #define GTE_C_ACOS_DEG1_C1 -1.5658276442180141e-01
  260. #define GTE_C_ACOS_DEG1_MAX_ERROR 1.1659002803738105e-2
  261. #define GTE_C_ACOS_DEG2_C0 +1.5707963267948966
  262. #define GTE_C_ACOS_DEG2_C1 -2.0347053865798365e-01
  263. #define GTE_C_ACOS_DEG2_C2 +4.6887774236182234e-02
  264. #define GTE_C_ACOS_DEG2_MAX_ERROR 9.0311602490029258e-4
  265. #define GTE_C_ACOS_DEG3_C0 +1.5707963267948966
  266. #define GTE_C_ACOS_DEG3_C1 -2.1253291899190285e-01
  267. #define GTE_C_ACOS_DEG3_C2 +7.4773789639484223e-02
  268. #define GTE_C_ACOS_DEG3_C3 -1.8823635069382449e-02
  269. #define GTE_C_ACOS_DEG3_MAX_ERROR 9.3066396954288172e-5
  270. #define GTE_C_ACOS_DEG4_C0 +1.5707963267948966
  271. #define GTE_C_ACOS_DEG4_C1 -2.1422258835275865e-01
  272. #define GTE_C_ACOS_DEG4_C2 +8.4936675142844198e-02
  273. #define GTE_C_ACOS_DEG4_C3 -3.5991475120957794e-02
  274. #define GTE_C_ACOS_DEG4_C4 +8.6946239090712751e-03
  275. #define GTE_C_ACOS_DEG4_MAX_ERROR 1.0930595804481413e-5
  276. #define GTE_C_ACOS_DEG5_C0 +1.5707963267948966
  277. #define GTE_C_ACOS_DEG5_C1 -2.1453292139805524e-01
  278. #define GTE_C_ACOS_DEG5_C2 +8.7973089282889383e-02
  279. #define GTE_C_ACOS_DEG5_C3 -4.5130266382166440e-02
  280. #define GTE_C_ACOS_DEG5_C4 +1.9467466687281387e-02
  281. #define GTE_C_ACOS_DEG5_C5 -4.3601326117634898e-03
  282. #define GTE_C_ACOS_DEG5_MAX_ERROR 1.3861070257241426-6
  283. #define GTE_C_ACOS_DEG6_C0 +1.5707963267948966
  284. #define GTE_C_ACOS_DEG6_C1 -2.1458939285677325e-01
  285. #define GTE_C_ACOS_DEG6_C2 +8.8784960563641491e-02
  286. #define GTE_C_ACOS_DEG6_C3 -4.8887131453156485e-02
  287. #define GTE_C_ACOS_DEG6_C4 +2.7011519960012720e-02
  288. #define GTE_C_ACOS_DEG6_C5 -1.1210537323478320e-02
  289. #define GTE_C_ACOS_DEG6_C6 +2.3078166879102469e-03
  290. #define GTE_C_ACOS_DEG6_MAX_ERROR 1.8491291330427484e-7
  291. #define GTE_C_ACOS_DEG7_C0 +1.5707963267948966
  292. #define GTE_C_ACOS_DEG7_C1 -2.1459960076929829e-01
  293. #define GTE_C_ACOS_DEG7_C2 +8.8986946573346160e-02
  294. #define GTE_C_ACOS_DEG7_C3 -5.0207843052845647e-02
  295. #define GTE_C_ACOS_DEG7_C4 +3.0961594977611639e-02
  296. #define GTE_C_ACOS_DEG7_C5 -1.7162031184398074e-02
  297. #define GTE_C_ACOS_DEG7_C6 +6.7072304676685235e-03
  298. #define GTE_C_ACOS_DEG7_C7 -1.2690614339589956e-03
  299. #define GTE_C_ACOS_DEG7_MAX_ERROR 2.5574620927948377e-8
  300. #define GTE_C_ACOS_DEG8_C0 +1.5707963267948966
  301. #define GTE_C_ACOS_DEG8_C1 -2.1460143648688035e-01
  302. #define GTE_C_ACOS_DEG8_C2 +8.9034700107934128e-02
  303. #define GTE_C_ACOS_DEG8_C3 -5.0625279962389413e-02
  304. #define GTE_C_ACOS_DEG8_C4 +3.2683762943179318e-02
  305. #define GTE_C_ACOS_DEG8_C5 -2.0949278766238422e-02
  306. #define GTE_C_ACOS_DEG8_C6 +1.1272900916992512e-02
  307. #define GTE_C_ACOS_DEG8_C7 -4.1160981058965262e-03
  308. #define GTE_C_ACOS_DEG8_C8 +7.1796493341480527e-04
  309. #define GTE_C_ACOS_DEG8_MAX_ERROR 3.6340015129032732e-9
  310. // Constants for minimax polynomial approximations to atan(x).
  311. // The algorithm minimizes the maximum absolute error on [-1,1].
  312. #define GTE_C_ATAN_DEG3_C0 +1.0
  313. #define GTE_C_ATAN_DEG3_C1 -2.1460183660255172e-01
  314. #define GTE_C_ATAN_DEG3_MAX_ERROR 1.5970326392614240e-2
  315. #define GTE_C_ATAN_DEG5_C0 +1.0
  316. #define GTE_C_ATAN_DEG5_C1 -3.0189478312144946e-01
  317. #define GTE_C_ATAN_DEG5_C2 +8.7292946518897740e-02
  318. #define GTE_C_ATAN_DEG5_MAX_ERROR 1.3509832247372636e-3
  319. #define GTE_C_ATAN_DEG7_C0 +1.0
  320. #define GTE_C_ATAN_DEG7_C1 -3.2570157599356531e-01
  321. #define GTE_C_ATAN_DEG7_C2 +1.5342994884206673e-01
  322. #define GTE_C_ATAN_DEG7_C3 -4.2330209451053591e-02
  323. #define GTE_C_ATAN_DEG7_MAX_ERROR 1.5051227215514412e-4
  324. #define GTE_C_ATAN_DEG9_C0 +1.0
  325. #define GTE_C_ATAN_DEG9_C1 -3.3157878236439586e-01
  326. #define GTE_C_ATAN_DEG9_C2 +1.8383034738018011e-01
  327. #define GTE_C_ATAN_DEG9_C3 -8.9253037587244677e-02
  328. #define GTE_C_ATAN_DEG9_C4 +2.2399635968909593e-02
  329. #define GTE_C_ATAN_DEG9_MAX_ERROR 1.8921598624582064e-5
  330. #define GTE_C_ATAN_DEG11_C0 +1.0
  331. #define GTE_C_ATAN_DEG11_C1 -3.3294527685374087e-01
  332. #define GTE_C_ATAN_DEG11_C2 +1.9498657165383548e-01
  333. #define GTE_C_ATAN_DEG11_C3 -1.1921576270475498e-01
  334. #define GTE_C_ATAN_DEG11_C4 +5.5063351366968050e-02
  335. #define GTE_C_ATAN_DEG11_C5 -1.2490720064867844e-02
  336. #define GTE_C_ATAN_DEG11_MAX_ERROR 2.5477724974187765e-6
  337. #define GTE_C_ATAN_DEG13_C0 +1.0
  338. #define GTE_C_ATAN_DEG13_C1 -3.3324998579202170e-01
  339. #define GTE_C_ATAN_DEG13_C2 +1.9856563505717162e-01
  340. #define GTE_C_ATAN_DEG13_C3 -1.3374657325451267e-01
  341. #define GTE_C_ATAN_DEG13_C4 +8.1675882859940430e-02
  342. #define GTE_C_ATAN_DEG13_C5 -3.5059680836411644e-02
  343. #define GTE_C_ATAN_DEG13_C6 +7.2128853633444123e-03
  344. #define GTE_C_ATAN_DEG13_MAX_ERROR 3.5859104691865484e-7
  345. // Constants for minimax polynomial approximations to exp2(x) = 2^x.
  346. // The algorithm minimizes the maximum absolute error on [0,1].
  347. #define GTE_C_EXP2_DEG1_C0 1.0
  348. #define GTE_C_EXP2_DEG1_C1 1.0
  349. #define GTE_C_EXP2_DEG1_MAX_ERROR 8.6071332055934313e-2
  350. #define GTE_C_EXP2_DEG2_C0 1.0
  351. #define GTE_C_EXP2_DEG2_C1 6.5571332605741528e-01
  352. #define GTE_C_EXP2_DEG2_C2 3.4428667394258472e-01
  353. #define GTE_C_EXP2_DEG2_MAX_ERROR 3.8132476831060358e-3
  354. #define GTE_C_EXP2_DEG3_C0 1.0
  355. #define GTE_C_EXP2_DEG3_C1 6.9589012084456225e-01
  356. #define GTE_C_EXP2_DEG3_C2 2.2486494900110188e-01
  357. #define GTE_C_EXP2_DEG3_C3 7.9244930154334980e-02
  358. #define GTE_C_EXP2_DEG3_MAX_ERROR 1.4694877755186408e-4
  359. #define GTE_C_EXP2_DEG4_C0 1.0
  360. #define GTE_C_EXP2_DEG4_C1 6.9300392358459195e-01
  361. #define GTE_C_EXP2_DEG4_C2 2.4154981722455560e-01
  362. #define GTE_C_EXP2_DEG4_C3 5.1744260331489045e-02
  363. #define GTE_C_EXP2_DEG4_C4 1.3701998859367848e-02
  364. #define GTE_C_EXP2_DEG4_MAX_ERROR 4.7617792624521371e-6
  365. #define GTE_C_EXP2_DEG5_C0 1.0
  366. #define GTE_C_EXP2_DEG5_C1 6.9315298010274962e-01
  367. #define GTE_C_EXP2_DEG5_C2 2.4014712313022102e-01
  368. #define GTE_C_EXP2_DEG5_C3 5.5855296413199085e-02
  369. #define GTE_C_EXP2_DEG5_C4 8.9477503096873079e-03
  370. #define GTE_C_EXP2_DEG5_C5 1.8968500441332026e-03
  371. #define GTE_C_EXP2_DEG5_MAX_ERROR 1.3162098333463490e-7
  372. #define GTE_C_EXP2_DEG6_C0 1.0
  373. #define GTE_C_EXP2_DEG6_C1 6.9314698914837525e-01
  374. #define GTE_C_EXP2_DEG6_C2 2.4023013440952923e-01
  375. #define GTE_C_EXP2_DEG6_C3 5.5481276898206033e-02
  376. #define GTE_C_EXP2_DEG6_C4 9.6838443037086108e-03
  377. #define GTE_C_EXP2_DEG6_C5 1.2388324048515642e-03
  378. #define GTE_C_EXP2_DEG6_C6 2.1892283501756538e-04
  379. #define GTE_C_EXP2_DEG6_MAX_ERROR 3.1589168225654163e-9
  380. #define GTE_C_EXP2_DEG7_C0 1.0
  381. #define GTE_C_EXP2_DEG7_C1 6.9314718588750690e-01
  382. #define GTE_C_EXP2_DEG7_C2 2.4022637363165700e-01
  383. #define GTE_C_EXP2_DEG7_C3 5.5505235570535660e-02
  384. #define GTE_C_EXP2_DEG7_C4 9.6136265387940512e-03
  385. #define GTE_C_EXP2_DEG7_C5 1.3429234504656051e-03
  386. #define GTE_C_EXP2_DEG7_C6 1.4299202757683815e-04
  387. #define GTE_C_EXP2_DEG7_C7 2.1662892777385423e-05
  388. #define GTE_C_EXP2_DEG7_MAX_ERROR 6.6864513925679603e-11
  389. // Constants for minimax polynomial approximations to log2(x).
  390. // The algorithm minimizes the maximum absolute error on [1,2].
  391. // The polynomials all have constant term zero.
  392. #define GTE_C_LOG2_DEG1_C1 +1.0
  393. #define GTE_C_LOG2_DEG1_MAX_ERROR 8.6071332055934202e-2
  394. #define GTE_C_LOG2_DEG2_C1 +1.3465553856377803
  395. #define GTE_C_LOG2_DEG2_C2 -3.4655538563778032e-01
  396. #define GTE_C_LOG2_DEG2_MAX_ERROR 7.6362868906658110e-3
  397. #define GTE_C_LOG2_DEG3_C1 +1.4228653756681227
  398. #define GTE_C_LOG2_DEG3_C2 -5.8208556916449616e-01
  399. #define GTE_C_LOG2_DEG3_C3 +1.5922019349637218e-01
  400. #define GTE_C_LOG2_DEG3_MAX_ERROR 8.7902902652883808e-4
  401. #define GTE_C_LOG2_DEG4_C1 +1.4387257478171547
  402. #define GTE_C_LOG2_DEG4_C2 -6.7778401359918661e-01
  403. #define GTE_C_LOG2_DEG4_C3 +3.2118898377713379e-01
  404. #define GTE_C_LOG2_DEG4_C4 -8.2130717995088531e-02
  405. #define GTE_C_LOG2_DEG4_MAX_ERROR 1.1318551355360418e-4
  406. #define GTE_C_LOG2_DEG5_C1 +1.4419170408633741
  407. #define GTE_C_LOG2_DEG5_C2 -7.0909645927612530e-01
  408. #define GTE_C_LOG2_DEG5_C3 +4.1560609399164150e-01
  409. #define GTE_C_LOG2_DEG5_C4 -1.9357573729558908e-01
  410. #define GTE_C_LOG2_DEG5_C5 +4.5149061716699634e-02
  411. #define GTE_C_LOG2_DEG5_MAX_ERROR 1.5521274478735858e-5
  412. #define GTE_C_LOG2_DEG6_C1 +1.4425449435950917
  413. #define GTE_C_LOG2_DEG6_C2 -7.1814525675038965e-01
  414. #define GTE_C_LOG2_DEG6_C3 +4.5754919692564044e-01
  415. #define GTE_C_LOG2_DEG6_C4 -2.7790534462849337e-01
  416. #define GTE_C_LOG2_DEG6_C5 +1.2179791068763279e-01
  417. #define GTE_C_LOG2_DEG6_C6 -2.5841449829670182e-02
  418. #define GTE_C_LOG2_DEG6_MAX_ERROR 2.2162051216689793e-6
  419. #define GTE_C_LOG2_DEG7_C1 +1.4426664401536078
  420. #define GTE_C_LOG2_DEG7_C2 -7.2055423726162360e-01
  421. #define GTE_C_LOG2_DEG7_C3 +4.7332419162501083e-01
  422. #define GTE_C_LOG2_DEG7_C4 -3.2514018752954144e-01
  423. #define GTE_C_LOG2_DEG7_C5 +1.9302965529095673e-01
  424. #define GTE_C_LOG2_DEG7_C6 -7.8534970641157997e-02
  425. #define GTE_C_LOG2_DEG7_C7 +1.5209108363023915e-02
  426. #define GTE_C_LOG2_DEG7_MAX_ERROR 3.2546531700261561e-7
  427. #define GTE_C_LOG2_DEG8_C1 +1.4426896453621882
  428. #define GTE_C_LOG2_DEG8_C2 -7.2115893912535967e-01
  429. #define GTE_C_LOG2_DEG8_C3 +4.7861716616785088e-01
  430. #define GTE_C_LOG2_DEG8_C4 -3.4699935395019565e-01
  431. #define GTE_C_LOG2_DEG8_C5 +2.4114048765477492e-01
  432. #define GTE_C_LOG2_DEG8_C6 -1.3657398692885181e-01
  433. #define GTE_C_LOG2_DEG8_C7 +5.1421382871922106e-02
  434. #define GTE_C_LOG2_DEG8_C8 -9.1364020499895560e-03
  435. #define GTE_C_LOG2_DEG8_MAX_ERROR 4.8796219218050219e-8
  436. // These functions are convenient for some applications. The classes
  437. // BSNumber, BSRational and IEEEBinary16 have implementations that
  438. // (for now) use typecasting to call the 'float' or 'double' versions.
  439. namespace WwiseGTE
  440. {
  441. inline float atandivpi(float x)
  442. {
  443. return std::atan(x) * (float)GTE_C_INV_PI;
  444. }
  445. inline float atan2divpi(float y, float x)
  446. {
  447. return std::atan2(y, x) * (float)GTE_C_INV_PI;
  448. }
  449. inline float clamp(float x, float xmin, float xmax)
  450. {
  451. return (x <= xmin ? xmin : (x >= xmax ? xmax : x));
  452. }
  453. inline float cospi(float x)
  454. {
  455. return std::cos(x * (float)GTE_C_PI);
  456. }
  457. inline float exp10(float x)
  458. {
  459. return std::exp(x * (float)GTE_C_LN_10);
  460. }
  461. inline float invsqrt(float x)
  462. {
  463. return 1.0f / std::sqrt(x);
  464. }
  465. inline int isign(float x)
  466. {
  467. return (x > 0.0f ? 1 : (x < 0.0f ? -1 : 0));
  468. }
  469. inline float saturate(float x)
  470. {
  471. return (x <= 0.0f ? 0.0f : (x >= 1.0f ? 1.0f : x));
  472. }
  473. inline float sign(float x)
  474. {
  475. return (x > 0.0f ? 1.0f : (x < 0.0f ? -1.0f : 0.0f));
  476. }
  477. inline float sinpi(float x)
  478. {
  479. return std::sin(x * (float)GTE_C_PI);
  480. }
  481. inline float sqr(float x)
  482. {
  483. return x * x;
  484. }
  485. inline double atandivpi(double x)
  486. {
  487. return std::atan(x) * GTE_C_INV_PI;
  488. }
  489. inline double atan2divpi(double y, double x)
  490. {
  491. return std::atan2(y, x) * GTE_C_INV_PI;
  492. }
  493. inline double clamp(double x, double xmin, double xmax)
  494. {
  495. return (x <= xmin ? xmin : (x >= xmax ? xmax : x));
  496. }
  497. inline double cospi(double x)
  498. {
  499. return std::cos(x * GTE_C_PI);
  500. }
  501. inline double exp10(double x)
  502. {
  503. return std::exp(x * GTE_C_LN_10);
  504. }
  505. inline double invsqrt(double x)
  506. {
  507. return 1.0 / std::sqrt(x);
  508. }
  509. inline double sign(double x)
  510. {
  511. return (x > 0.0 ? 1.0 : (x < 0.0 ? -1.0 : 0.0f));
  512. }
  513. inline int isign(double x)
  514. {
  515. return (x > 0.0 ? 1 : (x < 0.0 ? -1 : 0));
  516. }
  517. inline double saturate(double x)
  518. {
  519. return (x <= 0.0 ? 0.0 : (x >= 1.0 ? 1.0 : x));
  520. }
  521. inline double sinpi(double x)
  522. {
  523. return std::sin(x * GTE_C_PI);
  524. }
  525. inline double sqr(double x)
  526. {
  527. return x * x;
  528. }
  529. }
  530. // Type traits to support std::enable_if conditional compilation for
  531. // numerical computations.
  532. namespace WwiseGTE
  533. {
  534. // The trait is_arbitrary_precision<T> for type T of float, double or
  535. // long double generates is_arbitrary_precision<T>::value of false. The
  536. // implementations for arbitrary-precision arithmetic are found in
  537. // GteArbitraryPrecision.h.
  538. template <typename T>
  539. struct is_arbitrary_precision_internal : std::false_type {};
  540. template <typename T>
  541. struct is_arbitrary_precision : is_arbitrary_precision_internal<T>::type {};
  542. // The trait has_division_operator<T> for type T of float, double or
  543. // long double generates has_division_operator<T>::value of true. The
  544. // implementations for arbitrary-precision arithmetic are found in
  545. // ArbitraryPrecision.h.
  546. template <typename T>
  547. struct has_division_operator_internal : std::false_type {};
  548. template <typename T>
  549. struct has_division_operator : has_division_operator_internal<T>::type {};
  550. template <>
  551. struct has_division_operator_internal<float> : std::true_type {};
  552. template <>
  553. struct has_division_operator_internal<double> : std::true_type {};
  554. template <>
  555. struct has_division_operator_internal<long double> : std::true_type {};
  556. }