squiggle.c

Self-contained Monte Carlo estimation in C99
Log | Files | Refs | README

commit 32033b5c86201233f337e372698403e2d4dca399
parent 9e1d4ee6d4597a1f47b612168e7002b78cb3400a
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Sun, 23 Jul 2023 12:53:46 +0200

stop using pow when possible

https://stackoverflow.com/questions/2940367/what-is-more-efficient-using-pow-to-square-or-just-multiply-it-with-itself

Diffstat:
Mexamples/06_gamma_beta/example | 0
Msquiggle.c | 9+++++----
2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/examples/06_gamma_beta/example b/examples/06_gamma_beta/example Binary files differ. diff --git a/squiggle.c b/squiggle.c @@ -105,9 +105,9 @@ float sample_gamma(float alpha, uint64_t* seed) v = 1.0 + c * x; } while (v <= 0.0); - v = pow(v, 3); + v = v * v * v; u = sample_unit_uniform(seed); - if (u < 1.0 - 0.0331 * pow(x, 4)) { // Condition 1 + if (u < 1.0 - 0.0331 * (x * x * x * x)) { // Condition 1 // the 0.0331 doesn't inspire much confidence // however, this isn't the whole story // by knowing that Condition 1 implies condition 2 @@ -115,7 +115,7 @@ float sample_gamma(float alpha, uint64_t* seed) // i.e., of not using the logarithms return d * v; } - if (log(u) < 0.5 * pow(x, 2) + d * (1.0 - v + log(v))) { // Condition 2 + if (log(u) < 0.5 * (x * x) + d * (1.0 - v + log(v))) { // Condition 2 return d * v; } } @@ -161,7 +161,8 @@ float array_std(float* array, int length) float mean = array_mean(array, length); float std = 0.0; for (int i = 0; i < length; i++) { - std += pow(array[i] - mean, 2.0); + std += (array[i] - mean); + std *= std; } std = sqrt(std / length); return std;