Ref T111, lat/lng/angle changes

* get struct CAngle::DegMinSecFractionalSec to obtain parts
* round to epsilon utility functions and fix (qint64)
This commit is contained in:
Klaus Basan
2017-07-26 14:38:49 +02:00
committed by Mathew Sutcliffe
parent 77546a46b1
commit e55480737e
15 changed files with 328 additions and 102 deletions

View File

@@ -53,10 +53,12 @@ namespace BlackMisc
double CMathUtils::roundEpsilon(double value, double epsilon)
{
if (epsilon == 0) { return value; } // avoid division by 0
double fractpart, intpart;
fractpart = modf(value, &intpart);
if (fractpart == 0) return value; // do not mess any "integers" to the worse
qint64 ri = qRound(value / epsilon);
if (fractpart == 0) { return value; } // do not mess any "integers" to the worse
const double roundValue = value / epsilon;
qint64 ri = qRound64(roundValue);
double rv = static_cast<double>(ri) * epsilon; // do not loose any range here
return rv;
}
@@ -113,5 +115,15 @@ namespace BlackMisc
return multiplier * divisor;
}
QString CMathUtils::fractionalPartAsString(double value, int width)
{
double intpart;
const double fractpart = modf(value, &intpart);
const QString f = QString::number(fractpart);
const QString fInt = f.length() < 3 ? QString("0") : f.mid(2);
if (width < 0) { return fInt; }
if (fInt.length() >= width) { return fInt.left(width); }
return fInt.leftJustified(width, '0');
}
} // namespace
} // namespace

View File

@@ -21,7 +21,6 @@ namespace BlackMisc
{
namespace Math
{
//! Math utils
class BLACKMISC_EXPORT CMathUtils
{
@@ -103,10 +102,13 @@ namespace BlackMisc
//! Random number between low and high
static int randomInteger(int low, int high);
//! Round numToRound to the nearest multiple of multiple
//! Round numToRound to the nearest multiple of divisor
static int roundToMultipleOf(int value, int divisor);
};
//! Fractional part as integer string, e.g. 3.12 -> 12 / 3.012 -> 012
//! \remark because of leading 0 returned as string
static QString fractionalPartAsString(double value, int width = -1);
};
} // namespace
} // namespace