cauldron  0.0.0
 All Classes Namespaces Files Functions Variables Typedefs
floats.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <algorithm>
5 #include <limits>
6 #include <functional>
7 
8 #include "bases.h"
9 
10 
11 namespace cauldron {
12 template<typename Value>
13 void validate_floats(Value min_value,
14  Value max_value) {
15  if (max_value - min_value > std::numeric_limits<Value>::max()) {
16  throw std::invalid_argument(
17  "Difference between ``max_value`` and ``min_value`` "
18  "should not be greater than max possible value for type."
19  );
20  }
21 }
22 
23 
28 template<typename Value>
29 class Floats : public CloneHelper<Value, Floats<Value>> {
30  static_assert(std::is_floating_point<Value>(),
31  "``Value`` should have floating point type.");
32  public:
37  explicit Floats(Value min_value = 0.,
38  Value max_value = 1.) {
39  validate_floats<Value>(min_value,
40  max_value);
41  min_value_ = min_value;
42  max_value_ = max_value;
43  };
44 
48  Value operator()() const override {
49  static std::random_device random_device;
50  auto distribution = std::uniform_real_distribution<Value>(min_value_,
51  max_value_);
52  auto result = distribution(random_device);
53  return result;
54  }
55 
56  private:
57  Value min_value_;
58  Value max_value_;
59 };
60 }
Value operator()() const override
Definition: floats.h:48
Definition: floats.h:29
Definition: bases.h:97
void validate_floats(Value min_value, Value max_value)
Definition: floats.h:13
Floats(Value min_value=0., Value max_value=1.)
Definition: floats.h:37