From 78e1838569da7d4ded5c680475b85644e9317ed8 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 16 Jul 2023 17:00:26 +0200 Subject: [PATCH] rework returning result to define and return at the same time --- scratchpad/scratchpad.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/scratchpad/scratchpad.c b/scratchpad/scratchpad.c index 5c9372b..085e767 100644 --- a/scratchpad/scratchpad.c +++ b/scratchpad/scratchpad.c @@ -66,7 +66,6 @@ struct box inverse_cdf(float cdf(float), float p) // or an error // if EXIT_ON_ERROR is set to 1, it exits instead of providing an error - struct box result; float low = -1.0; float high = 1.0; @@ -97,8 +96,8 @@ struct box inverse_cdf(float cdf(float), float p) float mid = (high + low) / 2; int mid_not_new = (mid == low) || (mid == high); // float width = high - low; + // if ((width < 1e-8) || mid_not_new){ if (mid_not_new) { - // if ((width < 1e-8) || mid_not_new){ convergence_condition = 1; } else { float mid_sign = cdf(mid) - p; @@ -114,13 +113,12 @@ struct box inverse_cdf(float cdf(float), float p) } if (convergence_condition) { - result.content = low; - result.empty = 0; + struct box result = {.empty = 0, .content = low}; + return result; } else { PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); } - return result; } } @@ -133,7 +131,6 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p) // or an error // if EXIT_ON_ERROR is set to 1, it exits instead of providing an error - struct box result; float low = -1.0; float high = 1.0; @@ -194,8 +191,7 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p) } if (convergence_condition) { - result.content = low; - result.empty = 0; + struct box result = {.empty = 0, .content = low}; return result; } else { PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); @@ -228,9 +224,8 @@ float rand_0_to_1(uint32_t* seed) // Sampler based on inverse cdf struct box sampler(float cdf(float), uint32_t* seed) { - struct box result; float p = rand_0_to_1(seed); - result = inverse_cdf(cdf, p); + struct box result = inverse_cdf(cdf, p); return result; } @@ -282,8 +277,6 @@ struct box incbeta(float a, float b, float x) * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. */ - struct box result; - if (x < 0.0 || x > 1.0) { PROCESS_ERROR("x out of bounds [0, 1], in function incbeta"); } @@ -294,8 +287,10 @@ struct box incbeta(float a, float b, float x) if (symmetric_incbeta.empty) { return symmetric_incbeta; // propagate error } else { - result.empty = 0; - result.content = 1 - symmetric_incbeta.content; + struct box result = { + .empty = 0, + .content = 1 - symmetric_incbeta.content + }; return result; } } @@ -335,8 +330,10 @@ struct box incbeta(float a, float b, float x) /*Check for stop.*/ if (fabs(1.0 - cd) < STOP) { - result.content = front * (f - 1.0); - result.empty = 0; + struct box result = { + .empty = 0, + .content = front * (f - 1.0) + }; return result; } }