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:

    For Jeremy, direct, unmediated experience was always hard to take in, always more or less disquieting. Life became safe, things assumed meaning, only when they had been translated into words and confined between the covers of a book.
    Aldous Huxley (1894–1963)

    Advocating the mere tolerance of difference between women is the grossest reformism. It is a total denial of the creative function of difference in our lives. Difference must be not merely tolerated, but seen as a fund of necessary polarities between which our creativity can spark like a dialectic.
    Audre Lorde (1934–1992)