16namespace swift::misc::math
22 double fractpart = modf(value, &intpart);
27 const double m = pow(10.0, digits);
28 const qint64 ri = qRound64(value * m);
29 const double rv =
static_cast<double>(ri) / m;
40 double fractpart = modf(value, &intpart);
45 const double roundValue = value / epsilon;
46 const qint64 ri = qRound64(roundValue);
47 const double rv =
static_cast<double>(ri) * epsilon;
62 return (result <= -180.0) ? 180.0 : result;
67 const double result = std::fmod(degrees, 360.0);
68 return (result >= 0.0) ? result : result + 360.0;
79 Q_ASSERT_X(high < INT_MAX, Q_FUNC_INFO,
"Cannot add 1");
85 constexpr int MAX(std::min(RAND_MAX - 1, INT_MAX - 1));
87 return (r / MAX) * max;
94 Q_ASSERT(divisor != 0);
95 Q_ASSERT(divisor >= -std::numeric_limits<int>::max());
96 divisor = std::abs(divisor);
97 Q_ASSERT(std::abs(value) < std::numeric_limits<int>::max() - divisor / 2);
99 int multiplier = value / divisor;
100 int remainder = std::abs(value % divisor);
101 int shortfall = divisor - remainder;
103 if (shortfall < remainder)
105 multiplier += value < 0 ? -1 : 1;
108 return multiplier * divisor;
114 const double fractpart = modf(value, &intpart);
115 const int prec = width >= 0 ? width + 1 : 10;
122 if (fInt.
length() >= width)
124 return fInt.
left(width);
132 for (
double v : values)
141 const double meanValue =
mean(values);
142 return squaredDifferences(values, meanValue);
148 for (
double v : values)
150 const double vd = v - meanValue;
153 return squaresDifferences;
159 return sum(values) / values.
size();
164 const double variance =
mean(squaredDifferences(values));
170 const double sd = sqrt(variance(values));
176 const double meanValue =
mean(values);
177 const double varianceValue =
mean(squaredDifferences(values, meanValue));
178 const double sd = sqrt(varianceValue);
179 return { 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.