xref: /llvm-project/libcxx/test/std/numerics/rand/rand.util/rand.util.canonical/generate_canonical.pass.cpp (revision b43923da5bb5390653b40143c2ef72ae90a9c1b7)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <random>
10 
11 // template<class RealType, size_t bits, class URNG>
12 //     RealType generate_canonical(URNG& g);
13 
14 #include <random>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 #include "truncate_fp.h"
19 
20 int main(int, char**)
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()) / (static_cast<F>(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()) / (static_cast<F>(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()) / (static_cast<F>(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()) / (static_cast<F>(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()) / (static_cast<F>(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()) / (static_cast<F>(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()) / (static_cast<F>(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()) * (static_cast<F>(E::max() - E::min()) + F(1))) /
80             ((static_cast<F>(E::max() - E::min()) + F(1)) * (static_cast<F>(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()) * (static_cast<F>(E::max() - E::min()) + F(1))) /
90             ((static_cast<F>(E::max() - E::min()) + F(1)) * (static_cast<F>(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()) * (static_cast<F>(E::max() - E::min()) + F(1))) /
100             ((static_cast<F>(E::max() - E::min()) + F(1)) * (static_cast<F>(E::max() - E::min()) + F(1)))));
101     }
102 
103   return 0;
104 }
105