1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <random>
11
12 // template<class RealType, size_t bits, class URNG>
13 // RealType generate_canonical(URNG& g);
14
15 #include <random>
16 #include <cassert>
17
18 #include "truncate_fp.h"
19
main()20 int main()
21 {
22 {
23 typedef std::minstd_rand0 E;
24 typedef float F;
25 E r;
26 F f = std::generate_canonical<F, 0>(r);
27 assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
28 }
29 {
30 typedef std::minstd_rand0 E;
31 typedef float F;
32 E r;
33 F f = std::generate_canonical<F, 1>(r);
34 assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
35 }
36 {
37 typedef std::minstd_rand0 E;
38 typedef float F;
39 E r;
40 F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r);
41 assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
42 }
43 {
44 typedef std::minstd_rand0 E;
45 typedef float F;
46 E r;
47 F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r);
48 assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
49 }
50 {
51 typedef std::minstd_rand0 E;
52 typedef float F;
53 E r;
54 F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r);
55 assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
56 }
57
58 {
59 typedef std::minstd_rand0 E;
60 typedef double F;
61 E r;
62 F f = std::generate_canonical<F, 0>(r);
63 assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
64 }
65 {
66 typedef std::minstd_rand0 E;
67 typedef double F;
68 E r;
69 F f = std::generate_canonical<F, 1>(r);
70 assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
71 }
72 {
73 typedef std::minstd_rand0 E;
74 typedef double F;
75 E r;
76 F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r);
77 assert(f == truncate_fp(
78 (16807 - E::min() +
79 (282475249 - E::min()) * (E::max() - E::min() + F(1))) /
80 ((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1)))));
81 }
82 {
83 typedef std::minstd_rand0 E;
84 typedef double F;
85 E r;
86 F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r);
87 assert(f == truncate_fp(
88 (16807 - E::min() +
89 (282475249 - E::min()) * (E::max() - E::min() + F(1))) /
90 ((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1)))));
91 }
92 {
93 typedef std::minstd_rand0 E;
94 typedef double F;
95 E r;
96 F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r);
97 assert(f == truncate_fp(
98 (16807 - E::min() +
99 (282475249 - E::min()) * (E::max() - E::min() + F(1))) /
100 ((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1)))));
101 }
102 }
103