swift
mathutils.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_MISC_MATH_MATHUTILS_H
7 #define SWIFT_MISC_MATH_MATHUTILS_H
8 
9 #include <QtCore/qmath.h>
10 
11 #include <cmath>
12 #include <numeric>
13 
14 #include <QPair>
15 #include <QRandomGenerator>
16 
17 #include "misc/swiftmiscexport.h"
18 
19 namespace swift::misc::math
20 {
23  {
24  public:
26  CMathUtils() = delete;
27 
29  static double round(double value, int digits);
30 
32  static double roundEpsilon(double value, double epsilon);
33 
36  static bool epsilonEqual(float v1, float v2, float epsilon = 1E-06f);
37  static bool epsilonEqual(double v1, double v2, double epsilon = 1E-06);
38  static bool epsilonEqualLimits(double v1, double v2)
39  {
40  return qAbs(v1 - v2) <= std::numeric_limits<double>::epsilon();
41  }
43 
46  static bool epsilonZero(double v, double epsilon) { return epsilonEqual(v, 0.0, epsilon); }
47  static bool epsilonZero(double v) { return epsilonEqual(v, 0.0, 1E-06); }
48  static inline bool epsilonZeroLimits(double v) { return qAbs(v) <= std::numeric_limits<double>::epsilon(); }
50 
52  static inline double trunc(double value, double epsilon = 1e-10)
53  {
54  return value < 0 ? ceil(value - epsilon) : floor(value + epsilon);
55  }
56 
58  static inline double fract(double value)
59  {
60  double unused;
61  return modf(value, &unused);
62  }
63 
65  static double deg2rad(double degree);
66 
68  static double rad2deg(double radians);
69 
71  static double normalizeDegrees180(double degrees);
72 
74  static double normalizeDegrees360(double degrees);
75 
77  static QRandomGenerator &randomGenerator();
78 
80  static int randomInteger(int low, int high);
81 
83  static double randomDouble(double max = 1);
84 
86  static bool randomBool();
87 
89  static int roundToMultipleOf(int value, int divisor);
90 
93  static QString fractionalPartAsString(double value, int width = -1);
94 
96  static double sum(const QList<double> &values);
97 
99  static double mean(const QList<double> &values);
100 
102  static double standardDeviation(const QList<double> &values);
103 
105  static QPair<double, double> standardDeviationAndMean(const QList<double> &values);
106 
107  private:
109  static double variance(const QList<double> &values);
110 
112  static QList<double> squaredDifferences(const QList<double> &values);
113 
115  static QList<double> squaredDifferences(const QList<double> &values, double meanValue);
116  };
117 } // namespace swift::misc::math
118 
119 #endif // SWIFT_MISC_MATH_MATHUTILS_H
static double fract(double value)
Fractional part of value.
Definition: mathutils.h:58
CMathUtils()=delete
No objects, just static.
static double trunc(double value, double epsilon=1e-10)
Nearest integer not greater in magnitude than value, correcting for epsilon.
Definition: mathutils.h:52
static bool epsilonZeroLimits(double v)
Epsilon safe zero.
Definition: mathutils.h:48
static bool epsilonZero(double v, double epsilon)
Epsilon safe zero.
Definition: mathutils.h:46
static bool epsilonZero(double v)
Epsilon safe zero.
Definition: mathutils.h:47
static bool epsilonEqualLimits(double v1, double v2)
Epsilon safe equal.
Definition: mathutils.h:38
#define SWIFT_MISC_EXPORT
Export a class or function from the library.