SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
numericparsing.cpp
Go to the documentation of this file.
1// SedSAT3 – Sediment Source Assessment Tool, version 3
2// U.S. Geological Survey. See LICENSE.md and DISCLAIMER.md in the repository root.
3
4#include "numericparsing.h"
5
6#include <QJsonValue>
7#include <QRegularExpression>
8#include <QVariant>
9
10bool parseNumericCell(const QString& text, double& out, QString& error)
11{
12 const QString trimmed = text.trimmed();
13 if (trimmed.isEmpty())
14 {
15 error = QStringLiteral("the cell is empty");
16 return false;
17 }
18
19 // QString::toDouble accepts "nan", "inf" and "infinity", which are not data
20 static const QRegularExpression number(
21 QStringLiteral("^[+-]?(\\d+\\.?\\d*|\\.\\d+)([eE][+-]?\\d+)?$"));
22 if (!number.match(trimmed).hasMatch())
23 {
24 static const QRegularExpression non_finite(
25 QStringLiteral("^[+-]?(nan|inf|infinity)$"), QRegularExpression::CaseInsensitiveOption);
26 static const QRegularExpression comma_number(QStringLiteral("^[+-]?\\d*,\\d+$"));
27 if (non_finite.match(trimmed).hasMatch())
28 error = QStringLiteral("'%1' is not a finite number").arg(trimmed);
29 else if (comma_number.match(trimmed).hasMatch())
30 error = QStringLiteral("'%1' is not a number; use a point as the decimal separator "
31 "and no thousands separator").arg(trimmed);
32 else
33 error = QStringLiteral("'%1' is not a number").arg(trimmed);
34 return false;
35 }
36
37 bool ok = false;
38 const double value = trimmed.toDouble(&ok);
39 if (!ok || !std::isfinite(value))
40 {
41 error = QStringLiteral("'%1' is too large to represent").arg(trimmed);
42 return false;
43 }
44 out = value;
45 return true;
46}
47
48bool parseNumericValue(const QVariant& value, double& out, QString& error)
49{
50 switch (value.typeId())
51 {
52 case QMetaType::Double:
53 case QMetaType::Float:
54 case QMetaType::Int:
55 case QMetaType::UInt:
56 case QMetaType::LongLong:
57 case QMetaType::ULongLong:
58 {
59 const double number = value.toDouble();
60 if (!std::isfinite(number))
61 {
62 error = QStringLiteral("'%1' is not a finite number").arg(value.toString());
63 return false;
64 }
65 out = number;
66 return true;
67 }
68 case QMetaType::QString:
69 return parseNumericCell(value.toString(), out, error);
70 default:
71 if (!value.isValid() || value.isNull())
72 error = QStringLiteral("the cell is empty");
73 else
74 error = QStringLiteral("'%1' is not a number").arg(value.toString());
75 return false;
76 }
77}
78
79bool parseNumericJson(const QJsonValue& value, double& out, QString& error)
80{
81 if (value.isDouble())
82 {
83 const double number = value.toDouble();
84 if (!std::isfinite(number))
85 {
86 error = QStringLiteral("the value is not a finite number");
87 return false;
88 }
89 out = number;
90 return true;
91 }
92 if (value.isString())
93 return parseNumericCell(value.toString(), out, error);
94 if (value.isNull())
95 error = QStringLiteral("the value is missing (NaN and infinity are saved as null)");
96 else if (value.isUndefined())
97 error = QStringLiteral("the value is missing");
98 else
99 error = QStringLiteral("the value is not a number");
100 return false;
101}
bool parseNumericJson(const QJsonValue &value, double &out, QString &error)
Parses a project-file value as a finite number.
bool parseNumericValue(const QVariant &value, double &out, QString &error)
Parses a spreadsheet cell value as a finite number.
bool parseNumericCell(const QString &text, double &out, QString &error)
Parses text as a finite number.
The one place where data values entering SedSAT3 are checked.