Boost logo

Boost Users :

Subject: Re: [Boost-users] [random] infinite loop in uniform_real_distribution
From: Brian Budge (brian.budge_at_[hidden])
Date: 2013-11-25 16:03:28


On Mon, Nov 25, 2013 at 11:16 AM, Jérémy Coulon <jeremy.coulon_at_[hidden]> wrote:
> Hello,
>
> I'm using Boost 1.54.
> The following code hangs in infinite loop when generating a single floating
> point number :
>
> #include <boost/random.hpp>
> #include <iostream>
> #include <limits>
>
> int main()
> {
> boost::random::mt19937 rng;
> boost::random::uniform_real_distribution<float> distrib(
> -std::numeric_limits<float>::max(),
> std::numeric_limits<float>::max());
>
> std::cout << distrib(rng) << std::endl;
> return 0;
> }
>
> I can reproduce the bug with both
> - Linux GCC 4.8 x86_64 and
> - Windows VisualStudio 2012 32bits
>
> It looks like this uniform distribution doesn't work if the range [min,max]
> is large.
> From uniform_real_distribution.hpp:61
> T result = numerator / divisor * (max_value - min_value) + min_value;
>
> Here the operation (max_value - min_value) produces an overflow.
>
> Regards,
> Jeremy
>

It seems to me that it's not really just "large" that is the issue,
but only if you have a range larger than
std::numeric_limits<float>::max(). Ideally, you'd not really want to
be messing with too very many numbers that are within many orders of
magnitude of the max float value, as your precision will be awful.
Are you just edge-case testing, or do you have a real reason to
generate uniform distributions over this range?

  Brian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net