erf_inv.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // (C) Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_SF_ERF_INV_HPP
  6. #define BOOST_MATH_SF_ERF_INV_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #pragma warning(push)
  10. #pragma warning(disable:4127) // Conditional expression is constant
  11. #pragma warning(disable:4702) // Unreachable code: optimization warning
  12. #endif
  13. #include <type_traits>
  14. namespace boost{ namespace math{
  15. namespace detail{
  16. //
  17. // The inverse erf and erfc functions share a common implementation,
  18. // this version is for 80-bit long double's and smaller:
  19. //
  20. template <class T, class Policy>
  21. T erf_inv_imp(const T& p, const T& q, const Policy&, const std::integral_constant<int, 64>*)
  22. {
  23. BOOST_MATH_STD_USING // for ADL of std names.
  24. T result = 0;
  25. if(p <= 0.5)
  26. {
  27. //
  28. // Evaluate inverse erf using the rational approximation:
  29. //
  30. // x = p(p+10)(Y+R(p))
  31. //
  32. // Where Y is a constant, and R(p) is optimised for a low
  33. // absolute error compared to |Y|.
  34. //
  35. // double: Max error found: 2.001849e-18
  36. // long double: Max error found: 1.017064e-20
  37. // Maximum Deviation Found (actual error term at infinite precision) 8.030e-21
  38. //
  39. // LCOV_EXCL_START
  40. static const float Y = 0.0891314744949340820313f;
  41. static const T P[] = {
  42. BOOST_MATH_BIG_CONSTANT(T, 64, -0.000508781949658280665617),
  43. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00836874819741736770379),
  44. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0334806625409744615033),
  45. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0126926147662974029034),
  46. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0365637971411762664006),
  47. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0219878681111168899165),
  48. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00822687874676915743155),
  49. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00538772965071242932965)
  50. };
  51. static const T Q[] = {
  52. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  53. BOOST_MATH_BIG_CONSTANT(T, 64, -0.970005043303290640362),
  54. BOOST_MATH_BIG_CONSTANT(T, 64, -1.56574558234175846809),
  55. BOOST_MATH_BIG_CONSTANT(T, 64, 1.56221558398423026363),
  56. BOOST_MATH_BIG_CONSTANT(T, 64, 0.662328840472002992063),
  57. BOOST_MATH_BIG_CONSTANT(T, 64, -0.71228902341542847553),
  58. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0527396382340099713954),
  59. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0795283687341571680018),
  60. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00233393759374190016776),
  61. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000886216390456424707504)
  62. };
  63. // LCOV_EXCL_STOP
  64. T g = p * (p + 10);
  65. T r = tools::evaluate_polynomial(P, p) / tools::evaluate_polynomial(Q, p);
  66. result = g * Y + g * r;
  67. }
  68. else if(q >= 0.25)
  69. {
  70. //
  71. // Rational approximation for 0.5 > q >= 0.25
  72. //
  73. // x = sqrt(-2*log(q)) / (Y + R(q))
  74. //
  75. // Where Y is a constant, and R(q) is optimised for a low
  76. // absolute error compared to Y.
  77. //
  78. // double : Max error found: 7.403372e-17
  79. // long double : Max error found: 6.084616e-20
  80. // Maximum Deviation Found (error term) 4.811e-20
  81. //
  82. // LCOV_EXCL_START
  83. static const float Y = 2.249481201171875f;
  84. static const T P[] = {
  85. BOOST_MATH_BIG_CONSTANT(T, 64, -0.202433508355938759655),
  86. BOOST_MATH_BIG_CONSTANT(T, 64, 0.105264680699391713268),
  87. BOOST_MATH_BIG_CONSTANT(T, 64, 8.37050328343119927838),
  88. BOOST_MATH_BIG_CONSTANT(T, 64, 17.6447298408374015486),
  89. BOOST_MATH_BIG_CONSTANT(T, 64, -18.8510648058714251895),
  90. BOOST_MATH_BIG_CONSTANT(T, 64, -44.6382324441786960818),
  91. BOOST_MATH_BIG_CONSTANT(T, 64, 17.445385985570866523),
  92. BOOST_MATH_BIG_CONSTANT(T, 64, 21.1294655448340526258),
  93. BOOST_MATH_BIG_CONSTANT(T, 64, -3.67192254707729348546)
  94. };
  95. static const T Q[] = {
  96. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  97. BOOST_MATH_BIG_CONSTANT(T, 64, 6.24264124854247537712),
  98. BOOST_MATH_BIG_CONSTANT(T, 64, 3.9713437953343869095),
  99. BOOST_MATH_BIG_CONSTANT(T, 64, -28.6608180499800029974),
  100. BOOST_MATH_BIG_CONSTANT(T, 64, -20.1432634680485188801),
  101. BOOST_MATH_BIG_CONSTANT(T, 64, 48.5609213108739935468),
  102. BOOST_MATH_BIG_CONSTANT(T, 64, 10.8268667355460159008),
  103. BOOST_MATH_BIG_CONSTANT(T, 64, -22.6436933413139721736),
  104. BOOST_MATH_BIG_CONSTANT(T, 64, 1.72114765761200282724)
  105. };
  106. // LCOV_EXCL_STOP
  107. T g = sqrt(-2 * log(q));
  108. T xs = q - 0.25f;
  109. T r = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  110. result = g / (Y + r);
  111. }
  112. else
  113. {
  114. //
  115. // For q < 0.25 we have a series of rational approximations all
  116. // of the general form:
  117. //
  118. // let: x = sqrt(-log(q))
  119. //
  120. // Then the result is given by:
  121. //
  122. // x(Y+R(x-B))
  123. //
  124. // where Y is a constant, B is the lowest value of x for which
  125. // the approximation is valid, and R(x-B) is optimised for a low
  126. // absolute error compared to Y.
  127. //
  128. // Note that almost all code will really go through the first
  129. // or maybe second approximation. After than we're dealing with very
  130. // small input values indeed: 80 and 128 bit long double's go all the
  131. // way down to ~ 1e-5000 so the "tail" is rather long...
  132. //
  133. T x = sqrt(-log(q));
  134. if(x < 3)
  135. {
  136. // LCOV_EXCL_START
  137. // Max error found: 1.089051e-20
  138. static const float Y = 0.807220458984375f;
  139. static const T P[] = {
  140. BOOST_MATH_BIG_CONSTANT(T, 64, -0.131102781679951906451),
  141. BOOST_MATH_BIG_CONSTANT(T, 64, -0.163794047193317060787),
  142. BOOST_MATH_BIG_CONSTANT(T, 64, 0.117030156341995252019),
  143. BOOST_MATH_BIG_CONSTANT(T, 64, 0.387079738972604337464),
  144. BOOST_MATH_BIG_CONSTANT(T, 64, 0.337785538912035898924),
  145. BOOST_MATH_BIG_CONSTANT(T, 64, 0.142869534408157156766),
  146. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0290157910005329060432),
  147. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00214558995388805277169),
  148. BOOST_MATH_BIG_CONSTANT(T, 64, -0.679465575181126350155e-6),
  149. BOOST_MATH_BIG_CONSTANT(T, 64, 0.285225331782217055858e-7),
  150. BOOST_MATH_BIG_CONSTANT(T, 64, -0.681149956853776992068e-9)
  151. };
  152. static const T Q[] = {
  153. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  154. BOOST_MATH_BIG_CONSTANT(T, 64, 3.46625407242567245975),
  155. BOOST_MATH_BIG_CONSTANT(T, 64, 5.38168345707006855425),
  156. BOOST_MATH_BIG_CONSTANT(T, 64, 4.77846592945843778382),
  157. BOOST_MATH_BIG_CONSTANT(T, 64, 2.59301921623620271374),
  158. BOOST_MATH_BIG_CONSTANT(T, 64, 0.848854343457902036425),
  159. BOOST_MATH_BIG_CONSTANT(T, 64, 0.152264338295331783612),
  160. BOOST_MATH_BIG_CONSTANT(T, 64, 0.01105924229346489121)
  161. };
  162. // LCOV_EXCL_STOP
  163. T xs = x - 1.125f;
  164. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  165. result = Y * x + R * x;
  166. }
  167. else if(x < 6)
  168. {
  169. // LCOV_EXCL_START
  170. // Max error found: 8.389174e-21
  171. static const float Y = 0.93995571136474609375f;
  172. static const T P[] = {
  173. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0350353787183177984712),
  174. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00222426529213447927281),
  175. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0185573306514231072324),
  176. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00950804701325919603619),
  177. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00187123492819559223345),
  178. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000157544617424960554631),
  179. BOOST_MATH_BIG_CONSTANT(T, 64, 0.460469890584317994083e-5),
  180. BOOST_MATH_BIG_CONSTANT(T, 64, -0.230404776911882601748e-9),
  181. BOOST_MATH_BIG_CONSTANT(T, 64, 0.266339227425782031962e-11)
  182. };
  183. static const T Q[] = {
  184. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  185. BOOST_MATH_BIG_CONSTANT(T, 64, 1.3653349817554063097),
  186. BOOST_MATH_BIG_CONSTANT(T, 64, 0.762059164553623404043),
  187. BOOST_MATH_BIG_CONSTANT(T, 64, 0.220091105764131249824),
  188. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0341589143670947727934),
  189. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00263861676657015992959),
  190. BOOST_MATH_BIG_CONSTANT(T, 64, 0.764675292302794483503e-4)
  191. };
  192. // LCOV_EXCL_STOP
  193. T xs = x - 3;
  194. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  195. result = Y * x + R * x;
  196. }
  197. else if(x < 18)
  198. {
  199. // LCOV_EXCL_START
  200. // Max error found: 1.481312e-19
  201. static const float Y = 0.98362827301025390625f;
  202. static const T P[] = {
  203. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0167431005076633737133),
  204. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00112951438745580278863),
  205. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00105628862152492910091),
  206. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000209386317487588078668),
  207. BOOST_MATH_BIG_CONSTANT(T, 64, 0.149624783758342370182e-4),
  208. BOOST_MATH_BIG_CONSTANT(T, 64, 0.449696789927706453732e-6),
  209. BOOST_MATH_BIG_CONSTANT(T, 64, 0.462596163522878599135e-8),
  210. BOOST_MATH_BIG_CONSTANT(T, 64, -0.281128735628831791805e-13),
  211. BOOST_MATH_BIG_CONSTANT(T, 64, 0.99055709973310326855e-16)
  212. };
  213. static const T Q[] = {
  214. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  215. BOOST_MATH_BIG_CONSTANT(T, 64, 0.591429344886417493481),
  216. BOOST_MATH_BIG_CONSTANT(T, 64, 0.138151865749083321638),
  217. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0160746087093676504695),
  218. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000964011807005165528527),
  219. BOOST_MATH_BIG_CONSTANT(T, 64, 0.275335474764726041141e-4),
  220. BOOST_MATH_BIG_CONSTANT(T, 64, 0.282243172016108031869e-6)
  221. };
  222. // LCOV_EXCL_STOP
  223. T xs = x - 6;
  224. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  225. result = Y * x + R * x;
  226. }
  227. else if(x < 44)
  228. {
  229. // LCOV_EXCL_START
  230. // Max error found: 5.697761e-20
  231. static const float Y = 0.99714565277099609375f;
  232. static const T P[] = {
  233. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0024978212791898131227),
  234. BOOST_MATH_BIG_CONSTANT(T, 64, -0.779190719229053954292e-5),
  235. BOOST_MATH_BIG_CONSTANT(T, 64, 0.254723037413027451751e-4),
  236. BOOST_MATH_BIG_CONSTANT(T, 64, 0.162397777342510920873e-5),
  237. BOOST_MATH_BIG_CONSTANT(T, 64, 0.396341011304801168516e-7),
  238. BOOST_MATH_BIG_CONSTANT(T, 64, 0.411632831190944208473e-9),
  239. BOOST_MATH_BIG_CONSTANT(T, 64, 0.145596286718675035587e-11),
  240. BOOST_MATH_BIG_CONSTANT(T, 64, -0.116765012397184275695e-17)
  241. };
  242. static const T Q[] = {
  243. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  244. BOOST_MATH_BIG_CONSTANT(T, 64, 0.207123112214422517181),
  245. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0169410838120975906478),
  246. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000690538265622684595676),
  247. BOOST_MATH_BIG_CONSTANT(T, 64, 0.145007359818232637924e-4),
  248. BOOST_MATH_BIG_CONSTANT(T, 64, 0.144437756628144157666e-6),
  249. BOOST_MATH_BIG_CONSTANT(T, 64, 0.509761276599778486139e-9)
  250. };
  251. // LCOV_EXCL_STOP
  252. T xs = x - 18;
  253. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  254. result = Y * x + R * x;
  255. }
  256. else
  257. {
  258. // LCOV_EXCL_START
  259. // Max error found: 1.279746e-20
  260. static const float Y = 0.99941349029541015625f;
  261. static const T P[] = {
  262. BOOST_MATH_BIG_CONSTANT(T, 64, -0.000539042911019078575891),
  263. BOOST_MATH_BIG_CONSTANT(T, 64, -0.28398759004727721098e-6),
  264. BOOST_MATH_BIG_CONSTANT(T, 64, 0.899465114892291446442e-6),
  265. BOOST_MATH_BIG_CONSTANT(T, 64, 0.229345859265920864296e-7),
  266. BOOST_MATH_BIG_CONSTANT(T, 64, 0.225561444863500149219e-9),
  267. BOOST_MATH_BIG_CONSTANT(T, 64, 0.947846627503022684216e-12),
  268. BOOST_MATH_BIG_CONSTANT(T, 64, 0.135880130108924861008e-14),
  269. BOOST_MATH_BIG_CONSTANT(T, 64, -0.348890393399948882918e-21)
  270. };
  271. static const T Q[] = {
  272. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  273. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0845746234001899436914),
  274. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00282092984726264681981),
  275. BOOST_MATH_BIG_CONSTANT(T, 64, 0.468292921940894236786e-4),
  276. BOOST_MATH_BIG_CONSTANT(T, 64, 0.399968812193862100054e-6),
  277. BOOST_MATH_BIG_CONSTANT(T, 64, 0.161809290887904476097e-8),
  278. BOOST_MATH_BIG_CONSTANT(T, 64, 0.231558608310259605225e-11)
  279. };
  280. // LCOV_EXCL_STOP
  281. T xs = x - 44;
  282. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  283. result = Y * x + R * x;
  284. }
  285. }
  286. return result;
  287. }
  288. template <class T, class Policy>
  289. struct erf_roots
  290. {
  291. boost::math::tuple<T,T,T> operator()(const T& guess)
  292. {
  293. BOOST_MATH_STD_USING
  294. T derivative = sign * (2 / sqrt(constants::pi<T>())) * exp(-(guess * guess));
  295. T derivative2 = -2 * guess * derivative;
  296. return boost::math::make_tuple(((sign > 0) ? static_cast<T>(boost::math::erf(guess, Policy()) - target) : static_cast<T>(boost::math::erfc(guess, Policy())) - target), derivative, derivative2);
  297. }
  298. erf_roots(T z, int s) : target(z), sign(s) {}
  299. private:
  300. T target;
  301. int sign;
  302. };
  303. template <class T, class Policy>
  304. T erf_inv_imp(const T& p, const T& q, const Policy& pol, const std::integral_constant<int, 0>*)
  305. {
  306. //
  307. // Generic version, get a guess that's accurate to 64-bits (10^-19)
  308. //
  309. T guess = erf_inv_imp(p, q, pol, static_cast<std::integral_constant<int, 64> const*>(nullptr));
  310. T result;
  311. //
  312. // If T has more bit's than 64 in it's mantissa then we need to iterate,
  313. // otherwise we can just return the result:
  314. //
  315. if(policies::digits<T, Policy>() > 64)
  316. {
  317. std::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  318. if(p <= 0.5)
  319. {
  320. result = tools::halley_iterate(detail::erf_roots<typename std::remove_cv<T>::type, Policy>(p, 1), guess, static_cast<T>(0), tools::max_value<T>(), (policies::digits<T, Policy>() * 2) / 3, max_iter);
  321. }
  322. else
  323. {
  324. result = tools::halley_iterate(detail::erf_roots<typename std::remove_cv<T>::type, Policy>(q, -1), guess, static_cast<T>(0), tools::max_value<T>(), (policies::digits<T, Policy>() * 2) / 3, max_iter);
  325. }
  326. policies::check_root_iterations<T>("boost::math::erf_inv<%1%>", max_iter, pol);
  327. }
  328. else
  329. {
  330. result = guess;
  331. }
  332. return result;
  333. }
  334. } // namespace detail
  335. template <class T, class Policy>
  336. typename tools::promote_args<T>::type erfc_inv(T z, const Policy& pol)
  337. {
  338. typedef typename tools::promote_args<T>::type result_type;
  339. //
  340. // Begin by testing for domain errors, and other special cases:
  341. //
  342. static const char* function = "boost::math::erfc_inv<%1%>(%1%, %1%)";
  343. if((z < 0) || (z > 2))
  344. return policies::raise_domain_error<result_type>(function, "Argument outside range [0,2] in inverse erfc function (got p=%1%).", z, pol);
  345. if(z == 0)
  346. return policies::raise_overflow_error<result_type>(function, nullptr, pol);
  347. if(z == 2)
  348. return -policies::raise_overflow_error<result_type>(function, nullptr, pol);
  349. //
  350. // Normalise the input, so it's in the range [0,1], we will
  351. // negate the result if z is outside that range. This is a simple
  352. // application of the erfc reflection formula: erfc(-z) = 2 - erfc(z)
  353. //
  354. result_type p, q, s;
  355. if(z > 1)
  356. {
  357. q = 2 - z;
  358. p = 1 - q;
  359. s = -1;
  360. }
  361. else
  362. {
  363. p = 1 - z;
  364. q = z;
  365. s = 1;
  366. }
  367. //
  368. // A bit of meta-programming to figure out which implementation
  369. // to use, based on the number of bits in the mantissa of T:
  370. //
  371. typedef typename policies::precision<result_type, Policy>::type precision_type;
  372. typedef std::integral_constant<int,
  373. precision_type::value <= 0 ? 0 :
  374. precision_type::value <= 64 ? 64 : 0
  375. > tag_type;
  376. //
  377. // Likewise use internal promotion, so we evaluate at a higher
  378. // precision internally if it's appropriate:
  379. //
  380. typedef typename policies::evaluation<result_type, Policy>::type eval_type;
  381. typedef typename policies::normalise<
  382. Policy,
  383. policies::promote_float<false>,
  384. policies::promote_double<false>,
  385. policies::discrete_quantile<>,
  386. policies::assert_undefined<> >::type forwarding_policy;
  387. //
  388. // And get the result, negating where required:
  389. //
  390. return s * policies::checked_narrowing_cast<result_type, forwarding_policy>(
  391. detail::erf_inv_imp(static_cast<eval_type>(p), static_cast<eval_type>(q), forwarding_policy(), static_cast<tag_type const*>(nullptr)), function);
  392. }
  393. template <class T, class Policy>
  394. typename tools::promote_args<T>::type erf_inv(T z, const Policy& pol)
  395. {
  396. typedef typename tools::promote_args<T>::type result_type;
  397. //
  398. // Begin by testing for domain errors, and other special cases:
  399. //
  400. static const char* function = "boost::math::erf_inv<%1%>(%1%, %1%)";
  401. if((z < -1) || (z > 1))
  402. return policies::raise_domain_error<result_type>(function, "Argument outside range [-1, 1] in inverse erf function (got p=%1%).", z, pol);
  403. if(z == 1)
  404. return policies::raise_overflow_error<result_type>(function, nullptr, pol);
  405. if(z == -1)
  406. return -policies::raise_overflow_error<result_type>(function, nullptr, pol);
  407. if(z == 0)
  408. return 0;
  409. //
  410. // Normalise the input, so it's in the range [0,1], we will
  411. // negate the result if z is outside that range. This is a simple
  412. // application of the erf reflection formula: erf(-z) = -erf(z)
  413. //
  414. result_type p, q, s;
  415. if(z < 0)
  416. {
  417. p = -z;
  418. q = 1 - p;
  419. s = -1;
  420. }
  421. else
  422. {
  423. p = z;
  424. q = 1 - z;
  425. s = 1;
  426. }
  427. //
  428. // A bit of meta-programming to figure out which implementation
  429. // to use, based on the number of bits in the mantissa of T:
  430. //
  431. typedef typename policies::precision<result_type, Policy>::type precision_type;
  432. typedef std::integral_constant<int,
  433. precision_type::value <= 0 ? 0 :
  434. precision_type::value <= 64 ? 64 : 0
  435. > tag_type;
  436. //
  437. // Likewise use internal promotion, so we evaluate at a higher
  438. // precision internally if it's appropriate:
  439. //
  440. typedef typename policies::evaluation<result_type, Policy>::type eval_type;
  441. typedef typename policies::normalise<
  442. Policy,
  443. policies::promote_float<false>,
  444. policies::promote_double<false>,
  445. policies::discrete_quantile<>,
  446. policies::assert_undefined<> >::type forwarding_policy;
  447. //
  448. // Likewise use internal promotion, so we evaluate at a higher
  449. // precision internally if it's appropriate:
  450. //
  451. typedef typename policies::evaluation<result_type, Policy>::type eval_type;
  452. //
  453. // And get the result, negating where required:
  454. //
  455. return s * policies::checked_narrowing_cast<result_type, forwarding_policy>(
  456. detail::erf_inv_imp(static_cast<eval_type>(p), static_cast<eval_type>(q), forwarding_policy(), static_cast<tag_type const*>(nullptr)), function);
  457. }
  458. template <class T>
  459. inline typename tools::promote_args<T>::type erfc_inv(T z)
  460. {
  461. return erfc_inv(z, policies::policy<>());
  462. }
  463. template <class T>
  464. inline typename tools::promote_args<T>::type erf_inv(T z)
  465. {
  466. return erf_inv(z, policies::policy<>());
  467. }
  468. } // namespace math
  469. } // namespace boost
  470. #ifdef _MSC_VER
  471. #pragma warning(pop)
  472. #endif
  473. #endif // BOOST_MATH_SF_ERF_INV_HPP