ExRandom
3.0
|
The machinery to handle u-rands, arbitrary precision random deviates. More...
#include <exrandom/u_rand.hpp>
Public Member Functions | |
u_rand (digit_gen &D) | |
u_rand & | init () |
void | swap (u_rand &t) |
int | sign () const |
void | negate () |
unsigned | integer () const |
void | set_integer (unsigned n) |
size_t | ndigits () const |
template<typename Generator > | |
uint_t | digit (Generator &g, size_t k) |
uint_t | rawdigit (size_t k) const |
uint_t & | rawdigit (size_t k) |
template<typename Generator > | |
bool | less_than (Generator &g, u_rand &t) |
template<typename Generator > | |
bool | less_than_half (Generator &g) |
template<typename Generator > | |
bool | truncatep (Generator &g, size_t k) |
template<typename Generator > | |
int | compare (Generator &g, long long u1, long long u2, long long v) |
template<typename Generator > | |
bool | less_than (Generator &g, long long u0, long long c, long long v, i_rand< digit_gen > &h) |
std::pair< long long, long long > | rawrational (size_t k) const |
std::pair< long long, long long > | rational () const |
template<typename Generator > | |
std::pair< long long, long long > | rational (Generator &g, size_t k) |
template<typename RealType > | |
std::pair< RealType, RealType > | range () const |
template<typename RealType > | |
RealType | midpoint () |
template<typename RealType , typename Generator > | |
RealType | midpoint (Generator &g, size_t k) |
template<typename RealType , typename Generator > | |
RealType | value (Generator &g, std::float_round_style rnd, int &flag) |
template<typename RealType , typename Generator > | |
RealType | value (Generator &g, int &flag) |
template<typename RealType , typename Generator > | |
RealType | value (Generator &g) |
std::string | print () const |
template<typename Generator > | |
std::string | print_fixed (Generator &g, size_t k) |
Static Public Attributes | |
static const uint_t | base |
Friends | |
std::ostream & | operator<< (std::ostream &os, const u_rand &x) |
The machinery to handle u-rands, arbitrary precision random deviates.
A u_rand is represented in base b as s (n + ∑k=0K−1 dk b−k−1 + b−KU) where s = ±1, n and K are non-negative integers, and dk ∈ [0,b).
digit_gen | the type of digit generator. |
Definition at line 105 of file u_rand.hpp.
|
inline |
The constructor.
D | A reference to the digit generator to be used. |
The initial state represents a random deviate uniform in (0,1)
Definition at line 114 of file u_rand.hpp.
|
inline |
Reset to the initial state.
The returned result allows the idiom x.init().less_than(g, y) to be used.
Definition at line 123 of file u_rand.hpp.
|
inline |
Swap two u_rands.
t | the u_rand to swap with. |
The digit generator is not part of the swap.
Definition at line 134 of file u_rand.hpp.
|
inline |
Definition at line 142 of file u_rand.hpp.
|
inline |
Change the sign of the u_rand.
Definition at line 146 of file u_rand.hpp.
|
inline |
Definition at line 150 of file u_rand.hpp.
|
inline |
Set the integer part of the u_rand.
n | the new value of the (unsigned) integer part. |
Definition at line 155 of file u_rand.hpp.
|
inline |
Definition at line 159 of file u_rand.hpp.
|
inline |
The k'th digit generating it if necessary.
Generator | the type of g. |
g | the random generator engine used to generate the digit. |
k | which digit of the fraction to return (counting from 0). |
Definition at line 169 of file u_rand.hpp.
|
inline |
The k'th digit (which must already have been generated).
k | which digit of the fraction to return (counting from 0). |
Definition at line 179 of file u_rand.hpp.
|
inline |
A reference to the k'th digit (which must already be generated).
k | which digit of the fraction to return (counting from 0). |
Definition at line 186 of file u_rand.hpp.
|
inline |
Test *this < t.
Generator | the type of g. |
g | the random generator engine used to generate the digit. |
t | the u_rand to compare with. |
Definition at line 196 of file u_rand.hpp.
|
inline |
Test *this < 1/2.
Generator | the type of g. |
g | the random generator engine used to generate the digit. |
Definition at line 213 of file u_rand.hpp.
|
inline |
Determine how the result should be rounded.
Generator | the type of g. |
g | the random generator engine used to generate the digit. |
k | which rounding digit. |
Look at the kth (and subsequent) digits to determine whether the (k-1)th digit should retained (i.e., the result rounded towards zero) or incremented (i.e., the result should rounded away from zero). If k = 0, then the (k-1)th digit affects the integer part of the result. If base is even only one digit is tested.
Definition at line 233 of file u_rand.hpp.
|
inline |
Compare *this with u1/v and u2/v.
Generator | the type of g. |
g | the random generator engine. |
u1 | the numerator of the first fraction. |
u2 | the numerator of the second fraction. |
v | the denominator of the fractions. |
This function requires v > 0, u2 > u1.
Definition at line 254 of file u_rand.hpp.
|
inline |
Compare *this with a i_rand.
Generator | the type of g. |
g | the random generator engine. |
u0 | the offset in the numerator. |
c | the multiplier for the i_rand. |
v | the denominator of the fraction. |
h | the i_rand. |
This function requires v > 0, c > 0.
Definition at line 281 of file u_rand.hpp.
|
inline |
The lower end of the range as a rational.
k | the number of digits to include from the fractional part of the number. |
The fraction is not in lowest terms. Thus the upper end can be found by adding 1 to the numerator. Overflow is possible in this routine and no attempt is made to detect this.
This function requires that base ≤ 28 (in an attempt to avoid overflow).
Definition at line 306 of file u_rand.hpp.
|
inline |
The lower end of the range as a rational.
The fraction is not in lowest terms. Thus the upper end can be found by adding 1 to the numerator. Overflow is possible in this routine and no attempt is made to detect this.
This function requires that base ≤ 28 (in an attempt to avoid overflow).
Definition at line 329 of file u_rand.hpp.
|
inline |
The lower end of the range as a rational.
Generator | the type of g. |
g | the random generator engine. |
k | the number of digits to include from the fractional part of the number. |
The fraction is not in lowest terms. Thus the upper end can be found by adding 1 to the numerator. Overflow is possible in this routine and no attempt is made to detect this.
This function requires that base ≤ 28 (in an attempt to avoid overflow).
Definition at line 350 of file u_rand.hpp.
|
inline |
The range.
RealType | the floating point type for the ends of the range. |
The calculations of the end points are done using ordinary floating point arithmetic. The results will be reasonably close to the true end points.
Definition at line 364 of file u_rand.hpp.
|
inline |
The midpoint of the range.
RealType | the floating point type for the result. |
The calculation of the midpoint is done using ordinary floating point arithmetic. The result will be reasonably close to the true midpoint.
Definition at line 387 of file u_rand.hpp.
|
inline |
The midpoint of the range.
RealType | the floating point type for the result. |
Generator | the type of g. |
g | the random generator engine. |
k | make sure there are at least k digits in the fraction. |
The calculation of the midpoint is done using ordinary floating point arithmetic. The result will be reasonably close to the true midpoint.
Definition at line 404 of file u_rand.hpp.
|
inline |
Return the value of the u_rand with specified rounding as a floating point number of type RealType and, if necessary, creating additional digits of the number. Also return inexact flag to indicate whether the rounded result is greater or less than the true result.
RealType | the floating point type to convert to. |
Generator | the type of g. |
g | the random generator engine. | |
rnd | the rounding mode; by default this is the rounding mode for RealType. | |
[out] | flag | the inexact flag, +1 (resp. -1) if rounded result is greater (resp. less) than the true result. |
This function is only implemented if the base is a power of two for conventional floating point types (which have radix = 2). If the floating point radix is not two (e.g., radix = 10), then the base needs to match the radix and be even. The meaning for the inexact flag is the same as in the MPFR library.
Possible values for rnd are
The conversions have been tested for
RealType needs to support the following operations:
Special treatment is included for
Definition at line 454 of file u_rand.hpp.
|
inline |
Return the value of the u_rand rounded to nearest floating point number of type RealType and, if necessary, creating additional digits of the number. Also return inexact flag to indicate whether the rounded result is greater or less than the true result.
RealType | the floating point type to convert to. |
Generator | the type of g. |
g | the random generator engine. | |
[out] | flag | the inexact flag, +1 (resp. -1) if rounded result is greater (resp. less) than the true result. |
This function is only implemented if the base is a power of two for conventional floating point types (which have radix = 2). If the floating point radix is not two (e.g., radix = 10), then the base needs to match the radix and be even. The meaning for the inexact flag is the same as in the MPFR library.
Definition at line 575 of file u_rand.hpp.
|
inline |
Return the value of the u_rand rounded to nearest floating point number of type RealType and, if necessary, creating additional digits of the number.
RealType | the floating point type to convert to. |
Generator | the type of g. |
g | the random generator engine. |
This function is only implemented if the base is a power of two for conventional floating point types (which have radix = 2). If the floating point radix is not two (e.g., radix = 10), then the base needs to match the radix and be even.
Definition at line 594 of file u_rand.hpp.
|
inline |
Print a u_rand in u-rand format.
This prints the number to the existing precision followed by an ellipsis "…" to indicate the digits which haven't been generated yet. This function is only implemented if the base is less than 16 or a power of 16. The representation uses the base of the u_rand or hexadecimal if base > 16.
Definition at line 610 of file u_rand.hpp.
|
inline |
Print a u_rand in rounded fixed point format.
Generator | the type of g. |
g | the random generator engine. |
k |
This prints the number with k digits in the fraction and (+) (resp. (−)) to indicate that the true result is further from (resp. closer to) zero. This function is only implemented if the base is less than 16 or a power of 16. The representation uses the base of the u_rand or hexadecimal if base > 16.
Definition at line 628 of file u_rand.hpp.
|
friend |
Print a u_rand in u-rand format.
os | an output stream. |
x | a u_rand. |
This prints the number to the existing precision followed by an ellipsis "..." to indicate the digits which haven't been generated yet. This function is only implemented if the base is less than 16 or a power of
Definition at line 658 of file u_rand.hpp.
|
static |
The base of the digit generator.
Definition at line 664 of file u_rand.hpp.