home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / LIBC / LIBC-4.6 / LIBC-4 / libc-linux / sysdeps / linux / i386 / math / erf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-21  |  2.6 KB  |  127 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.  The Berkeley software License Agreement
  4.  * specifies the terms and conditions for redistribution.
  5.  */
  6.  
  7. #if 0
  8. #ifndef lint
  9. static char sccsid[] = "@(#)erf.c    5.2 (Berkeley) 4/29/88";
  10. #endif /* not lint */
  11. #endif
  12.  
  13. /*
  14.     C program for floating point error function
  15.  
  16.     erf(x) returns the error function of its argument
  17.     erfc(x) returns 1.0-erf(x)
  18.  
  19.     erf(x) is defined by
  20.     ${2 over sqrt(pi)} int from 0 to x e sup {-t sup 2} dt$
  21.  
  22.     the entry for erfc is provided because of the
  23.     extreme loss of relative accuracy if erf(x) is
  24.     called for large x and the result subtracted
  25.     from 1. (e.g. for x= 10, 12 places are lost).
  26.  
  27.     There are no error returns.
  28.  
  29.     Calls exp.
  30.  
  31.     Coefficients for large x are #5667 from Hart & Cheney (18.72D).
  32. */
  33.  
  34. #include <math.h>
  35.  
  36. #define M 7
  37. #define N 9
  38. static double torp = 1.1283791670955125738961589031;
  39. static double p1[] = {
  40.     0.804373630960840172832162e5,
  41.     0.740407142710151470082064e4,
  42.     0.301782788536507577809226e4,
  43.     0.380140318123903008244444e2,
  44.     0.143383842191748205576712e2,
  45.     -.288805137207594084924010e0,
  46.     0.007547728033418631287834e0,
  47. };
  48. static double q1[]  = {
  49.     0.804373630960840172826266e5,
  50.     0.342165257924628539769006e5,
  51.     0.637960017324428279487120e4,
  52.     0.658070155459240506326937e3,
  53.     0.380190713951939403753468e2,
  54.     0.100000000000000000000000e1,
  55.     0.0,
  56. };
  57. static double p2[]  = {
  58.     0.18263348842295112592168999e4,
  59.     0.28980293292167655611275846e4,
  60.     0.2320439590251635247384768711e4,
  61.     0.1143262070703886173606073338e4,
  62.     0.3685196154710010637133875746e3,
  63.     0.7708161730368428609781633646e2,
  64.     0.9675807882987265400604202961e1,
  65.     0.5641877825507397413087057563e0,
  66.     0.0,
  67. };
  68. static double q2[]  = {
  69.     0.18263348842295112595576438e4,
  70.     0.495882756472114071495438422e4,
  71.     0.60895424232724435504633068e4,
  72.     0.4429612803883682726711528526e4,
  73.     0.2094384367789539593790281779e4,
  74.     0.6617361207107653469211984771e3,
  75.     0.1371255960500622202878443578e3,
  76.     0.1714980943627607849376131193e2,
  77.     1.0,
  78. };
  79.  
  80. double
  81. erf(double arg)
  82. {
  83.     int sign;
  84.     double argsq;
  85.     double d, n;
  86.     int i;
  87.  
  88.     sign = 1;
  89.     if(arg < 0.){
  90.         arg = -arg;
  91.         sign = -1;
  92.     }
  93.     if(arg < 0.5){
  94.         argsq = arg*arg;
  95.         for(n=0,d=0,i=M-1; i>=0; i--){
  96.             n = n*argsq + p1[i];
  97.             d = d*argsq + q1[i];
  98.         }
  99.         return(sign*torp*arg*n/d);
  100.     }
  101.     if(arg >= 10.)
  102.         return(sign*1.);
  103.     return(sign*(1. - erfc(arg)));
  104. }
  105.  
  106. double
  107. erfc(double arg)
  108. {
  109.     double n, d;
  110.     int i;
  111.  
  112.     if(arg < 0.)
  113.         return(2. - erfc(-arg));
  114. /*
  115.     if(arg < 0.5)
  116.         return(1. - erf(arg));
  117. */
  118.     if(arg >= 10.)
  119.         return(0.);
  120.  
  121.     for(n=0,d=0,i=N-1; i>=0; i--){
  122.         n = n*arg + p2[i];
  123.         d = d*arg + q2[i];
  124.     }
  125.     return(exp(-arg*arg)*n/d);
  126. }
  127.