ndtrl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* ndtrl.c
  2. *
  3. * Normal distribution function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * long double x, y, ndtrl();
  10. *
  11. * y = ndtrl( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the area under the Gaussian probability density
  18. * function, integrated from minus infinity to x:
  19. *
  20. * x
  21. * -
  22. * 1 | | 2
  23. * ndtr(x) = --------- | exp( - t /2 ) dt
  24. * sqrt(2pi) | |
  25. * -
  26. * -inf.
  27. *
  28. * = ( 1 + erf(z) ) / 2
  29. * = erfc(z) / 2
  30. *
  31. * where z = x/sqrt(2). Computation is via the functions
  32. * erf and erfc.
  33. *
  34. *
  35. * ACCURACY:
  36. *
  37. * Relative error:
  38. * arithmetic domain # trials peak rms
  39. * IEEE -13,0 30000 1.6e-17 2.9e-18
  40. * IEEE -150.7,0 2000 1.6e-15 3.8e-16
  41. * Accuracy is limited by error amplification in computing exp(-x^2).
  42. *
  43. *
  44. * ERROR MESSAGES:
  45. *
  46. * message condition value returned
  47. * erfcl underflow x^2 / 2 > MAXLOGL 0.0
  48. *
  49. */
  50. /* erfl.c
  51. *
  52. * Error function
  53. *
  54. *
  55. *
  56. * SYNOPSIS:
  57. *
  58. * long double x, y, erfl();
  59. *
  60. * y = erfl( x );
  61. *
  62. *
  63. *
  64. * DESCRIPTION:
  65. *
  66. * The integral is
  67. *
  68. * x
  69. * -
  70. * 2 | | 2
  71. * erf(x) = -------- | exp( - t ) dt.
  72. * sqrt(pi) | |
  73. * -
  74. * 0
  75. *
  76. * The magnitude of x is limited to about 106.56 for IEEE
  77. * arithmetic; 1 or -1 is returned outside this range.
  78. *
  79. * For 0 <= |x| < 1, erf(x) = x * P6(x^2)/Q6(x^2); otherwise
  80. * erf(x) = 1 - erfc(x).
  81. *
  82. *
  83. *
  84. * ACCURACY:
  85. *
  86. * Relative error:
  87. * arithmetic domain # trials peak rms
  88. * IEEE 0,1 50000 2.0e-19 5.7e-20
  89. *
  90. */
  91. /* erfcl.c
  92. *
  93. * Complementary error function
  94. *
  95. *
  96. *
  97. * SYNOPSIS:
  98. *
  99. * long double x, y, erfcl();
  100. *
  101. * y = erfcl( x );
  102. *
  103. *
  104. *
  105. * DESCRIPTION:
  106. *
  107. *
  108. * 1 - erf(x) =
  109. *
  110. * inf.
  111. * -
  112. * 2 | | 2
  113. * erfc(x) = -------- | exp( - t ) dt
  114. * sqrt(pi) | |
  115. * -
  116. * x
  117. *
  118. *
  119. * For small x, erfc(x) = 1 - erf(x); otherwise rational
  120. * approximations are computed.
  121. *
  122. *
  123. *
  124. * ACCURACY:
  125. *
  126. * Relative error:
  127. * arithmetic domain # trials peak rms
  128. * IEEE 0,13 20000 7.0e-18 1.8e-18
  129. * IEEE 0,106.56 10000 4.4e-16 1.2e-16
  130. * Accuracy is limited by error amplification in computing exp(-x^2).
  131. *
  132. *
  133. * ERROR MESSAGES:
  134. *
  135. * message condition value returned
  136. * erfcl underflow x^2 > MAXLOGL 0.0
  137. *
  138. *
  139. */
  140. /*
  141. Cephes Math Library Release 2.3: January, 1995
  142. Copyright 1984, 1995 by Stephen L. Moshier
  143. */
  144. #include <math.h>
  145. extern long double MAXLOGL;
  146. static long double SQRTHL = 7.071067811865475244008e-1L;
  147. /* erfc(x) = exp(-x^2) P(1/x)/Q(1/x)
  148. 1/8 <= 1/x <= 1
  149. Peak relative error 5.8e-21 */
  150. #if UNK
  151. static long double P[10] = {
  152. 1.130609921802431462353E9L,
  153. 2.290171954844785638925E9L,
  154. 2.295563412811856278515E9L,
  155. 1.448651275892911637208E9L,
  156. 6.234814405521647580919E8L,
  157. 1.870095071120436715930E8L,
  158. 3.833161455208142870198E7L,
  159. 4.964439504376477951135E6L,
  160. 3.198859502299390825278E5L,
  161. -9.085943037416544232472E-6L,
  162. };
  163. static long double Q[10] = {
  164. /* 1.000000000000000000000E0L, */
  165. 1.130609910594093747762E9L,
  166. 3.565928696567031388910E9L,
  167. 5.188672873106859049556E9L,
  168. 4.588018188918609726890E9L,
  169. 2.729005809811924550999E9L,
  170. 1.138778654945478547049E9L,
  171. 3.358653716579278063988E8L,
  172. 6.822450775590265689648E7L,
  173. 8.799239977351261077610E6L,
  174. 5.669830829076399819566E5L,
  175. };
  176. #endif
  177. #if IBMPC
  178. static short P[] = {
  179. 0x4bf0,0x9ad8,0x7a03,0x86c7,0x401d, XPD
  180. 0xdf23,0xd843,0x4032,0x8881,0x401e, XPD
  181. 0xd025,0xcfd5,0x8494,0x88d3,0x401e, XPD
  182. 0xb6d0,0xc92b,0x5417,0xacb1,0x401d, XPD
  183. 0xada8,0x356a,0x4982,0x94a6,0x401c, XPD
  184. 0x4e13,0xcaee,0x9e31,0xb258,0x401a, XPD
  185. 0x5840,0x554d,0x37a3,0x9239,0x4018, XPD
  186. 0x3b58,0x3da2,0xaf02,0x9780,0x4015, XPD
  187. 0x0144,0x489e,0xbe68,0x9c31,0x4011, XPD
  188. 0x333b,0xd9e6,0xd404,0x986f,0xbfee, XPD
  189. };
  190. static short Q[] = {
  191. /* 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD */
  192. 0x0e43,0x302d,0x79ed,0x86c7,0x401d, XPD
  193. 0xf817,0x9128,0xc0f8,0xd48b,0x401e, XPD
  194. 0x8eae,0x8dad,0x6eb4,0x9aa2,0x401f, XPD
  195. 0x00e7,0x7595,0xcd06,0x88bb,0x401f, XPD
  196. 0x4991,0xcfda,0x52f1,0xa2a9,0x401e, XPD
  197. 0xc39d,0xe415,0xc43d,0x87c0,0x401d, XPD
  198. 0xa75d,0x436f,0x30dd,0xa027,0x401b, XPD
  199. 0xc4cb,0x305a,0xbf78,0x8220,0x4019, XPD
  200. 0x3708,0x33b1,0x07fa,0x8644,0x4016, XPD
  201. 0x24fa,0x96f6,0x7153,0x8a6c,0x4012, XPD
  202. };
  203. #endif
  204. #if MIEEE
  205. static long P[30] = {
  206. 0x401d0000,0x86c77a03,0x9ad84bf0,
  207. 0x401e0000,0x88814032,0xd843df23,
  208. 0x401e0000,0x88d38494,0xcfd5d025,
  209. 0x401d0000,0xacb15417,0xc92bb6d0,
  210. 0x401c0000,0x94a64982,0x356aada8,
  211. 0x401a0000,0xb2589e31,0xcaee4e13,
  212. 0x40180000,0x923937a3,0x554d5840,
  213. 0x40150000,0x9780af02,0x3da23b58,
  214. 0x40110000,0x9c31be68,0x489e0144,
  215. 0xbfee0000,0x986fd404,0xd9e6333b,
  216. };
  217. static long Q[30] = {
  218. /* 0x3fff0000,0x80000000,0x00000000, */
  219. 0x401d0000,0x86c779ed,0x302d0e43,
  220. 0x401e0000,0xd48bc0f8,0x9128f817,
  221. 0x401f0000,0x9aa26eb4,0x8dad8eae,
  222. 0x401f0000,0x88bbcd06,0x759500e7,
  223. 0x401e0000,0xa2a952f1,0xcfda4991,
  224. 0x401d0000,0x87c0c43d,0xe415c39d,
  225. 0x401b0000,0xa02730dd,0x436fa75d,
  226. 0x40190000,0x8220bf78,0x305ac4cb,
  227. 0x40160000,0x864407fa,0x33b13708,
  228. 0x40120000,0x8a6c7153,0x96f624fa,
  229. };
  230. #endif
  231. /* erfc(x) = exp(-x^2) 1/x R(1/x^2) / S(1/x^2)
  232. 1/128 <= 1/x < 1/8
  233. Peak relative error 1.9e-21 */
  234. #if UNK
  235. static long double R[5] = {
  236. 3.621349282255624026891E0L,
  237. 7.173690522797138522298E0L,
  238. 3.445028155383625172464E0L,
  239. 5.537445669807799246891E-1L,
  240. 2.697535671015506686136E-2L,
  241. };
  242. static long double S[5] = {
  243. /* 1.000000000000000000000E0L, */
  244. 1.072884067182663823072E1L,
  245. 1.533713447609627196926E1L,
  246. 6.572990478128949439509E0L,
  247. 1.005392977603322982436E0L,
  248. 4.781257488046430019872E-2L,
  249. };
  250. #endif
  251. #if IBMPC
  252. static short R[] = {
  253. 0x260a,0xab95,0x2fc7,0xe7c4,0x4000, XPD
  254. 0x4761,0x613e,0xdf6d,0xe58e,0x4001, XPD
  255. 0x0615,0x4b00,0x575f,0xdc7b,0x4000, XPD
  256. 0x521d,0x8527,0x3435,0x8dc2,0x3ffe, XPD
  257. 0x22cf,0xc711,0x6c5b,0xdcfb,0x3ff9, XPD
  258. };
  259. static short S[] = {
  260. /* 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD */
  261. 0x5de6,0x17d7,0x54d6,0xaba9,0x4002, XPD
  262. 0x55d5,0xd300,0xe71e,0xf564,0x4002, XPD
  263. 0xb611,0x8f76,0xf020,0xd255,0x4001, XPD
  264. 0x3684,0x3798,0xb793,0x80b0,0x3fff, XPD
  265. 0xf5af,0x2fb2,0x1e57,0xc3d7,0x3ffa, XPD
  266. };
  267. #endif
  268. #if MIEEE
  269. static long R[15] = {
  270. 0x40000000,0xe7c42fc7,0xab95260a,
  271. 0x40010000,0xe58edf6d,0x613e4761,
  272. 0x40000000,0xdc7b575f,0x4b000615,
  273. 0x3ffe0000,0x8dc23435,0x8527521d,
  274. 0x3ff90000,0xdcfb6c5b,0xc71122cf,
  275. };
  276. static long S[15] = {
  277. /* 0x3fff0000,0x80000000,0x00000000, */
  278. 0x40020000,0xaba954d6,0x17d75de6,
  279. 0x40020000,0xf564e71e,0xd30055d5,
  280. 0x40010000,0xd255f020,0x8f76b611,
  281. 0x3fff0000,0x80b0b793,0x37983684,
  282. 0x3ffa0000,0xc3d71e57,0x2fb2f5af,
  283. };
  284. #endif
  285. /* erf(x) = x P(x^2)/Q(x^2)
  286. 0 <= x <= 1
  287. Peak relative error 7.6e-23 */
  288. #if UNK
  289. static long double T[7] = {
  290. 1.097496774521124996496E-1L,
  291. 5.402980370004774841217E0L,
  292. 2.871822526820825849235E2L,
  293. 2.677472796799053019985E3L,
  294. 4.825977363071025440855E4L,
  295. 1.549905740900882313773E5L,
  296. 1.104385395713178565288E6L,
  297. };
  298. static long double U[6] = {
  299. /* 1.000000000000000000000E0L, */
  300. 4.525777638142203713736E1L,
  301. 9.715333124857259246107E2L,
  302. 1.245905812306219011252E4L,
  303. 9.942956272177178491525E4L,
  304. 4.636021778692893773576E5L,
  305. 9.787360737578177599571E5L,
  306. };
  307. #endif
  308. #if IBMPC
  309. static short T[] = {
  310. 0xfd7a,0x3a1a,0x705b,0xe0c4,0x3ffb, XPD
  311. 0x3128,0xc337,0x3716,0xace5,0x4001, XPD
  312. 0x9517,0x4e93,0x540e,0x8f97,0x4007, XPD
  313. 0x6118,0x6059,0x9093,0xa757,0x400a, XPD
  314. 0xb954,0xa987,0xc60c,0xbc83,0x400e, XPD
  315. 0x7a56,0xe45a,0xa4bd,0x975b,0x4010, XPD
  316. 0xc446,0x6bab,0x0b2a,0x86d0,0x4013, XPD
  317. };
  318. static short U[] = {
  319. /* 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD */
  320. 0x3453,0x1f8e,0xf688,0xb507,0x4004, XPD
  321. 0x71ac,0xb12f,0x21ca,0xf2e2,0x4008, XPD
  322. 0xffe8,0x9cac,0x3b84,0xc2ac,0x400c, XPD
  323. 0x481d,0x445b,0xc807,0xc232,0x400f, XPD
  324. 0x9ad5,0x1aef,0x45b1,0xe25e,0x4011, XPD
  325. 0x71a7,0x1cad,0x012e,0xeef3,0x4012, XPD
  326. };
  327. #endif
  328. #if MIEEE
  329. static long T[21] = {
  330. 0x3ffb0000,0xe0c4705b,0x3a1afd7a,
  331. 0x40010000,0xace53716,0xc3373128,
  332. 0x40070000,0x8f97540e,0x4e939517,
  333. 0x400a0000,0xa7579093,0x60596118,
  334. 0x400e0000,0xbc83c60c,0xa987b954,
  335. 0x40100000,0x975ba4bd,0xe45a7a56,
  336. 0x40130000,0x86d00b2a,0x6babc446,
  337. };
  338. static long U[18] = {
  339. /* 0x3fff0000,0x80000000,0x00000000, */
  340. 0x40040000,0xb507f688,0x1f8e3453,
  341. 0x40080000,0xf2e221ca,0xb12f71ac,
  342. 0x400c0000,0xc2ac3b84,0x9cacffe8,
  343. 0x400f0000,0xc232c807,0x445b481d,
  344. 0x40110000,0xe25e45b1,0x1aef9ad5,
  345. 0x40120000,0xeef3012e,0x1cad71a7,
  346. };
  347. #endif
  348. #ifdef ANSIPROT
  349. extern long double polevll ( long double, void *, int );
  350. extern long double p1evll ( long double, void *, int );
  351. extern long double expl ( long double );
  352. extern long double logl ( long double );
  353. extern long double erfl ( long double );
  354. extern long double erfcl ( long double );
  355. extern long double fabsl ( long double );
  356. #else
  357. long double polevll(), p1evll(), expl(), logl(), erfl(), erfcl(), fabsl();
  358. #endif
  359. #ifdef INFINITIES
  360. extern long double INFINITYL;
  361. #endif
  362. long double ndtrl(a)
  363. long double a;
  364. {
  365. long double x, y, z;
  366. x = a * SQRTHL;
  367. z = fabsl(x);
  368. if( z < SQRTHL )
  369. y = 0.5L + 0.5L * erfl(x);
  370. else
  371. {
  372. y = 0.5L * erfcl(z);
  373. if( x > 0.0L )
  374. y = 1.0L - y;
  375. }
  376. return(y);
  377. }
  378. long double erfcl(a)
  379. long double a;
  380. {
  381. long double p,q,x,y,z;
  382. #ifdef INFINITIES
  383. if( a == INFINITYL )
  384. return(0.0L);
  385. if( a == -INFINITYL )
  386. return(2.0L);
  387. #endif
  388. if( a < 0.0L )
  389. x = -a;
  390. else
  391. x = a;
  392. if( x < 1.0L )
  393. return( 1.0L - erfl(a) );
  394. z = -a * a;
  395. if( z < -MAXLOGL )
  396. {
  397. under:
  398. mtherr( "erfcl", UNDERFLOW );
  399. if( a < 0 )
  400. return( 2.0L );
  401. else
  402. return( 0.0L );
  403. }
  404. z = expl(z);
  405. y = 1.0L/x;
  406. if( x < 8.0L )
  407. {
  408. p = polevll( y, P, 9 );
  409. q = p1evll( y, Q, 10 );
  410. }
  411. else
  412. {
  413. q = y * y;
  414. p = y * polevll( q, R, 4 );
  415. q = p1evll( q, S, 5 );
  416. }
  417. y = (z * p)/q;
  418. if( a < 0.0L )
  419. y = 2.0L - y;
  420. if( y == 0.0L )
  421. goto under;
  422. return(y);
  423. }
  424. long double erfl(x)
  425. long double x;
  426. {
  427. long double y, z;
  428. #if MINUSZERO
  429. if( x == 0.0L )
  430. return(x);
  431. #endif
  432. #ifdef INFINITIES
  433. if( x == -INFINITYL )
  434. return(-1.0L);
  435. if( x == INFINITYL )
  436. return(1.0L);
  437. #endif
  438. if( fabsl(x) > 1.0L )
  439. return( 1.0L - erfcl(x) );
  440. z = x * x;
  441. y = x * polevll( z, T, 6 ) / p1evll( z, U, 6 );
  442. return( y );
  443. }