From 28e59ff06458a72d2cc276fc0d072547da5cca5e Mon Sep 17 00:00:00 2001 From: kuepper Date: Wed, 22 Aug 2007 07:30:47 +0000 Subject: [PATCH] eoRNG.h (eoRng::random): Add comment on truncation as standard way of floating point to integer conversion. --- eo/src/utils/ChangeLog | 5 +++++ eo/src/utils/eoRNG.h | 10 ++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/eo/src/utils/ChangeLog b/eo/src/utils/ChangeLog index 503fc55e..a661c0cf 100644 --- a/eo/src/utils/ChangeLog +++ b/eo/src/utils/ChangeLog @@ -1,3 +1,8 @@ +2007-08-22 Jochen Küpper + + * eoRNG.h (eoRng::random): Add comment on truncation as standard way of + floating point to integer conversion. + 2006-12-04 Jochen Küpper * eoRNG.h (eoRng::normal(stdev)): Revert erroneous change. diff --git a/eo/src/utils/eoRNG.h b/eo/src/utils/eoRNG.h index d2857a83..d2a44a0e 100644 --- a/eo/src/utils/eoRNG.h +++ b/eo/src/utils/eoRNG.h @@ -178,10 +178,12 @@ public : */ uint32_t random(uint32_t m) { - // Make sure we always round towards zero, in order to get the - // half-open interval we want (as documented). The floor function - // does exactly this for the (always positive) values occuring here. - return uint32_t(floor(uniform() * double(m))); + // C++ Standard (4.9 Floatingintegral conversions [conv.fpint]) + // defines floating point to integer conversion as truncation + // ("rounding towards zero"): "An rvalue of a floating point type + // can be converted to an rvalue of an integer type. The conversion + // truncates; that is, the fractional part is discarded" + return uint32_t(uniform() * double(m)); } /** Biased coin toss