1 /* mpfr_erf -- error function of a floating-point number
3 Copyright 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by Ludovic Meunier and Paul Zimmermann.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 #define EXP1 2.71828182845904523536 /* exp(1) */
28 static int mpfr_erf_0 (mpfr_ptr
, mpfr_srcptr
, double, mp_rnd_t
);
31 mpfr_erf (mpfr_ptr y
, mpfr_srcptr x
, mp_rnd_t rnd_mode
)
35 MPFR_SAVE_EXPO_DECL (expo
);
37 MPFR_LOG_FUNC (("x[%#R]=%R rnd=%d", x
, x
, rnd_mode
),
38 ("y[%#R]=%R inexact=%d", y
, y
, inex
));
40 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x
)))
47 else if (MPFR_IS_INF (x
)) /* erf(+inf) = +1, erf(-inf) = -1 */
48 return mpfr_set_si (y
, MPFR_INT_SIGN (x
), GMP_RNDN
);
49 else /* erf(+0) = +0, erf(-0) = -0 */
51 MPFR_ASSERTD (MPFR_IS_ZERO (x
));
52 return mpfr_set (y
, x
, GMP_RNDN
); /* should keep the sign of x */
56 /* now x is neither NaN, Inf nor 0 */
58 /* first try expansion at x=0 when x is small, or asymptotic expansion
61 MPFR_SAVE_EXPO_MARK (expo
);
63 /* around x=0, we have erf(x) = 2x/sqrt(Pi) (1 - x^2/3 + ...),
64 with 1 - x^2/3 <= sqrt(Pi)*erf(x)/2/x <= 1 for x >= 0. This means that
65 if x^2/3 < 2^(-PREC(y)-1) we can decide of the correct rounding,
66 unless we have a worst-case for 2x/sqrt(Pi). */
67 if (MPFR_EXP(x
) < - (mp_exp_t
) (MPFR_PREC(y
) / 2))
69 /* we use 2x/sqrt(Pi) (1 - x^2/3) <= erf(x) <= 2x/sqrt(Pi) for x > 0
70 and 2x/sqrt(Pi) <= erf(x) <= 2x/sqrt(Pi) (1 - x^2/3) for x < 0.
71 In both cases |2x/sqrt(Pi) (1 - x^2/3)| <= |erf(x)| <= |2x/sqrt(Pi)|.
72 We will compute l and h such that l <= |2x/sqrt(Pi) (1 - x^2/3)|
73 and |2x/sqrt(Pi)| <= h. If l and h round to the same value to
74 precision PREC(y) and rounding rnd_mode, then we are done. */
75 mpfr_t l
, h
; /* lower and upper bounds for erf(x) */
78 mpfr_init2 (l
, MPFR_PREC(y
) + 17);
79 mpfr_init2 (h
, MPFR_PREC(y
) + 17);
81 mpfr_mul (l
, x
, x
, GMP_RNDU
);
82 mpfr_div_ui (l
, l
, 3, GMP_RNDU
); /* upper bound on x^2/3 */
83 mpfr_ui_sub (l
, 1, l
, GMP_RNDZ
); /* lower bound on 1 - x^2/3 */
84 mpfr_const_pi (h
, GMP_RNDU
); /* upper bound of Pi */
85 mpfr_sqrt (h
, h
, GMP_RNDU
); /* upper bound on sqrt(Pi) */
86 mpfr_div (l
, l
, h
, GMP_RNDZ
); /* lower bound on 1/sqrt(Pi) (1 - x^2/3) */
87 mpfr_mul_2ui (l
, l
, 1, GMP_RNDZ
); /* 2/sqrt(Pi) (1 - x^2/3) */
88 mpfr_mul (l
, l
, x
, GMP_RNDZ
); /* |l| is a lower bound on
89 |2x/sqrt(Pi) (1 - x^2/3)| */
91 mpfr_const_pi (h
, GMP_RNDD
); /* lower bound on Pi */
92 mpfr_sqrt (h
, h
, GMP_RNDD
); /* lower bound on sqrt(Pi) */
93 mpfr_div_2ui (h
, h
, 1, GMP_RNDD
); /* lower bound on sqrt(Pi)/2 */
94 /* since sqrt(Pi)/2 < 1, the following should not underflow */
95 mpfr_div (h
, x
, h
, MPFR_IS_POS(x
) ? GMP_RNDU
: GMP_RNDD
);
96 /* round l and h to precision PREC(y) */
97 inex
= mpfr_prec_round (l
, MPFR_PREC(y
), rnd_mode
);
98 inex2
= mpfr_prec_round (h
, MPFR_PREC(y
), rnd_mode
);
99 /* Caution: we also need inex=inex2 (inex might be 0). */
100 ok
= SAME_SIGN (inex
, inex2
) && mpfr_cmp (l
, h
) == 0;
102 mpfr_set (y
, h
, rnd_mode
);
107 /* this test can still fail for small precision, for example
108 for x=-0.100E-2 with a target precision of 3 bits, since
109 the error term x^2/3 is not that small. */
113 mpfr_const_log2 (xf
, GMP_RNDU
);
114 mpfr_div (xf
, x
, xf
, GMP_RNDZ
); /* round to zero ensures we get a lower
115 bound of |x/log(2)| */
116 mpfr_mul (xf
, xf
, x
, GMP_RNDZ
);
117 large
= mpfr_cmp_ui (xf
, MPFR_PREC (y
) + 1) > 0;
120 /* when x goes to infinity, we have erf(x) = 1 - 1/sqrt(Pi)/exp(x^2)/x + ...
121 and |erf(x) - 1| <= exp(-x^2) is true for any x >= 0, thus if
122 exp(-x^2) < 2^(-PREC(y)-1) the result is 1 or 1-epsilon.
123 This rewrites as x^2/log(2) > p+1. */
124 if (MPFR_UNLIKELY (large
))
125 /* |erf x| = 1 or 1- */
127 mp_rnd_t rnd2
= MPFR_IS_POS (x
) ? rnd_mode
: MPFR_INVERT_RND(rnd_mode
);
128 if (rnd2
== GMP_RNDN
|| rnd2
== GMP_RNDU
)
130 inex
= MPFR_INT_SIGN (x
);
131 mpfr_set_si (y
, inex
, rnd2
);
133 else /* round to zero */
135 inex
= -MPFR_INT_SIGN (x
);
136 mpfr_setmax (y
, 0); /* warning: setmax keeps the old sign of y */
137 MPFR_SET_SAME_SIGN (y
, x
);
140 else /* use Taylor */
144 /* FIXME: get rid of doubles/mpfr_get_d here */
145 xf2
= mpfr_get_d (x
, GMP_RNDN
);
146 xf2
= xf2
* xf2
; /* xf2 ~ x^2 */
147 inex
= mpfr_erf_0 (y
, x
, xf2
, rnd_mode
);
151 MPFR_SAVE_EXPO_FREE (expo
);
152 return mpfr_check_range (y
, inex
, rnd_mode
);
157 mul_2exp (double x
, mp_exp_t e
)
173 /* evaluates erf(x) using the expansion at x=0:
175 erf(x) = 2/sqrt(Pi) * sum((-1)^k*x^(2k+1)/k!/(2k+1), k=0..infinity)
177 Assumes x is neither NaN nor infinite nor zero.
178 Assumes also that e*x^2 <= n (target precision).
181 mpfr_erf_0 (mpfr_ptr res
, mpfr_srcptr x
, double xf2
, mp_rnd_t rnd_mode
)
184 mp_exp_t nuk
, sigmak
;
190 MPFR_ZIV_DECL (loop
);
192 n
= MPFR_PREC (res
); /* target precision */
194 /* initial working precision */
195 m
= n
+ (mp_prec_t
) (xf2
/ LOG2
) + 8 + MPFR_INT_CEIL_LOG2 (n
);
202 MPFR_ZIV_INIT (loop
, m
);
205 mpfr_mul (y
, x
, x
, GMP_RNDU
); /* err <= 1 ulp */
206 mpfr_set_ui (s
, 1, GMP_RNDN
);
207 mpfr_set_ui (t
, 1, GMP_RNDN
);
212 mpfr_mul (t
, y
, t
, GMP_RNDU
);
213 mpfr_div_ui (t
, t
, k
, GMP_RNDU
);
214 mpfr_div_ui (u
, t
, 2 * k
+ 1, GMP_RNDU
);
215 sigmak
= MPFR_GET_EXP (s
);
217 mpfr_sub (s
, s
, u
, GMP_RNDN
);
219 mpfr_add (s
, s
, u
, GMP_RNDN
);
220 sigmak
-= MPFR_GET_EXP(s
);
221 nuk
= MPFR_GET_EXP(u
) - MPFR_GET_EXP(s
);
223 if ((nuk
< - (mp_exp_t
) m
) && ((double) k
>= xf2
))
226 /* tauk <- 1/2 + tauk * 2^sigmak + (1+8k)*2^nuk */
227 tauk
= 0.5 + mul_2exp (tauk
, sigmak
)
228 + mul_2exp (1.0 + 8.0 * (double) k
, nuk
);
231 mpfr_mul (s
, x
, s
, GMP_RNDU
);
232 MPFR_SET_EXP (s
, MPFR_GET_EXP (s
) + 1);
234 mpfr_const_pi (t
, GMP_RNDZ
);
235 mpfr_sqrt (t
, t
, GMP_RNDZ
);
236 mpfr_div (s
, s
, t
, GMP_RNDN
);
237 tauk
= 4.0 * tauk
+ 11.0; /* final ulp-error on s */
238 log2tauk
= __gmpfr_ceil_log2 (tauk
);
240 if (MPFR_LIKELY (MPFR_CAN_ROUND (s
, m
- log2tauk
, n
, rnd_mode
)))
243 /* Actualisation of the precision */
244 MPFR_ZIV_NEXT (loop
, m
);
245 mpfr_set_prec (y
, m
);
246 mpfr_set_prec (s
, m
);
247 mpfr_set_prec (t
, m
);
248 mpfr_set_prec (u
, m
);
251 MPFR_ZIV_FREE (loop
);
253 inex
= mpfr_set (res
, s
, rnd_mode
);