Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2020-10-03 10:17:37


AMDG

On 10/1/20 5:45 PM, John W via Boost-users wrote:
> I have a fixed-point number class (based on libfixmath) which in code
> largely behaves like a float due to various operator overloading.
>
> Now, I am trying to generate random numbers with them.
> Uniform random is easy enough — just reinterpret some random bits as
> my fixedpoint type.
>
> However, I'd like to have other distributions, like
> boost::random::normal_distribution.
>
> But from what I can tell, Boost.Random might have some baked-in
> assumptions about only working on float/double/long double.
>
> Is that correct?
>
> Or are there ways I can more-or-less transparently use
> boost::random::xxx distributions with my custom type?
>
> Basically, I want to know how much the <RealType> template argument
> can be abused.
>

Boost.Random works with custom floating point types, but I
don't recommend using it with fixed point. The algorithms
will likely compile as long as you provide conversions
from built in numeric types and overloads of cmath functions,
but they are written with floating point in mind and may
lose precision when applied to fixed point.

> In std::random, there is some clear language[1], saying:
> "The result type generated by the generator. The effect is undefined
> if this is not one of float, double, or long double."
>
> and I believe boost::random aims to be in agreement with std::random,
> generally. But I could not find such clear language in the
> Boost.Random docs.
>

Boost.Random is generally more permissive than the standard.

> Thanks
> -John
>
> [1] https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution
>

In Christ,
Steven Watanabe


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