star-sp

Random number generators and distributions
git clone git://git.meso-star.fr/star-sp.git
Log | Files | Refs | README | LICENSE

commit 84920ea85e0878c466e5ec0b9f142ec2880859c4
parent 950190d3074397cf05610318d08551810fa584e9
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  4 May 2016 17:34:41 +0200

Fix an issue in the ssp_rng_canonical_float routine

The returned number could be equal to 1.

Diffstat:
Msrc/ssp_rng.c | 13+++++++++++++
1 file changed, 13 insertions(+), 0 deletions(-)

diff --git a/src/ssp_rng.c b/src/ssp_rng.c @@ -608,8 +608,21 @@ ssp_rng_canonical_float(struct ssp_rng* rng) { if(!rng) FATAL("The Random Number Generator is NULL\n"); rng_cxx rng_cxx(*rng); + /* The C++ standard library does not ensure that the generated single + * precision floating point number is effectively canonical, i.e. it may be + * equal to 1. Use a hand made workaround to handle this bug. */ +#if 0 return RAN_NAMESPACE::generate_canonical <float, std::numeric_limits<float>::digits>(rng_cxx); +#else + const double rd = RAN_NAMESPACE::generate_canonical + <double, std::numeric_limits<float>::digits>(rng_cxx); + float rf = (float)rd; + if(rf > rd) { + rf = std::nextafter(rf, -std::numeric_limits<float>::infinity()); + } + return rf; +#endif } uint64_t