16 namespace swift::misc::math
21 double fractpart, intpart;
22 fractpart = modf(value, &intpart);
24 const double m = pow(10.0, digits);
25 const qint64 ri = qRound64(value * m);
26 const double rv =
static_cast<double>(ri) / m;
33 double fractpart, intpart;
34 fractpart = modf(value, &intpart);
36 const double roundValue = value / epsilon;
37 const qint64 ri = qRound64(roundValue);
38 const double rv =
static_cast<double>(ri) * epsilon;
53 return (result <= -180.0) ? 180.0 : result;
58 const double result = std::fmod(degrees, 360.0);
59 return (result >= 0.0) ? result : result + 360.0;
70 Q_ASSERT_X(high < INT_MAX, Q_FUNC_INFO,
"Cannot add 1");
76 constexpr
int MAX(std::min(RAND_MAX - 1, INT_MAX - 1));
78 return (r / MAX) * max;
85 Q_ASSERT(divisor != 0);
86 Q_ASSERT(divisor >= -std::numeric_limits<int>::max());
87 divisor = std::abs(divisor);
88 Q_ASSERT(std::abs(value) < std::numeric_limits<int>::max() - divisor / 2);
90 int multiplier = value / divisor;
91 int remainder = std::abs(value % divisor);
92 int shortfall = divisor - remainder;
94 if (shortfall < remainder) { multiplier += value < 0 ? -1 : 1; }
96 return multiplier * divisor;
102 const double fractpart = modf(value, &intpart);
103 const int prec = width >= 0 ? width + 1 : 10;
106 if (width < 0) {
return fInt; }
107 if (fInt.
length() >= width) {
return fInt.
left(width); }
114 for (
double v : values) {
sum += v; }
120 const double meanValue =
mean(values);
121 return squaredDifferences(values, meanValue);
127 for (
double v : values)
129 const double vd = v - meanValue;
132 return squaresDifferences;
138 return sum(values) / values.
size();
143 const double variance =
mean(squaredDifferences(values));
149 const double sd = sqrt(variance(values));
155 const double meanValue =
mean(values);
156 const double varianceValue =
mean(squaredDifferences(values, meanValue));
157 const double sd = sqrt(varianceValue);
158 return QPair<double, double>(sd, meanValue);
static double rad2deg(double radians)
Radians to degrees.
static double deg2rad(double degree)
Degrees to radians.
static bool epsilonEqual(float v1, float v2, float epsilon=1E-06f)
Epsilon safe equal.
static QRandomGenerator & randomGenerator()
Thread-local random generator.
static bool epsilonZeroLimits(double v)
Epsilon safe zero.
static int randomInteger(int low, int high)
Random number between low and high.
static bool randomBool()
Random boolean.
static double sum(const QList< double > &values)
Calculate the sum.
static int roundToMultipleOf(int value, int divisor)
Round numToRound to the nearest multiple of divisor.
static double normalizeDegrees180(double degrees)
Normalize: -180< degrees ≤180.
static QPair< double, double > standardDeviationAndMean(const QList< double > &values)
Standard deviation (first) and mean (second)
static double standardDeviation(const QList< double > &values)
Calculate the standard deviation.
static double mean(const QList< double > &values)
Calculate the mean.
static double round(double value, int digits)
Utility round method.
static QString fractionalPartAsString(double value, int width=-1)
Fractional part as integer string, e.g. 3.12 -> 12 / 3.012 -> 012.
static double normalizeDegrees360(double degrees)
Normalize: 0≤ degrees <360.
static double randomDouble(double max=1)
Random double 0-max.
static double roundEpsilon(double value, double epsilon)
Round by given epsilon.
bool isEmpty() const const
void push_back(QList< T >::parameter_type value)
qsizetype size() const const
double bounded(double highest)
QRandomGenerator * global()
QString left(qsizetype n) &&
QString leftJustified(qsizetype width, QChar fill, bool truncate) const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) &&
QString number(double n, char format, int precision)
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.