erf_inv.hpp 23 KB

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