BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
numericutils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/utils/numericutils.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include <algorithm>
17 #include <cmath>
18 #include <limits>
19 #include <random>
20 
21 using namespace ModelView;
22 
23 bool Utils::AreAlmostEqual(double a, double b, double tolerance)
24 {
25  constexpr double eps = std::numeric_limits<double>::epsilon();
26  return std::abs(a - b)
27  <= eps * std::max(tolerance * eps, std::max(1., tolerance) * std::abs(b));
28 }
29 
30 int Utils::RandInt(int low, int high)
31 {
32  std::random_device rd;
33  std::mt19937 gen(rd());
34  std::uniform_int_distribution<int> uniform_int(low, high);
35  return uniform_int(gen);
36 }
37 
38 double Utils::RandDouble(double low, double high)
39 {
40  std::random_device rd;
41  std::mt19937 gen(rd());
42  std::uniform_real_distribution<> uniform_real(low, high);
43  return uniform_real(gen);
44 }
MVVM_MODEL_EXPORT bool AreAlmostEqual(double a, double b, double tolerance_factor=1.0)
Returns true if two doubles agree within epsilon*tolerance.
MVVM_MODEL_EXPORT double RandDouble(double low, double high)
Produces random FLOAT values uniformly distributed on the interval [low, high).
MVVM_MODEL_EXPORT int RandInt(int low, int high)
Produces random integer values uniformly distributed on the closed interval [low, high].
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?