J
Jesper Lund
Bill Daly said:It is possible to compress the code further at the expense of
clarity (warning: this is not for the faint of heart):
double Phi(double x)
{long double s=x,t=0,b=x,x2=x*x,i=1;
while(s!=t) s=(t=s)+(b*=x2/(i+=2));
return .5+s*exp(-.5*x2-.91893853320467274178L);
}
This avoids the unnecessary computation of i+1 followed by the
unnecessary conversion of the result from int to long double,
eliminates the unnecessary variable pwr, and precomputes x*x.
Have you checked this algorithm in the tail? For, say, x = -50, I either
get inf/nan or an incorrect answer (0.5), depending on which compiler I
use. The compiler in my tests is gcc, specifically DJGPP or MingW. The
problem is overflow in s.
Furthermore, Phi(x) is not increasing in x for values between -13 and -7,
see below (DJGPP using long double).
x Phi(x)
-13 -2.94464e-15
-12 -2.89807e-15
-11 6.27672e-16
-10 6.49085e-16
-9 6.65294e-16
-8 1.28093e-15
-7 1.28042e-12
Best regards,
Jesper Lund