From 7ae9ac24e27e621a4b428bb55aa35e47ca94de48 Mon Sep 17 00:00:00 2001 From: vlefevre Date: Tue, 29 Aug 2017 12:17:13 +0000 Subject: [src/urandom.c] Clarified a comment about a subtle case. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/branches/3.1@11689 280ebfd0-de03-0410-8827-d642c229c3f4 --- src/urandom.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/urandom.c b/src/urandom.c index 0283bdccb..a30f9a843 100644 --- a/src/urandom.c +++ b/src/urandom.c @@ -120,10 +120,16 @@ mpfr_urandom (mpfr_ptr rop, gmp_randstate_t rstate, mpfr_rnd_t rnd_mode) in a row, then return 0 or the smallest representable positive number. - The rounding to nearest mode is subtle: - If exp == emin - 1, the rounding bit is set, except - if cnt == DRAW_BITS in which case the rounding bit is - outside rp[0] and must be generated. */ + The rounding-to-nearest mode is subtle: We need to round to + the smallest representable positive number iff the exponent + is emin - 1. This condition can be satisfied only if the + current emin is emin - 1. In this case, if cnt != DRAW_BITS, + this in the final emin, so that the condition is satisfied. + But if cnt == DRAW_BITS, we need to draw an additional bit + to determine whether emin == emin - 1 or emin < emin - 1 + (with equal probabilities); the reason is that we return + just below instead of doing more iterations in the "while" + loop to find the final value of emin. */ __gmpfr_flags |= MPFR_FLAGS_UNDERFLOW; if (rnd_mode == MPFR_RNDU || rnd_mode == MPFR_RNDA || (rnd_mode == MPFR_RNDN && exp == __gmpfr_emin - 1 -- cgit v1.2.1