BC Programming Language - A Translated C Function

A Translated C Function

Because the syntax of bc is similar to that of C, published numerical functions written in C can often be translated into BC quite easily, which immediately provides the arbitrary precision of BC. For example, in the Journal of Statistical Software (July 2004, Volume 11, Issue 5), George Marsaglia published the following C code for the cumulative normal distribution:

double Phi(double x) { long double s=x,t=0,b=x,q=x*x,i=1; while(s!=t) s=(t=s)+(b*=q/(i+=2)); return .5+s*exp(-.5*q-.91893853320467274178L); }

With some necessary changes to accommodate bc's different syntax, and realizing that the constant "0.9189..." is actually log(2*PI)/2, this can be translated to the following GNU bc code:

define phi(x) { auto s,t,b,q,i,const s=x; t=0; b=x; q=x*x; i=1 while(s!=t) s=(t=s)+(b*=q/(i+=2)) const=0.5*l(8*a(1)) # 0.91893... return .5+s*e(-.5*q-const) }

Read more about this topic:  Bc Programming Language

Famous quotes containing the words translated and/or function:

    God employs several translators; some pieces are translated by age, some by sickness, some by war, some by justice.
    John Donne (c. 1572–1631)

    My function in life is not to be a politician in Parliament: it is to get something done.
    Bernadette Devlin (b. 1947)