Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*                                                      erfl.c
  2.  *
  3.  *      Error function
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * long double x, y, erfl();
  10.  *
  11.  * y = erfl( x );
  12.  *
  13.  *
  14.  *
  15.  * DESCRIPTION:
  16.  *
  17.  * The integral is
  18.  *
  19.  *                           x
  20.  *                            -
  21.  *                 2         | |          2
  22.  *   erf(x)  =  --------     |    exp( - t  ) dt.
  23.  *              sqrt(pi)   | |
  24.  *                          -
  25.  *                           0
  26.  *
  27.  * The magnitude of x is limited to about 106.56 for IEEE
  28.  * arithmetic; 1 or -1 is returned outside this range.
  29.  *
  30.  * For 0 <= |x| < 1, erf(x) = x * P6(x^2)/Q6(x^2);
  31.  * Otherwise: erf(x) = 1 - erfc(x).
  32.  *
  33.  *
  34.  *
  35.  * ACCURACY:
  36.  *
  37.  *                      Relative error:
  38.  * arithmetic   domain     # trials      peak         rms
  39.  *    IEEE      0,1         50000       2.0e-19     5.7e-20
  40.  *
  41.  */
  42. /*                                                      erfcl.c
  43.  *
  44.  *      Complementary error function
  45.  *
  46.  *
  47.  *
  48.  * SYNOPSIS:
  49.  *
  50.  * long double x, y, erfcl();
  51.  *
  52.  * y = erfcl( x );
  53.  *
  54.  *
  55.  *
  56.  * DESCRIPTION:
  57.  *
  58.  *
  59.  *  1 - erf(x) =
  60.  *
  61.  *                           inf.
  62.  *                             -
  63.  *                  2         | |          2
  64.  *   erfc(x)  =  --------     |    exp( - t  ) dt
  65.  *               sqrt(pi)   | |
  66.  *                           -
  67.  *                            x
  68.  *
  69.  *
  70.  * For small x, erfc(x) = 1 - erf(x); otherwise rational
  71.  * approximations are computed.
  72.  *
  73.  * A special function expx2l.c is used to suppress error amplification
  74.  * in computing exp(-x^2).
  75.  *
  76.  *
  77.  * ACCURACY:
  78.  *
  79.  *                      Relative error:
  80.  * arithmetic   domain     # trials      peak         rms
  81.  *    IEEE      0,13        50000      8.4e-19      9.7e-20
  82.  *    IEEE      6,106.56    20000      2.9e-19      7.1e-20
  83.  *
  84.  *
  85.  * ERROR MESSAGES:
  86.  *
  87.  *   message          condition              value returned
  88.  * erfcl underflow    x^2 > MAXLOGL              0.0
  89.  *
  90.  *
  91.  */
  92.  
  93. /*
  94. Modified from file ndtrl.c
  95. Cephes Math Library Release 2.3:  January, 1995
  96. Copyright 1984, 1995 by Stephen L. Moshier
  97. */
  98.  
  99. #include <math.h>
  100. #include "cephes_mconf.h"
  101.  
  102. /* erfc(x) = exp(-x^2) P(1/x)/Q(1/x)
  103.    1/8 <= 1/x <= 1
  104.    Peak relative error 5.8e-21  */
  105.  
  106. static const unsigned short P[] = {
  107. 0x4bf0,0x9ad8,0x7a03,0x86c7,0x401d, XPD
  108. 0xdf23,0xd843,0x4032,0x8881,0x401e, XPD
  109. 0xd025,0xcfd5,0x8494,0x88d3,0x401e, XPD
  110. 0xb6d0,0xc92b,0x5417,0xacb1,0x401d, XPD
  111. 0xada8,0x356a,0x4982,0x94a6,0x401c, XPD
  112. 0x4e13,0xcaee,0x9e31,0xb258,0x401a, XPD
  113. 0x5840,0x554d,0x37a3,0x9239,0x4018, XPD
  114. 0x3b58,0x3da2,0xaf02,0x9780,0x4015, XPD
  115. 0x0144,0x489e,0xbe68,0x9c31,0x4011, XPD
  116. 0x333b,0xd9e6,0xd404,0x986f,0xbfee, XPD
  117. };
  118. static const unsigned short Q[] = {
  119. /* 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD */
  120. 0x0e43,0x302d,0x79ed,0x86c7,0x401d, XPD
  121. 0xf817,0x9128,0xc0f8,0xd48b,0x401e, XPD
  122. 0x8eae,0x8dad,0x6eb4,0x9aa2,0x401f, XPD
  123. 0x00e7,0x7595,0xcd06,0x88bb,0x401f, XPD
  124. 0x4991,0xcfda,0x52f1,0xa2a9,0x401e, XPD
  125. 0xc39d,0xe415,0xc43d,0x87c0,0x401d, XPD
  126. 0xa75d,0x436f,0x30dd,0xa027,0x401b, XPD
  127. 0xc4cb,0x305a,0xbf78,0x8220,0x4019, XPD
  128. 0x3708,0x33b1,0x07fa,0x8644,0x4016, XPD
  129. 0x24fa,0x96f6,0x7153,0x8a6c,0x4012, XPD
  130. };
  131.  
  132. /* erfc(x) = exp(-x^2) 1/x R(1/x^2) / S(1/x^2)
  133.    1/128 <= 1/x < 1/8
  134.    Peak relative error 1.9e-21  */
  135.  
  136. static const unsigned short R[] = {
  137. 0x260a,0xab95,0x2fc7,0xe7c4,0x4000, XPD
  138. 0x4761,0x613e,0xdf6d,0xe58e,0x4001, XPD
  139. 0x0615,0x4b00,0x575f,0xdc7b,0x4000, XPD
  140. 0x521d,0x8527,0x3435,0x8dc2,0x3ffe, XPD
  141. 0x22cf,0xc711,0x6c5b,0xdcfb,0x3ff9, XPD
  142. };
  143. static const unsigned short S[] = {
  144. /* 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD */
  145. 0x5de6,0x17d7,0x54d6,0xaba9,0x4002, XPD
  146. 0x55d5,0xd300,0xe71e,0xf564,0x4002, XPD
  147. 0xb611,0x8f76,0xf020,0xd255,0x4001, XPD
  148. 0x3684,0x3798,0xb793,0x80b0,0x3fff, XPD
  149. 0xf5af,0x2fb2,0x1e57,0xc3d7,0x3ffa, XPD
  150. };
  151.  
  152. /* erf(x)  = x T(x^2)/U(x^2)
  153.    0 <= x <= 1
  154.    Peak relative error 7.6e-23  */
  155.  
  156. static const unsigned short T[] = {
  157. 0xfd7a,0x3a1a,0x705b,0xe0c4,0x3ffb, XPD
  158. 0x3128,0xc337,0x3716,0xace5,0x4001, XPD
  159. 0x9517,0x4e93,0x540e,0x8f97,0x4007, XPD
  160. 0x6118,0x6059,0x9093,0xa757,0x400a, XPD
  161. 0xb954,0xa987,0xc60c,0xbc83,0x400e, XPD
  162. 0x7a56,0xe45a,0xa4bd,0x975b,0x4010, XPD
  163. 0xc446,0x6bab,0x0b2a,0x86d0,0x4013, XPD
  164. };
  165.  
  166. static const unsigned short U[] = {
  167. /* 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD */
  168. 0x3453,0x1f8e,0xf688,0xb507,0x4004, XPD
  169. 0x71ac,0xb12f,0x21ca,0xf2e2,0x4008, XPD
  170. 0xffe8,0x9cac,0x3b84,0xc2ac,0x400c, XPD
  171. 0x481d,0x445b,0xc807,0xc232,0x400f, XPD
  172. 0x9ad5,0x1aef,0x45b1,0xe25e,0x4011, XPD
  173. 0x71a7,0x1cad,0x012e,0xeef3,0x4012, XPD
  174. };
  175.  
  176. /*                                                      expx2l.c
  177.  *
  178.  *      Exponential of squared argument
  179.  *
  180.  *
  181.  *
  182.  * SYNOPSIS:
  183.  *
  184.  * long double x, y, expmx2l();
  185.  * int sign;
  186.  *
  187.  * y = expx2l( x );
  188.  *
  189.  *
  190.  *
  191.  * DESCRIPTION:
  192.  *
  193.  * Computes y = exp(x*x) while suppressing error amplification
  194.  * that would ordinarily arise from the inexactness of the
  195.  * exponential argument x*x.
  196.  *
  197.  *
  198.  *
  199.  * ACCURACY:
  200.  *
  201.  *                      Relative error:
  202.  * arithmetic      domain        # trials      peak         rms
  203.  *   IEEE     -106.566, 106.566    10^5       1.6e-19     4.4e-20
  204.  *
  205.  */
  206.  
  207. #define M 32768.0L
  208. #define MINV 3.0517578125e-5L
  209.  
  210. static long double expx2l (long double x)
  211. {
  212.   long double u, u1, m, f;
  213.  
  214.   x = fabsl (x);
  215.   /* Represent x as an exact multiple of M plus a residual.
  216.      M is a power of 2 chosen so that exp(m * m) does not overflow
  217.      or underflow and so that |x - m| is small.  */
  218.   m = MINV * floorl(M * x + 0.5L);
  219.   f = x - m;
  220.  
  221.   /* x^2 = m^2 + 2mf + f^2 */
  222.   u = m * m;
  223.   u1 = 2 * m * f  +  f * f;
  224.  
  225.   if ((u+u1) > MAXLOGL)
  226.     return (INFINITYL);
  227.  
  228.   /* u is exact, u1 is small.  */
  229.   u = expl(u) * expl(u1);
  230.   return(u);
  231. }
  232.  
  233. long double erfcl(long double a)
  234. {
  235. long double p,q,x,y,z;
  236.  
  237. if (isinf (a))
  238.         return (signbit (a) ? 2.0 : 0.0);
  239.  
  240. x = fabsl (a);
  241.  
  242. if (x < 1.0L)
  243.         return (1.0L - erfl(a));
  244.  
  245. z = a * a;
  246.  
  247. if( z  > MAXLOGL )
  248.         {
  249. under:
  250.         mtherr( "erfcl", UNDERFLOW );
  251.         errno = ERANGE;
  252.         return (signbit (a) ? 2.0 : 0.0);
  253.         }
  254.  
  255. /* Compute z = expl(a * a).  */
  256. z = expx2l (a);
  257. y = 1.0L/x;
  258.  
  259. if (x < 8.0L)
  260.         {
  261.         p = polevll (y, P, 9);
  262.         q = p1evll (y, Q, 10);
  263.         }
  264. else
  265.         {
  266.         q = y * y;
  267.         p = y * polevll (q, R, 4);
  268.         q = p1evll (q, S, 5);
  269.         }
  270. y = p/(q * z);
  271.  
  272. if (a < 0.0L)
  273.         y = 2.0L - y;
  274.  
  275. if (y == 0.0L)
  276.         goto under;
  277.  
  278. return (y);
  279. }
  280.  
  281. long double erfl(long double x)
  282. {
  283. long double y, z;
  284.  
  285. if( x == 0.0L )
  286.         return (x);
  287.  
  288. if (isinf (x))
  289.         return (signbit (x) ?  -1.0L : 1.0L);
  290.  
  291. if (fabsl(x) > 1.0L)
  292.         return (1.0L - erfcl (x));
  293.  
  294. z = x * x;
  295. y = x * polevll( z, T, 6 ) / p1evll( z, U, 6 );
  296. return( y );
  297. }
  298.