# can't find error function (s/a "erf") in borland c++ libraries

This is a discussion on can't find error function (s/a "erf") in borland c++ libraries within the C++ Programming forums, part of the General Programming Boards category; hi, i need to use the error function (note i'm talking about the area under a gaussian curve, NOT any ...

1. ## can't find error function (s/a "erf") in borland c++ libraries

hi, i need to use the error function (note i'm talking about the area under a gaussian curve, NOT any sort of error handling in the language), and i can't find it in the libraries that came with borland c++.

i've programmed in c on a unix platform, and there was an erf(x) function in there. i find it hard to believe that this common function wouldn't be included in the package. does anyone know where it is? if i have to get it elsewhere, does anyone know a good source?

thanks
carrie

2. ## Preprocessor directives may help!

I haven't heard of that function. What may help if if you used a series of #if directives, followed by an #error mesage, and an #endif. This will only work if you were talking about calling a function in the event of an error instead of making a compiler error. In that case, you could just do the math yourself and do if statements then call the function.

Sean Mackrory
sean_mackrory@hotmail.com

3. Hmm... what exactly is the erf defined as?

Is this related to the area of one tail of a gaussian distribution (or can it be rewritten as such)?

There is no ANSI C stuff to handle this -- but the attached code can be used to get the area of a tail (UT for upper tail, LT for lower) of the standard normal gaussian curve.

Code:
```double probZUT(double z){
/*

Probability that a standard normal random variable has value >= z
(i.e. the area under the standard normal curve for Z in [z,+inf]

Originally adapted by Gary Perlman from a polynomial approximation in:
Ibbetson D, Algorithm 209
Collected Algorithms of the CACM 1963 p. 616

*/
double	y, x, w;
if (z == 0.0)
x = 0.0;
else{
y = 0.5 * fabs (z);
if (y >= (Z_MAX * 0.5))
x = 1.0;
else if (y < 1.0){
w = y*y;
x = ((((((((0.000124818987 * w
-0.001075204047) * w +0.005198775019) * w
-0.019198292004) * w +0.059054035642) * w
-0.151968751364) * w +0.319152932694) * w
-0.531923007300) * w +0.797884560593) * y * 2.0;
}
else{
y -= 2.0;
x = (((((((((((((-0.000045255659 * y
+0.000152529290) * y -0.000019538132) * y
-0.000676904986) * y +0.001390604284) * y
-0.000794620820) * y -0.002034254874) * y
+0.006549791214) * y -0.010557625006) * y
+0.011630447319) * y -0.009279453341) * y
+0.005353579108) * y -0.002141268741) * y
+0.000535310849) * y +0.999936657524;
}
}
return (z < 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
}

double probZLT(double z){
return 1.0 - probZUT(z);
}```

4. thanks, i'll have a look at that function.

(integral evaluated from 0 to x of the quantity exp(-t^2)dt

i'm looking at calling erf(x) around 10,000 times per reconstruction, so the speed might be a problem. a solution i'm toying with is to build a 2-d array (on the heap) with a running total of area (calculated by parabolic approximation or maybe that function) calculated every .01 or so along with an approximate/average height for that interval, format would be something like {{area1,height1},{area2,height2}...} and if the area i'm looking for is between area10 and area11 (for example), i could say areaX=area10+(height11*(x-.01*10))

i assume i should build this on the heap. i am uninterested in area past 3 standard deviations. would this be considered a really big array on most systems? (i have no idea how much space the heap usually has and whether a table is worth the speed.)