107 lines · cpp
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-exception6//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 16#include <cassert>17#include <limits>18 19#include "test_macros.h"20#include "truncate_fp.h"21 22int main(int, char**)23{24 {25 typedef std::minstd_rand0 E;26 typedef float F;27 E r;28 F f = std::generate_canonical<F, 0>(r);29 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));30 }31 {32 typedef std::minstd_rand0 E;33 typedef float F;34 E r;35 F f = std::generate_canonical<F, 1>(r);36 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));37 }38 {39 typedef std::minstd_rand0 E;40 typedef float F;41 E r;42 F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r);43 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));44 }45 {46 typedef std::minstd_rand0 E;47 typedef float F;48 E r;49 F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r);50 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));51 }52 {53 typedef std::minstd_rand0 E;54 typedef float F;55 E r;56 F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r);57 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));58 }59 60 {61 typedef std::minstd_rand0 E;62 typedef double F;63 E r;64 F f = std::generate_canonical<F, 0>(r);65 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));66 }67 {68 typedef std::minstd_rand0 E;69 typedef double F;70 E r;71 F f = std::generate_canonical<F, 1>(r);72 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));73 }74 {75 typedef std::minstd_rand0 E;76 typedef double F;77 E r;78 F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r);79 assert(f == truncate_fp(80 (16807 - E::min() +81 (282475249 - E::min()) * (static_cast<F>(E::max() - E::min()) + F(1))) /82 ((static_cast<F>(E::max() - E::min()) + F(1)) * (static_cast<F>(E::max() - E::min()) + F(1)))));83 }84 {85 typedef std::minstd_rand0 E;86 typedef double F;87 E r;88 F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r);89 assert(f == truncate_fp(90 (16807 - E::min() +91 (282475249 - E::min()) * (static_cast<F>(E::max() - E::min()) + F(1))) /92 ((static_cast<F>(E::max() - E::min()) + F(1)) * (static_cast<F>(E::max() - E::min()) + F(1)))));93 }94 {95 typedef std::minstd_rand0 E;96 typedef double F;97 E r;98 F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r);99 assert(f == truncate_fp(100 (16807 - E::min() +101 (282475249 - E::min()) * (static_cast<F>(E::max() - E::min()) + F(1))) /102 ((static_cast<F>(E::max() - E::min()) + F(1)) * (static_cast<F>(E::max() - E::min()) + F(1)))));103 }104 105 return 0;106}107