eoRNG.h (eoRng::random): Add comment on truncation as standard way of
floating point to integer conversion.
This commit is contained in:
parent
4c89acad72
commit
28e59ff064
2 changed files with 11 additions and 4 deletions
|
|
@ -1,3 +1,8 @@
|
|||
2007-08-22 Jochen Küpper <jochen@fhi-berlin.mpg.de>
|
||||
|
||||
* eoRNG.h (eoRng::random): Add comment on truncation as standard way of
|
||||
floating point to integer conversion.
|
||||
|
||||
2006-12-04 Jochen Küpper <jochen@fhi-berlin.mpg.de>
|
||||
|
||||
* eoRNG.h (eoRng::normal(stdev)): Revert erroneous change.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Reference in a new issue