Skip to content

Commit 7f9339b

Browse files
zuhu2195jorenham
andauthored
MAINT: Eliminate ambiguous order of evaluation in the ratios of random variates. (#29598)
In C, in an expression such as f1()/f2(), the order of evaluation of f1() and f2() is unspecified. When such a ratio was used in the C functions random_f and random_standard_cauchy, it meant the streams of random variates for the f and standard_cauchy distributions depended on how the compiler chose to order the evaluation. This could result in different streams of variates when numpy is compiled with different compilers. By evaluating the numerator and denominator in separate statements, the ambiguity is eliminated. Co-authored-by: Joren Hammudoglu <[email protected]>
1 parent 37c9cb6 commit 7f9339b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

numpy/random/src/distributions/distributions.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,15 @@ double random_chisquare(bitgen_t *bitgen_state, double df) {
468468
}
469469

470470
double random_f(bitgen_t *bitgen_state, double dfnum, double dfden) {
471-
return ((random_chisquare(bitgen_state, dfnum) * dfden) /
472-
(random_chisquare(bitgen_state, dfden) * dfnum));
471+
double subexpr1 = random_chisquare(bitgen_state, dfnum) * dfden;
472+
double subexpr2 = random_chisquare(bitgen_state, dfden) * dfnum;
473+
return subexpr1 / subexpr2;
473474
}
474475

475476
double random_standard_cauchy(bitgen_t *bitgen_state) {
476-
return random_standard_normal(bitgen_state) / random_standard_normal(bitgen_state);
477+
double subexpr1 = random_standard_normal(bitgen_state);
478+
double subexpr2 = random_standard_normal(bitgen_state);
479+
return subexpr1 / subexpr2;
477480
}
478481

479482
double random_pareto(bitgen_t *bitgen_state, double a) {

0 commit comments

Comments
 (0)