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