The Elk Code
 
Loading...
Searching...
No Matches
erf.f90
Go to the documentation of this file.
1
2! This code is based on a routine from the NSWC Library of Mathematics
3! Subroutines and is in the public domain.
4
5!BOP
6! !ROUTINE: erf
7! !INTERFACE:
8elemental real(8) function erf(x)
9! !INPUT/OUTPUT PARAMETERS:
10! x : real argument (in,real)
11! !DESCRIPTION:
12! Returns the error function ${\rm erf}(x)$ using a rational function
13! approximation. This procedure is numerically stable and accurate to near
14! machine precision.
15!
16! !REVISION HISTORY:
17! Modified version of a NSWC routine, April 2003 (JKD)
18!EOP
19!BOC
20implicit none
21! arguments
22real(8), intent(in) :: x
23! local variables
24real(8) ax,x2,t,bot,top
25real(8), parameter :: c=0.564189583547756d0
26real(8), parameter :: &
27 a(5)=[ 0.771058495001320d-04,-0.133733772997339d-02, 0.323076579225834d-01, &
28 0.479137145607681d-01, 0.128379167095513d+00], &
29 b(3)=[ 0.301048631703895d-02, 0.538971687740286d-01, 0.375795757275549d+00], &
30 p(8)=[-1.36864857382717d-07, 5.64195517478974d-01, 7.21175825088309d+00, &
31 4.31622272220567d+01, 1.52989285046940d+02, 3.39320816734344d+02, &
32 4.51918953711873d+02, 3.00459261020162d+02], &
33 q(8)=[ 1.00000000000000d+00, 1.27827273196294d+01, 7.70001529352295d+01, &
34 2.77585444743988d+02, 6.38980264465631d+02, 9.31354094850610d+02, &
35 7.90950925327898d+02, 3.00459260956983d+02], &
36 r(5)=[ 2.10144126479064d+00, 2.62370141675169d+01, 2.13688200555087d+01, &
37 4.65807828718470d+00, 2.82094791773523d-01], &
38 s(4)=[ 9.41537750555460d+01, 1.87114811799590d+02, 9.90191814623914d+01, &
39 1.80124575948747d+01]
40ax=abs(x)
41if (ax < 0.5d0) then
42 t=x**2
43 top=((((a(1)*t+a(2))*t+a(3))*t+a(4))*t+a(5))+1.d0
44 bot=((b(1)*t+b(2))*t+b(3))*t+1.d0
45 erf=x*(top/bot)
46 return
47end if
48if (ax < 4.d0) then
49 top=((((((p(1)*ax+p(2))*ax+p(3))*ax+p(4))*ax+p(5))*ax+p(6))*ax+p(7))*ax+p(8)
50 bot=((((((q(1)*ax+q(2))*ax+q(3))*ax+q(4))*ax+q(5))*ax+q(6))*ax+q(7))*ax+q(8)
51 erf=0.5d0+(0.5d0-exp(-x**2)*top/bot)
52 if (x < 0.d0) erf=-erf
53 return
54end if
55if (ax < 5.8d0) then
56 x2=x**2
57 t=1.d0/x2
58 top=(((r(1)*t+r(2))*t+r(3))*t+r(4))*t+r(5)
59 bot=(((s(1)*t+s(2))*t+s(3))*t+s(4))*t+1.d0
60 erf=(c-top/(x2*bot))/ax
61 erf=0.5d0+(0.5d0-exp(-x2)*erf)
62 if (x < 0.d0) erf=-erf
63 return
64end if
65erf=sign(1.d0,x)
66end function
67!EOC
68
elemental real(8) function erf(x)
Definition erf.f90:9