math_erf.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. erf_f16 :: proc "contextless" (x: f16) -> f16 { return f16(erf_f64(f64(x))) }
  118. erf_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(erf_f64(f64(x))) }
  119. erf_f16be :: proc "contextless" (x: f16be) -> f16be { return f16be(erf_f64(f64(x))) }
  120. erf_f32 :: proc "contextless" (x: f32) -> f32 { return f32(erf_f64(f64(x))) }
  121. erf_f32le :: proc "contextless" (x: f32le) -> f32le { return f32le(erf_f64(f64(x))) }
  122. erf_f32be :: proc "contextless" (x: f32be) -> f32be { return f32be(erf_f64(f64(x))) }
  123. erf_f64 :: proc "contextless" (x: f64) -> f64 {
  124. erx :: 0h3FEB0AC160000000
  125. // Coefficients for approximation to erf in [0, 0.84375]
  126. efx :: 0h3FC06EBA8214DB69
  127. efx8 :: 0h3FF06EBA8214DB69
  128. pp0 :: 0h3FC06EBA8214DB68
  129. pp1 :: 0hBFD4CD7D691CB913
  130. pp2 :: 0hBF9D2A51DBD7194F
  131. pp3 :: 0hBF77A291236668E4
  132. pp4 :: 0hBEF8EAD6120016AC
  133. qq1 :: 0h3FD97779CDDADC09
  134. qq2 :: 0h3FB0A54C5536CEBA
  135. qq3 :: 0h3F74D022C4D36B0F
  136. qq4 :: 0h3F215DC9221C1A10
  137. qq5 :: 0hBED09C4342A26120
  138. // Coefficients for approximation to erf in [0.84375, 1.25]
  139. pa0 :: 0hBF6359B8BEF77538
  140. pa1 :: 0h3FDA8D00AD92B34D
  141. pa2 :: 0hBFD7D240FBB8C3F1
  142. pa3 :: 0h3FD45FCA805120E4
  143. pa4 :: 0hBFBC63983D3E28EC
  144. pa5 :: 0h3FA22A36599795EB
  145. pa6 :: 0hBF61BF380A96073F
  146. qa1 :: 0h3FBB3E6618EEE323
  147. qa2 :: 0h3FE14AF092EB6F33
  148. qa3 :: 0h3FB2635CD99FE9A7
  149. qa4 :: 0h3FC02660E763351F
  150. qa5 :: 0h3F8BEDC26B51DD1C
  151. qa6 :: 0h3F888B545735151D
  152. // Coefficients for approximation to erfc in [1.25, 1/0.35]
  153. ra0 :: 0hBF843412600D6435
  154. ra1 :: 0hBFE63416E4BA7360
  155. ra2 :: 0hC0251E0441B0E726
  156. ra3 :: 0hC04F300AE4CBA38D
  157. ra4 :: 0hC0644CB184282266
  158. ra5 :: 0hC067135CEBCCABB2
  159. ra6 :: 0hC054526557E4D2F2
  160. ra7 :: 0hC023A0EFC69AC25C
  161. sa1 :: 0h4033A6B9BD707687
  162. sa2 :: 0h4061350C526AE721
  163. sa3 :: 0h407B290DD58A1A71
  164. sa4 :: 0h40842B1921EC2868
  165. sa5 :: 0h407AD02157700314
  166. sa6 :: 0h405B28A3EE48AE2C
  167. sa7 :: 0h401A47EF8E484A93
  168. sa8 :: 0hBFAEEFF2EE749A62
  169. // Coefficients for approximation to erfc in [1/.35, 28]
  170. rb0 :: 0hBF84341239E86F4A
  171. rb1 :: 0hBFE993BA70C285DE
  172. rb2 :: 0hC031C209555F995A
  173. rb3 :: 0hC064145D43C5ED98
  174. rb4 :: 0hC083EC881375F228
  175. rb5 :: 0hC09004616A2E5992
  176. rb6 :: 0hC07E384E9BDC383F
  177. sb1 :: 0h403E568B261D5190
  178. sb2 :: 0h40745CAE221B9F0A
  179. sb3 :: 0h409802EB189D5118
  180. sb4 :: 0h40A8FFB7688C246A
  181. sb5 :: 0h40A3F219CEDF3BE6
  182. sb6 :: 0h407DA874E79FE763
  183. sb7 :: 0hC03670E242712D62
  184. VERY_TINY :: 0h0080000000000000
  185. SMALL :: 1.0 / (1 << 28) // 2**-28
  186. // special cases
  187. switch {
  188. case is_nan(x):
  189. return nan_f64()
  190. case is_inf(x, 1):
  191. return 1
  192. case is_inf(x, -1):
  193. return -1
  194. }
  195. x := x
  196. sign := false
  197. if x < 0 {
  198. x = -x
  199. sign = true
  200. }
  201. if x < 0.84375 { // |x| < 0.84375
  202. temp: f64
  203. if x < SMALL { // |x| < 2**-28
  204. if x < VERY_TINY {
  205. temp = 0.125 * (8.0*x + efx8*x) // avoid underflow
  206. } else {
  207. temp = x + efx*x
  208. }
  209. } else {
  210. z := x * x
  211. r := pp0 + z*(pp1+z*(pp2+z*(pp3+z*pp4)))
  212. s := 1 + z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
  213. y := r / s
  214. temp = x + x*y
  215. }
  216. if sign {
  217. return -temp
  218. }
  219. return temp
  220. }
  221. if x < 1.25 { // 0.84375 <= |x| < 1.25
  222. s := x - 1
  223. P := pa0 + s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))))
  224. Q := 1 + s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))))
  225. if sign {
  226. return -erx - P/Q
  227. }
  228. return erx + P/Q
  229. }
  230. if x >= 6 { // inf > |x| >= 6
  231. if sign {
  232. return -1
  233. }
  234. return 1
  235. }
  236. s := 1 / (x * x)
  237. R, S: f64
  238. if x < 1/0.35 { // |x| < 1 / 0.35 ~ 2.857143
  239. R = ra0 + s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7))))))
  240. S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
  241. } else { // |x| >= 1 / 0.35 ~ 2.857143
  242. R = rb0 + s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6)))))
  243. S = 1 + s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7))))))
  244. }
  245. z := transmute(f64)(0xffffffff00000000 & transmute(u64)x) // pseudo-single (20-bit) precision x
  246. r := exp(-z*z-0.5625) * exp((z-x)*(z+x)+R/S)
  247. if sign {
  248. return r/x - 1
  249. }
  250. return 1 - r/x
  251. }
  252. erfc :: proc{
  253. erfc_f16,
  254. erfc_f16le,
  255. erfc_f16be,
  256. erfc_f32,
  257. erfc_f32le,
  258. erfc_f32be,
  259. erfc_f64,
  260. }
  261. erfc_f16 :: proc "contextless" (x: f16) -> f16 { return f16(erfc_f64(f64(x))) }
  262. erfc_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(erfc_f64(f64(x))) }
  263. erfc_f16be :: proc "contextless" (x: f16be) -> f16be { return f16be(erfc_f64(f64(x))) }
  264. erfc_f32 :: proc "contextless" (x: f32) -> f32 { return f32(erfc_f64(f64(x))) }
  265. erfc_f32le :: proc "contextless" (x: f32le) -> f32le { return f32le(erfc_f64(f64(x))) }
  266. erfc_f32be :: proc "contextless" (x: f32be) -> f32be { return f32be(erfc_f64(f64(x))) }
  267. erfc_f64 :: proc "contextless" (x: f64) -> f64 {
  268. erx :: 0h3FEB0AC160000000
  269. // Coefficients for approximation to erf in [0, 0.84375]
  270. efx :: 0h3FC06EBA8214DB69
  271. efx8 :: 0h3FF06EBA8214DB69
  272. pp0 :: 0h3FC06EBA8214DB68
  273. pp1 :: 0hBFD4CD7D691CB913
  274. pp2 :: 0hBF9D2A51DBD7194F
  275. pp3 :: 0hBF77A291236668E4
  276. pp4 :: 0hBEF8EAD6120016AC
  277. qq1 :: 0h3FD97779CDDADC09
  278. qq2 :: 0h3FB0A54C5536CEBA
  279. qq3 :: 0h3F74D022C4D36B0F
  280. qq4 :: 0h3F215DC9221C1A10
  281. qq5 :: 0hBED09C4342A26120
  282. // Coefficients for approximation to erf in [0.84375, 1.25]
  283. pa0 :: 0hBF6359B8BEF77538
  284. pa1 :: 0h3FDA8D00AD92B34D
  285. pa2 :: 0hBFD7D240FBB8C3F1
  286. pa3 :: 0h3FD45FCA805120E4
  287. pa4 :: 0hBFBC63983D3E28EC
  288. pa5 :: 0h3FA22A36599795EB
  289. pa6 :: 0hBF61BF380A96073F
  290. qa1 :: 0h3FBB3E6618EEE323
  291. qa2 :: 0h3FE14AF092EB6F33
  292. qa3 :: 0h3FB2635CD99FE9A7
  293. qa4 :: 0h3FC02660E763351F
  294. qa5 :: 0h3F8BEDC26B51DD1C
  295. qa6 :: 0h3F888B545735151D
  296. // Coefficients for approximation to erfc in [1.25, 1/0.35]
  297. ra0 :: 0hBF843412600D6435
  298. ra1 :: 0hBFE63416E4BA7360
  299. ra2 :: 0hC0251E0441B0E726
  300. ra3 :: 0hC04F300AE4CBA38D
  301. ra4 :: 0hC0644CB184282266
  302. ra5 :: 0hC067135CEBCCABB2
  303. ra6 :: 0hC054526557E4D2F2
  304. ra7 :: 0hC023A0EFC69AC25C
  305. sa1 :: 0h4033A6B9BD707687
  306. sa2 :: 0h4061350C526AE721
  307. sa3 :: 0h407B290DD58A1A71
  308. sa4 :: 0h40842B1921EC2868
  309. sa5 :: 0h407AD02157700314
  310. sa6 :: 0h405B28A3EE48AE2C
  311. sa7 :: 0h401A47EF8E484A93
  312. sa8 :: 0hBFAEEFF2EE749A62
  313. // Coefficients for approximation to erfc in [1/.35, 28]
  314. rb0 :: 0hBF84341239E86F4A
  315. rb1 :: 0hBFE993BA70C285DE
  316. rb2 :: 0hC031C209555F995A
  317. rb3 :: 0hC064145D43C5ED98
  318. rb4 :: 0hC083EC881375F228
  319. rb5 :: 0hC09004616A2E5992
  320. rb6 :: 0hC07E384E9BDC383F
  321. sb1 :: 0h403E568B261D5190
  322. sb2 :: 0h40745CAE221B9F0A
  323. sb3 :: 0h409802EB189D5118
  324. sb4 :: 0h40A8FFB7688C246A
  325. sb5 :: 0h40A3F219CEDF3BE6
  326. sb6 :: 0h407DA874E79FE763
  327. sb7 :: 0hC03670E242712D62
  328. TINY :: 1.0 / (1 << 56) // 2**-56
  329. // special cases
  330. switch {
  331. case is_nan(x):
  332. return nan_f64()
  333. case is_inf(x, 1):
  334. return 0
  335. case is_inf(x, -1):
  336. return 2
  337. }
  338. x := x
  339. sign := false
  340. if x < 0 {
  341. x = -x
  342. sign = true
  343. }
  344. if x < 0.84375 { // |x| < 0.84375
  345. temp: f64
  346. if x < TINY { // |x| < 2**-56
  347. temp = x
  348. } else {
  349. z := x * x
  350. r := pp0 + z*(pp1+z*(pp2+z*(pp3+z*pp4)))
  351. s := 1 + z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
  352. y := r / s
  353. if x < 0.25 { // |x| < 1/4
  354. temp = x + x*y
  355. } else {
  356. temp = 0.5 + (x*y + (x - 0.5))
  357. }
  358. }
  359. if sign {
  360. return 1 + temp
  361. }
  362. return 1 - temp
  363. }
  364. if x < 1.25 { // 0.84375 <= |x| < 1.25
  365. s := x - 1
  366. P := pa0 + s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))))
  367. Q := 1 + s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))))
  368. if sign {
  369. return 1 + erx + P/Q
  370. }
  371. return 1 - erx - P/Q
  372. }
  373. if x < 28 { // |x| < 28
  374. s := 1 / (x * x)
  375. R, S: f64
  376. if x < 1/0.35 { // |x| < 1 / 0.35 ~ 2.857143
  377. R = ra0 + s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7))))))
  378. S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
  379. } else { // |x| >= 1 / 0.35 ~ 2.857143
  380. if sign && x > 6 {
  381. return 2 // x < -6
  382. }
  383. R = rb0 + s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6)))))
  384. S = 1 + s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7))))))
  385. }
  386. z := transmute(f64)(0xffffffff00000000 & transmute(u64)x) // pseudo-single (20-bit) precision x
  387. r := exp(-z*z-0.5625) * exp((z-x)*(z+x)+R/S)
  388. if sign {
  389. return 2 - r/x
  390. }
  391. return r / x
  392. }
  393. if sign {
  394. return 2
  395. }
  396. return 0
  397. }