math_erf.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package math
  2. // The original C code and the long comment below are
  3. // from FreeBSD's /usr/src/lib/msun/src/s_erf.c and
  4. // came with this notice.
  5. //
  6. // ====================================================
  7. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  8. //
  9. // Developed at SunPro, a Sun Microsystems, Inc. business.
  10. // Permission to use, copy, modify, and distribute this
  11. // software is freely granted, provided that this notice
  12. // is preserved.
  13. // ====================================================
  14. //
  15. //
  16. // double erf(double x)
  17. // double erfc(double x)
  18. // x
  19. // 2 |\
  20. // erf(x) = --------- | exp(-t*t)dt
  21. // sqrt(pi) \|
  22. // 0
  23. //
  24. // erfc(x) = 1-erf(x)
  25. // Note that
  26. // erf(-x) = -erf(x)
  27. // erfc(-x) = 2 - erfc(x)
  28. //
  29. // Method:
  30. // 1. For |x| in [0, 0.84375]
  31. // erf(x) = x + x*R(x**2)
  32. // erfc(x) = 1 - erf(x) if x in [-.84375,0.25]
  33. // = 0.5 + ((0.5-x)-x*R) if x in [0.25,0.84375]
  34. // where R = P/Q where P is an odd poly of degree 8 and
  35. // Q is an odd poly of degree 10.
  36. // -57.90
  37. // | R - (erf(x)-x)/x | <= 2
  38. //
  39. //
  40. // Remark. The formula is derived by noting
  41. // erf(x) = (2/sqrt(pi))*(x - x**3/3 + x**5/10 - x**7/42 + ....)
  42. // and that
  43. // 2/sqrt(pi) = 1.128379167095512573896158903121545171688
  44. // is close to one. The interval is chosen because the fix
  45. // point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
  46. // near 0.6174), and by some experiment, 0.84375 is chosen to
  47. // guarantee the error is less than one ulp for erf.
  48. //
  49. // 2. For |x| in [0.84375,1.25], let s = |x| - 1, and
  50. // c = 0.84506291151 rounded to single (24 bits)
  51. // erf(x) = sign(x) * (c + P1(s)/Q1(s))
  52. // erfc(x) = (1-c) - P1(s)/Q1(s) if x > 0
  53. // 1+(c+P1(s)/Q1(s)) if x < 0
  54. // |P1/Q1 - (erf(|x|)-c)| <= 2**-59.06
  55. // Remark: here we use the taylor series expansion at x=1.
  56. // erf(1+s) = erf(1) + s*Poly(s)
  57. // = 0.845.. + P1(s)/Q1(s)
  58. // That is, we use rational approximation to approximate
  59. // erf(1+s) - (c = (single)0.84506291151)
  60. // Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
  61. // where
  62. // P1(s) = degree 6 poly in s
  63. // Q1(s) = degree 6 poly in s
  64. //
  65. // 3. For x in [1.25,1/0.35(~2.857143)],
  66. // erfc(x) = (1/x)*exp(-x*x-0.5625+R1/S1)
  67. // erf(x) = 1 - erfc(x)
  68. // where
  69. // R1(z) = degree 7 poly in z, (z=1/x**2)
  70. // S1(z) = degree 8 poly in z
  71. //
  72. // 4. For x in [1/0.35,28]
  73. // erfc(x) = (1/x)*exp(-x*x-0.5625+R2/S2) if x > 0
  74. // = 2.0 - (1/x)*exp(-x*x-0.5625+R2/S2) if -6<x<0
  75. // = 2.0 - tiny (if x <= -6)
  76. // erf(x) = sign(x)*(1.0 - erfc(x)) if x < 6, else
  77. // erf(x) = sign(x)*(1.0 - tiny)
  78. // where
  79. // R2(z) = degree 6 poly in z, (z=1/x**2)
  80. // S2(z) = degree 7 poly in z
  81. //
  82. // Note1:
  83. // To compute exp(-x*x-0.5625+R/S), let s be a single
  84. // precision number and s := x; then
  85. // -x*x = -s*s + (s-x)*(s+x)
  86. // exp(-x*x-0.5626+R/S) =
  87. // exp(-s*s-0.5625)*exp((s-x)*(s+x)+R/S);
  88. // Note2:
  89. // Here 4 and 5 make use of the asymptotic series
  90. // exp(-x*x)
  91. // erfc(x) ~ ---------- * ( 1 + Poly(1/x**2) )
  92. // x*sqrt(pi)
  93. // We use rational approximation to approximate
  94. // g(s)=f(1/x**2) = log(erfc(x)*x) - x*x + 0.5625
  95. // Here is the error bound for R1/S1 and R2/S2
  96. // |R1/S1 - f(x)| < 2**(-62.57)
  97. // |R2/S2 - f(x)| < 2**(-61.52)
  98. //
  99. // 5. For inf > x >= 28
  100. // erf(x) = sign(x) *(1 - tiny) (raise inexact)
  101. // erfc(x) = tiny*tiny (raise underflow) if x > 0
  102. // = 2 - tiny if x<0
  103. //
  104. // 7. Special case:
  105. // erf(0) = 0, erf(inf) = 1, erf(-inf) = -1,
  106. // erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2,
  107. // erfc/erf(NaN) is NaN
  108. erf :: proc{
  109. erf_f16,
  110. erf_f16le,
  111. erf_f16be,
  112. erf_f32,
  113. erf_f32le,
  114. erf_f32be,
  115. erf_f64,
  116. }
  117. @(require_results) erf_f16 :: proc "contextless" (x: f16) -> f16 { return f16(erf_f64(f64(x))) }
  118. @(require_results) erf_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(erf_f64(f64(x))) }
  119. @(require_results) erf_f16be :: proc "contextless" (x: f16be) -> f16be { return f16be(erf_f64(f64(x))) }
  120. @(require_results) erf_f32 :: proc "contextless" (x: f32) -> f32 { return f32(erf_f64(f64(x))) }
  121. @(require_results) erf_f32le :: proc "contextless" (x: f32le) -> f32le { return f32le(erf_f64(f64(x))) }
  122. @(require_results) erf_f32be :: proc "contextless" (x: f32be) -> f32be { return f32be(erf_f64(f64(x))) }
  123. @(require_results)
  124. erf_f64 :: proc "contextless" (x: f64) -> f64 {
  125. erx :: 0h3FEB0AC160000000
  126. // Coefficients for approximation to erf in [0, 0.84375]
  127. efx :: 0h3FC06EBA8214DB69
  128. efx8 :: 0h3FF06EBA8214DB69
  129. pp0 :: 0h3FC06EBA8214DB68
  130. pp1 :: 0hBFD4CD7D691CB913
  131. pp2 :: 0hBF9D2A51DBD7194F
  132. pp3 :: 0hBF77A291236668E4
  133. pp4 :: 0hBEF8EAD6120016AC
  134. qq1 :: 0h3FD97779CDDADC09
  135. qq2 :: 0h3FB0A54C5536CEBA
  136. qq3 :: 0h3F74D022C4D36B0F
  137. qq4 :: 0h3F215DC9221C1A10
  138. qq5 :: 0hBED09C4342A26120
  139. // Coefficients for approximation to erf in [0.84375, 1.25]
  140. pa0 :: 0hBF6359B8BEF77538
  141. pa1 :: 0h3FDA8D00AD92B34D
  142. pa2 :: 0hBFD7D240FBB8C3F1
  143. pa3 :: 0h3FD45FCA805120E4
  144. pa4 :: 0hBFBC63983D3E28EC
  145. pa5 :: 0h3FA22A36599795EB
  146. pa6 :: 0hBF61BF380A96073F
  147. qa1 :: 0h3FBB3E6618EEE323
  148. qa2 :: 0h3FE14AF092EB6F33
  149. qa3 :: 0h3FB2635CD99FE9A7
  150. qa4 :: 0h3FC02660E763351F
  151. qa5 :: 0h3F8BEDC26B51DD1C
  152. qa6 :: 0h3F888B545735151D
  153. // Coefficients for approximation to erfc in [1.25, 1/0.35]
  154. ra0 :: 0hBF843412600D6435
  155. ra1 :: 0hBFE63416E4BA7360
  156. ra2 :: 0hC0251E0441B0E726
  157. ra3 :: 0hC04F300AE4CBA38D
  158. ra4 :: 0hC0644CB184282266
  159. ra5 :: 0hC067135CEBCCABB2
  160. ra6 :: 0hC054526557E4D2F2
  161. ra7 :: 0hC023A0EFC69AC25C
  162. sa1 :: 0h4033A6B9BD707687
  163. sa2 :: 0h4061350C526AE721
  164. sa3 :: 0h407B290DD58A1A71
  165. sa4 :: 0h40842B1921EC2868
  166. sa5 :: 0h407AD02157700314
  167. sa6 :: 0h405B28A3EE48AE2C
  168. sa7 :: 0h401A47EF8E484A93
  169. sa8 :: 0hBFAEEFF2EE749A62
  170. // Coefficients for approximation to erfc in [1/.35, 28]
  171. rb0 :: 0hBF84341239E86F4A
  172. rb1 :: 0hBFE993BA70C285DE
  173. rb2 :: 0hC031C209555F995A
  174. rb3 :: 0hC064145D43C5ED98
  175. rb4 :: 0hC083EC881375F228
  176. rb5 :: 0hC09004616A2E5992
  177. rb6 :: 0hC07E384E9BDC383F
  178. sb1 :: 0h403E568B261D5190
  179. sb2 :: 0h40745CAE221B9F0A
  180. sb3 :: 0h409802EB189D5118
  181. sb4 :: 0h40A8FFB7688C246A
  182. sb5 :: 0h40A3F219CEDF3BE6
  183. sb6 :: 0h407DA874E79FE763
  184. sb7 :: 0hC03670E242712D62
  185. VERY_TINY :: 0h0080000000000000
  186. SMALL :: 1.0 / (1 << 28) // 2**-28
  187. // special cases
  188. switch {
  189. case is_nan(x):
  190. return nan_f64()
  191. case is_inf(x, 1):
  192. return 1
  193. case is_inf(x, -1):
  194. return -1
  195. }
  196. x := x
  197. sign := false
  198. if x < 0 {
  199. x = -x
  200. sign = true
  201. }
  202. if x < 0.84375 { // |x| < 0.84375
  203. temp: f64
  204. if x < SMALL { // |x| < 2**-28
  205. if x < VERY_TINY {
  206. temp = 0.125 * (8.0*x + efx8*x) // avoid underflow
  207. } else {
  208. temp = x + efx*x
  209. }
  210. } else {
  211. z := x * x
  212. r := pp0 + z*(pp1+z*(pp2+z*(pp3+z*pp4)))
  213. s := 1 + z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
  214. y := r / s
  215. temp = x + x*y
  216. }
  217. if sign {
  218. return -temp
  219. }
  220. return temp
  221. }
  222. if x < 1.25 { // 0.84375 <= |x| < 1.25
  223. s := x - 1
  224. P := pa0 + s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))))
  225. Q := 1 + s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))))
  226. if sign {
  227. return -erx - P/Q
  228. }
  229. return erx + P/Q
  230. }
  231. if x >= 6 { // inf > |x| >= 6
  232. if sign {
  233. return -1
  234. }
  235. return 1
  236. }
  237. s := 1 / (x * x)
  238. R, S: f64
  239. if x < 1/0.35 { // |x| < 1 / 0.35 ~ 2.857143
  240. R = ra0 + s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7))))))
  241. S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
  242. } else { // |x| >= 1 / 0.35 ~ 2.857143
  243. R = rb0 + s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6)))))
  244. S = 1 + s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7))))))
  245. }
  246. z := transmute(f64)(0xffffffff00000000 & transmute(u64)x) // pseudo-single (20-bit) precision x
  247. r := exp(-z*z-0.5625) * exp((z-x)*(z+x)+R/S)
  248. if sign {
  249. return r/x - 1
  250. }
  251. return 1 - r/x
  252. }
  253. erfc :: proc{
  254. erfc_f16,
  255. erfc_f16le,
  256. erfc_f16be,
  257. erfc_f32,
  258. erfc_f32le,
  259. erfc_f32be,
  260. erfc_f64,
  261. }
  262. @(require_results) erfc_f16 :: proc "contextless" (x: f16) -> f16 { return f16(erfc_f64(f64(x))) }
  263. @(require_results) erfc_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(erfc_f64(f64(x))) }
  264. @(require_results) erfc_f16be :: proc "contextless" (x: f16be) -> f16be { return f16be(erfc_f64(f64(x))) }
  265. @(require_results) erfc_f32 :: proc "contextless" (x: f32) -> f32 { return f32(erfc_f64(f64(x))) }
  266. @(require_results) erfc_f32le :: proc "contextless" (x: f32le) -> f32le { return f32le(erfc_f64(f64(x))) }
  267. @(require_results) erfc_f32be :: proc "contextless" (x: f32be) -> f32be { return f32be(erfc_f64(f64(x))) }
  268. @(require_results)
  269. erfc_f64 :: proc "contextless" (x: f64) -> f64 {
  270. erx :: 0h3FEB0AC160000000
  271. // Coefficients for approximation to erf in [0, 0.84375]
  272. efx :: 0h3FC06EBA8214DB69
  273. efx8 :: 0h3FF06EBA8214DB69
  274. pp0 :: 0h3FC06EBA8214DB68
  275. pp1 :: 0hBFD4CD7D691CB913
  276. pp2 :: 0hBF9D2A51DBD7194F
  277. pp3 :: 0hBF77A291236668E4
  278. pp4 :: 0hBEF8EAD6120016AC
  279. qq1 :: 0h3FD97779CDDADC09
  280. qq2 :: 0h3FB0A54C5536CEBA
  281. qq3 :: 0h3F74D022C4D36B0F
  282. qq4 :: 0h3F215DC9221C1A10
  283. qq5 :: 0hBED09C4342A26120
  284. // Coefficients for approximation to erf in [0.84375, 1.25]
  285. pa0 :: 0hBF6359B8BEF77538
  286. pa1 :: 0h3FDA8D00AD92B34D
  287. pa2 :: 0hBFD7D240FBB8C3F1
  288. pa3 :: 0h3FD45FCA805120E4
  289. pa4 :: 0hBFBC63983D3E28EC
  290. pa5 :: 0h3FA22A36599795EB
  291. pa6 :: 0hBF61BF380A96073F
  292. qa1 :: 0h3FBB3E6618EEE323
  293. qa2 :: 0h3FE14AF092EB6F33
  294. qa3 :: 0h3FB2635CD99FE9A7
  295. qa4 :: 0h3FC02660E763351F
  296. qa5 :: 0h3F8BEDC26B51DD1C
  297. qa6 :: 0h3F888B545735151D
  298. // Coefficients for approximation to erfc in [1.25, 1/0.35]
  299. ra0 :: 0hBF843412600D6435
  300. ra1 :: 0hBFE63416E4BA7360
  301. ra2 :: 0hC0251E0441B0E726
  302. ra3 :: 0hC04F300AE4CBA38D
  303. ra4 :: 0hC0644CB184282266
  304. ra5 :: 0hC067135CEBCCABB2
  305. ra6 :: 0hC054526557E4D2F2
  306. ra7 :: 0hC023A0EFC69AC25C
  307. sa1 :: 0h4033A6B9BD707687
  308. sa2 :: 0h4061350C526AE721
  309. sa3 :: 0h407B290DD58A1A71
  310. sa4 :: 0h40842B1921EC2868
  311. sa5 :: 0h407AD02157700314
  312. sa6 :: 0h405B28A3EE48AE2C
  313. sa7 :: 0h401A47EF8E484A93
  314. sa8 :: 0hBFAEEFF2EE749A62
  315. // Coefficients for approximation to erfc in [1/.35, 28]
  316. rb0 :: 0hBF84341239E86F4A
  317. rb1 :: 0hBFE993BA70C285DE
  318. rb2 :: 0hC031C209555F995A
  319. rb3 :: 0hC064145D43C5ED98
  320. rb4 :: 0hC083EC881375F228
  321. rb5 :: 0hC09004616A2E5992
  322. rb6 :: 0hC07E384E9BDC383F
  323. sb1 :: 0h403E568B261D5190
  324. sb2 :: 0h40745CAE221B9F0A
  325. sb3 :: 0h409802EB189D5118
  326. sb4 :: 0h40A8FFB7688C246A
  327. sb5 :: 0h40A3F219CEDF3BE6
  328. sb6 :: 0h407DA874E79FE763
  329. sb7 :: 0hC03670E242712D62
  330. TINY :: 1.0 / (1 << 56) // 2**-56
  331. // special cases
  332. switch {
  333. case is_nan(x):
  334. return nan_f64()
  335. case is_inf(x, 1):
  336. return 0
  337. case is_inf(x, -1):
  338. return 2
  339. }
  340. x := x
  341. sign := false
  342. if x < 0 {
  343. x = -x
  344. sign = true
  345. }
  346. if x < 0.84375 { // |x| < 0.84375
  347. temp: f64
  348. if x < TINY { // |x| < 2**-56
  349. temp = x
  350. } else {
  351. z := x * x
  352. r := pp0 + z*(pp1+z*(pp2+z*(pp3+z*pp4)))
  353. s := 1 + z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
  354. y := r / s
  355. if x < 0.25 { // |x| < 1/4
  356. temp = x + x*y
  357. } else {
  358. temp = 0.5 + (x*y + (x - 0.5))
  359. }
  360. }
  361. if sign {
  362. return 1 + temp
  363. }
  364. return 1 - temp
  365. }
  366. if x < 1.25 { // 0.84375 <= |x| < 1.25
  367. s := x - 1
  368. P := pa0 + s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))))
  369. Q := 1 + s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))))
  370. if sign {
  371. return 1 + erx + P/Q
  372. }
  373. return 1 - erx - P/Q
  374. }
  375. if x < 28 { // |x| < 28
  376. s := 1 / (x * x)
  377. R, S: f64
  378. if x < 1/0.35 { // |x| < 1 / 0.35 ~ 2.857143
  379. R = ra0 + s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7))))))
  380. S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
  381. } else { // |x| >= 1 / 0.35 ~ 2.857143
  382. if sign && x > 6 {
  383. return 2 // x < -6
  384. }
  385. R = rb0 + s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6)))))
  386. S = 1 + s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7))))))
  387. }
  388. z := transmute(f64)(0xffffffff00000000 & transmute(u64)x) // pseudo-single (20-bit) precision x
  389. r := exp(-z*z-0.5625) * exp((z-x)*(z+x)+R/S)
  390. if sign {
  391. return 2 - r/x
  392. }
  393. return r / x
  394. }
  395. if sign {
  396. return 2
  397. }
  398. return 0
  399. }