Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
nan_rejecting_distribution.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_NAN_REJECTING_DISTRIBUTION_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_NAN_REJECTING_DISTRIBUTION_HPP_
7
10#include "arene/base/stdlib_choice/declval.hpp"
11#include "arene/base/stdlib_choice/enable_if.hpp"
12#include "arene/base/stdlib_choice/is_arithmetic.hpp"
13#include "arene/base/stdlib_choice/is_default_constructible.hpp"
14#include "arene/base/stdlib_choice/is_floating_point.hpp"
15
16namespace arene {
17namespace base {
18namespace testing {
19
20/// @brief A distribution adaptor that discards NaN values drawn from the wrapped distribution and redraws
21/// @tparam Distribution A distribution return floating point values
22template <class Distribution>
24 public:
25 /// @brief The value type produced by this adaptor
26 using result_type = typename Distribution::result_type;
27
28 static_assert(
30 "nan_rejecting_distribution only makes sense for arithmetic distributions"
31 );
32
33 /// @brief Default construct adapted distribution
34 template < //
35 class T = Distribution,
37 constexpr nan_rejecting_distribution() noexcept
38 : dist_{} {}
39
40 /// @brief Construct the adaptor wrapping the given distribution
41 /// @param dist The distribution to adapt
42 explicit constexpr nan_rejecting_distribution(Distribution dist) noexcept
43 : dist_{dist} {}
44
45 /// @brief Draw a non-NaN value from the wrapped distribution; SFINAE overload for floating-point types
46 /// @tparam PRNG A bit generator (PRNG) type satisfying UniformRandomBitGenerator
47 /// @param prng A bit generator (PRNG) instance
48 /// @return A value drawn from the wrapped distribution with NaN results discarded and redrawn
49 template <
50 class PRNG,
51 class T = result_type,
53 constexpr auto operator()(PRNG& prng) const noexcept(noexcept(std::declval<Distribution const&>()(prng)))
54 -> result_type {
55 auto value = dist_(prng);
56 while (isnan(value)) {
57 value = dist_(prng);
58 }
59 return value;
60 }
61
62 /// @brief Draw a non-NaN value from the wrapped distribution; SFINAE overload for non-floating-point types
63 /// @tparam PRNG A bit generator (PRNG) type satisfying UniformRandomBitGenerator
64 /// @param prng A bit generator (PRNG) instance
65 /// @return Any value drawn from the wrapped distribution; it's assumed that NaNs can not actually be generated
66 template <
67 class PRNG,
68 class T = result_type,
70 constexpr auto operator()(PRNG& prng) const noexcept(noexcept(std::declval<Distribution const&>()(prng)))
71 -> result_type {
72 return dist_(prng);
73 }
74
75 private:
76 Distribution dist_;
77};
78
79} // namespace testing
80} // namespace base
81} // namespace arene
82
83#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_NAN_REJECTING_DISTRIBUTION_HPP_
typename Distribution::result_type result_type
The value type produced by this adaptor.
Definition nan_rejecting_distribution.hpp:26
constexpr nan_rejecting_distribution() noexcept
Default construct adapted distribution.
Definition nan_rejecting_distribution.hpp:37
constexpr auto operator()(PRNG &prng) const noexcept(noexcept(std::declval< Distribution const & >()(prng))) -> result_type
Draw a non-NaN value from the wrapped distribution; SFINAE overload for floating-point types.
Definition nan_rejecting_distribution.hpp:53
constexpr nan_rejecting_distribution(Distribution dist) noexcept
Construct the adaptor wrapping the given distribution.
Definition nan_rejecting_distribution.hpp:42
static constexpr value_type value
Definition integral_constant.hpp:34
Definition customization.hpp:31
Definition accumulate.hpp:37
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...