rework returning result to define and return at the same time

This commit is contained in:
NunoSempere 2023-07-16 17:00:26 +02:00
parent 84f94e2bea
commit 78e1838569

View File

@ -66,7 +66,6 @@ struct box inverse_cdf(float cdf(float), float p)
// or an error // or an error
// if EXIT_ON_ERROR is set to 1, it exits instead of providing 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 low = -1.0;
float high = 1.0; float high = 1.0;
@ -97,8 +96,8 @@ struct box inverse_cdf(float cdf(float), float p)
float mid = (high + low) / 2; float mid = (high + low) / 2;
int mid_not_new = (mid == low) || (mid == high); int mid_not_new = (mid == low) || (mid == high);
// float width = high - low; // float width = high - low;
if (mid_not_new) {
// if ((width < 1e-8) || mid_not_new){ // if ((width < 1e-8) || mid_not_new){
if (mid_not_new) {
convergence_condition = 1; convergence_condition = 1;
} else { } else {
float mid_sign = cdf(mid) - p; float mid_sign = cdf(mid) - p;
@ -114,13 +113,12 @@ struct box inverse_cdf(float cdf(float), float p)
} }
if (convergence_condition) { if (convergence_condition) {
result.content = low; struct box result = {.empty = 0, .content = low};
result.empty = 0; return result;
} else { } else {
PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); 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 // or an error
// if EXIT_ON_ERROR is set to 1, it exits instead of providing 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 low = -1.0;
float high = 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) { if (convergence_condition) {
result.content = low; struct box result = {.empty = 0, .content = low};
result.empty = 0;
return result; return result;
} else { } else {
PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); 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 // Sampler based on inverse cdf
struct box sampler(float cdf(float), uint32_t* seed) struct box sampler(float cdf(float), uint32_t* seed)
{ {
struct box result;
float p = rand_0_to_1(seed); float p = rand_0_to_1(seed);
result = inverse_cdf(cdf, p); struct box result = inverse_cdf(cdf, p);
return result; return result;
} }
@ -282,8 +277,6 @@ struct box incbeta(float a, float b, float x)
* misrepresented as being the original software. * misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution. * 3. This notice may not be removed or altered from any source distribution.
*/ */
struct box result;
if (x < 0.0 || x > 1.0) { if (x < 0.0 || x > 1.0) {
PROCESS_ERROR("x out of bounds [0, 1], in function incbeta"); 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) { if (symmetric_incbeta.empty) {
return symmetric_incbeta; // propagate error return symmetric_incbeta; // propagate error
} else { } else {
result.empty = 0; struct box result = {
result.content = 1 - symmetric_incbeta.content; .empty = 0,
.content = 1 - symmetric_incbeta.content
};
return result; return result;
} }
} }
@ -335,8 +330,10 @@ struct box incbeta(float a, float b, float x)
/*Check for stop.*/ /*Check for stop.*/
if (fabs(1.0 - cd) < STOP) { if (fabs(1.0 - cd) < STOP) {
result.content = front * (f - 1.0); struct box result = {
result.empty = 0; .empty = 0,
.content = front * (f - 1.0)
};
return result; return result;
} }
} }