109 lines
5.0 KiB
C++
109 lines
5.0 KiB
C++
// This file is part of c-evo-evo.
|
|
//
|
|
// This software is governed by the CeCILL license under French law and
|
|
// abiding by the rules of distribution of free software. You can use,
|
|
// modify and/or redistribute the software under the terms of the CeCILL
|
|
// license as circulated by CEA, CNRS and INRIA at the following URL
|
|
// "http://www.cecill.info".
|
|
//
|
|
// As a counterpart to the access to the source code and rights to copy,
|
|
// modify and redistribute granted by the license, users are provided only
|
|
// with a limited warranty and the software's author, the holder of the
|
|
// economic rights, and the successive licensors have only limited
|
|
// liability.
|
|
//
|
|
// In this respect, the user's attention is drawn to the risks associated
|
|
// with loading, using, modifying and/or developing or reproducing the
|
|
// software by the user in light of its specific status of free software,
|
|
// that may mean that it is complicated to manipulate, and that also
|
|
// therefore means that it is reserved for developers and experienced
|
|
// professionals having in-depth computer knowledge. Users are therefore
|
|
// encouraged to load and test the software's suitability as regards their
|
|
// requirements in conditions enabling the security of their systems and/or
|
|
// data to be ensured and, more generally, to use and operate it in the
|
|
// same conditions as regards security.
|
|
//
|
|
// The fact that you are presently reading this means that you have had
|
|
// knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
#include "Cevo/PrefixMultiplier.h"
|
|
|
|
#include <cmath>
|
|
|
|
namespace Cevo {
|
|
|
|
const PrefixMultiplierDef BasePrefixMultipliers = { {
|
|
{ QStringLiteral("y"), QStringLiteral("yocto"), 1e-24 },
|
|
{ QStringLiteral("z"), QStringLiteral("zepto"), 1e-21 },
|
|
{ QStringLiteral("a"), QStringLiteral("atto"), 1e-18 },
|
|
{ QStringLiteral("f"), QStringLiteral("femto"), 1e-15 },
|
|
{ QStringLiteral("p"), QStringLiteral("piko"), 1e-12 },
|
|
{ QStringLiteral("n"), QStringLiteral("nano"), 1e-9 },
|
|
{ QStringLiteral("u"), QStringLiteral("mikro"), 1e-6 },
|
|
{ QStringLiteral("m"), QStringLiteral("mili"), 1e-3 },
|
|
{ QString(), QString(), 1e0 },
|
|
{ QStringLiteral("k"), QStringLiteral("kilo"), 1e3 },
|
|
{ QStringLiteral("M"), QStringLiteral("mega"), 1e6 },
|
|
{ QStringLiteral("G"), QStringLiteral("giga"), 1e9 },
|
|
{ QStringLiteral("T"), QStringLiteral("tera"), 1e12 },
|
|
{ QStringLiteral("P"), QStringLiteral("peta"), 1e15 },
|
|
{ QStringLiteral("E"), QStringLiteral("exa"), 1e18 },
|
|
{ QStringLiteral("Z"), QStringLiteral("zetta"), 1e21 },
|
|
{ QStringLiteral("Y"), QStringLiteral("yotta"), 1e24 },
|
|
} };
|
|
|
|
const PrefixMultiplierDef TimePrefixMultipliers = { {
|
|
{ QStringLiteral("ys"), QStringLiteral("yocto"), 1e-24 },
|
|
{ QStringLiteral("zs"), QStringLiteral("zepto"), 1e-21 },
|
|
{ QStringLiteral("as"), QStringLiteral("atto"), 1e-18 },
|
|
{ QStringLiteral("fs"), QStringLiteral("femto"), 1e-15 },
|
|
{ QStringLiteral("ps"), QStringLiteral("piko"), 1e-12 },
|
|
{ QStringLiteral("ns"), QStringLiteral("nano"), 1e-9 },
|
|
{ QStringLiteral("us"), QStringLiteral("mikro"), 1e-6 },
|
|
{ QStringLiteral("ms"), QStringLiteral("mili"), 1e-3 },
|
|
{ QStringLiteral("s"), QStringLiteral("sekunda"), 1 },
|
|
{ QStringLiteral("min"), QStringLiteral("minuta"), 60 },
|
|
{ QStringLiteral("hod"), QStringLiteral("hodina"), 3600 },
|
|
{ QStringLiteral("den"), QStringLiteral("den"), 24 * 3600 },
|
|
{ QStringLiteral("t\u00FDd"), QStringLiteral("t\u00FDden"), 7 * 24 * 3600 },
|
|
{ QStringLiteral("m\u011Bs"), QStringLiteral("m\u011Bs\u00EDc"), 30 * 24 * 3600 },
|
|
{ QStringLiteral("rok"), QStringLiteral("rok"), 365 * 24 * 3600 },
|
|
{ QStringLiteral("stolet\u00ED"), QStringLiteral("stolet\u00ED"), 3153600000.0 },
|
|
{ QStringLiteral("tis\u00EDcilet\u00ED"), QStringLiteral("tis\u00EDcilet\u00ED"), 3153600000.0 },
|
|
} };
|
|
|
|
double truncateDigits(double value, int digits)
|
|
{
|
|
for (int ii = 2; ii >= -1; --ii) {
|
|
if (value >= std::pow(10.0, ii)) {
|
|
const int realDigits = digits < (ii + 1) ? ii + 1 : digits;
|
|
const double scale = std::pow(10.0, ii - realDigits + 1);
|
|
return std::round(value / scale) * scale;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
QString formatWithPrefix(double value, const PrefixMultiplierDef &prefixMultipliers, const QString &unitText,
|
|
int digits)
|
|
{
|
|
const int size = static_cast<int>(prefixMultipliers.size());
|
|
int i = 8;
|
|
if (value == 0) {
|
|
// stays at the base-unit entry
|
|
} else if (value > 1) {
|
|
// bounds check moved before the array access (the original checks
|
|
// it after, which can read one element past the end of the array).
|
|
while (i + 1 < size && i + 1 >= 0 && (value / prefixMultipliers[i + 1].value) > 1)
|
|
++i;
|
|
} else {
|
|
while (i - 1 >= 0 && i - 1 < size && (value / prefixMultipliers[i - 1].value) < 1)
|
|
--i;
|
|
++i;
|
|
}
|
|
value = value / prefixMultipliers[i].value;
|
|
return QString::number(truncateDigits(value, digits)) + ' ' + prefixMultipliers[i].shortText + unitText;
|
|
}
|
|
|
|
} // namespace Cevo
|