From 07e77936d535e58b0aeb4f2a11400c1050556739 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sun, 5 Dec 2021 16:16:21 -0800 Subject: Feat: added math library Used Plan9's libc as starting point. This cleans up dangling references due to loss of libc. --- src/base/rng/exponential.c | 2 +- src/base/rng/normal.c | 4 ++-- src/base/rng/poisson.c | 21 ++++++++++----------- 3 files changed, 13 insertions(+), 14 deletions(-) (limited to 'src/base/rng') diff --git a/src/base/rng/exponential.c b/src/base/rng/exponential.c index c07e007..9aa0d0c 100644 --- a/src/base/rng/exponential.c +++ b/src/base/rng/exponential.c @@ -7,5 +7,5 @@ rng·exponential(double lambda) double f; f = rng·random(); - return -log(1 - f)/lambda; + return -math·log(1 - f)/lambda; } diff --git a/src/base/rng/normal.c b/src/base/rng/normal.c index aab5731..8e3e5d4 100644 --- a/src/base/rng/normal.c +++ b/src/base/rng/normal.c @@ -53,7 +53,7 @@ erfinv(double x) z2 = ((((((b7*r+b6)*r+b5)*r+b4)*r+b3)*r+b2)*r+b1)*r + b0; return s*(x*z1) / z2; } - r = sqrt(Ln2 - log(1.0-x)); + r = math·sqrt(Ln2 - math·log(1.0-x)); if(r <= 5.0) { r -= 1.6; z1 = ((((((c7*r+c6)*r+c5)*r+c4)*r+c3)*r+c2)*r+c1)*r + c0; @@ -73,5 +73,5 @@ rng·normal(void) double f; f = rng·random(); - return sqrt(2)*erfinv(2*f-1); + return math·sqrt(2)*erfinv(2*f-1); } diff --git a/src/base/rng/poisson.c b/src/base/rng/poisson.c index 3ec15c9..776070c 100644 --- a/src/base/rng/poisson.c +++ b/src/base/rng/poisson.c @@ -28,7 +28,7 @@ log1pmx(double x, double off) return x*x*r; } - return log(1+x) - off; + return math·log(1+x) - off; } static inline @@ -49,14 +49,14 @@ procf(double mu, double s, int64 K, double *px, double *py, double *fx, double * if(K < 10) { *px = -mu; - *py = pow(mu,K) / factorial[K]; + *py = math·pow(mu,K) / factorial[K]; }else{ d = 0.08333333333333333/K; d = d - 4.8*d*d*d; V = (mu - K) / K; *px = K*log1pmx(V,mu-K) - d; - *py = 0.3989422804014327/sqrt(K); + *py = 0.3989422804014327/math·sqrt(K); } X = (K - mu + 0.5)/s; @@ -66,21 +66,20 @@ procf(double mu, double s, int64 K, double *px, double *py, double *fx, double * return c; } -static inline -uint64 +static inline uint64 bigpoisson(double mu) { int64 L,K; double G,s,d,U,E,T; double px,py,fx,fy,c; - s = sqrt(mu); + s = math·sqrt(mu); d = 6*mu*mu; - L = floor(mu - 1.1484); + L = math·floor(mu - 1.1484); stepN: G = mu + s*rng·normal(); - K = floor(G); + K = math·floor(G); if(K<0) goto stepP; stepI: @@ -99,13 +98,13 @@ stepE: E = rng·exponential(1.0); U = rng·random(); U = U + U - 1; - T = 1.8 + copysign(E,U); + T = 1.8 + math·copysign(E,U); if(T < 0.6744) goto stepE; - K = floor(mu + s*T); + K = math·floor(mu + s*T); c = procf(mu, s, K, &px, &py, &fx, &fy); stepH: - if(c*fabs(U) > (py*exp(px + E) - fy*exp(fx + E))) + if(c*math·fabs(U) > (py*math·exp(px + E) - fy*math·exp(fx + E))) goto stepE; return K; } -- cgit v1.2.1