Skip to content

Commit

Permalink
binomial_distribution: mitigate undocumented behaviour
Browse files Browse the repository at this point in the history
In param_type::_M_initialize(): Change integer literal to corresponding floating-point literal in multiplication by _M_t to avoid unsigned overflow and consequent chain of unpredicted behaviour
  • Loading branch information
Pignotto authored Jan 21, 2023
1 parent 046dc9d commit 0d79c40
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion libstdc++-v3/include/bits/random.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// sqrt(pi / 2)
const double __spi_2 = 1.2533141373155002512078826424055226L;
_M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
_M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
_M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4. * _M_t * __1p));
_M_c = 2 * _M_d1 / __np;
_M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
const double __a12 = _M_a1 + _M_s2 * __spi_2;
Expand Down

1 comment on commit 0d79c40

@Pignotto
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In particular, for values of _M_t that overflow to 0 by the 4* multiplication, the operator()(_UniformRandomNumberGenerator& __urng, const param_type& __param) hangs in an infinite loop as noted and described on https://stackoverflow.com/q/75179395/15114624

Please sign in to comment.