diff --git a/src/terrama2/core/utility/TimeUtils.cpp b/src/terrama2/core/utility/TimeUtils.cpp index f18aaa5e8..a45d10904 100644 --- a/src/terrama2/core/utility/TimeUtils.cpp +++ b/src/terrama2/core/utility/TimeUtils.cpp @@ -147,21 +147,14 @@ double terrama2::core::TimeUtils::convertTimeString(const std::string& time, std throw terrama2::InitializationException() << terrama2::ErrorDescription(msg); } - bool isNegative = false; - if(timeStr.front() == '-') - { - isNegative = true; - timeStr.erase(0, 1); - } - // split the input time string in numeric value and unit std::string numberStr; std::string unitStr; std::partition_copy(std::begin(timeStr), std::end(timeStr), - std::back_inserter(numberStr), std::back_inserter(unitStr), - [](const char& ch) {return std::isdigit(ch);} ); + std::back_inserter(numberStr), + [](const char& ch) {return std::isalpha(ch);} ); // if no unit was given, use defaultUnit if(unitStr.empty()) unitStr = defaultUnit; @@ -203,7 +196,7 @@ double terrama2::core::TimeUtils::convertTimeString(const std::string& time, std } else { - QString msg(QObject::tr("Could not find any known unit of measure in the given string: %1.").arg(QString::fromStdString(time))); + QString msg(QObject::tr("Could not find any known unit of measure in the given string: %1.").arg(QString::fromStdString(timeStr))); TERRAMA2_LOG_ERROR() << msg; throw terrama2::InvalidArgumentException() << terrama2::ErrorDescription(msg); } @@ -211,10 +204,7 @@ double terrama2::core::TimeUtils::convertTimeString(const std::string& time, std if(unitName != "SECOND") result = unitsManager.getConversion("SECOND", unitName) * result; - if(isNegative) - return -result; - else - return result; + return result; } diff --git a/src/terrama2/services/analysis/core/utility/Utils.cpp b/src/terrama2/services/analysis/core/utility/Utils.cpp index b1e162e16..20ba06dcf 100644 --- a/src/terrama2/services/analysis/core/utility/Utils.cpp +++ b/src/terrama2/services/analysis/core/utility/Utils.cpp @@ -265,6 +265,29 @@ double terrama2::services::analysis::core::getValue(terrama2::core::Synchronized return value; } +/*! + Calculates the variance of the elements of a container + + source: http://roth.cs.kuleuven.be/w-ess/index.php/Accurate_variance_and_mean_calculations_in_C%2B%2B11 + + */ +template +T internal_variance(const Container& xs) +{ + // begin(xs) will point to the first element in xs + // end(xs) will point to the last element in xs + // the distance between them gives the number of elements + size_t N = end(xs) - begin(xs); + // first pass through all data (hidden in accumulate): + T m = std::accumulate(begin(xs), end(xs), T(0)) / N; + T s2 = 0; + // second pass through all data: + for(auto x : xs) { + s2 += (x - m) * (x - m); + } + return s2 / (N-1); +} + void terrama2::services::analysis::core::calculateStatistics(std::vector& values, OperatorCache& cache) { if(values.empty()) @@ -279,11 +302,17 @@ void terrama2::services::analysis::core::calculateStatistics(std::vector cache.min = min(acc); cache.max = max(acc); cache.median = median(acc); - cache.variance = variance(acc); - cache.standardDeviation = std::sqrt(variance(acc)); + //============================================ + // WARNING + // + // The boost function returned a different then expected value + // check internal_variance + // + // cache.variance = variance(acc); + cache.variance = internal_variance(values); + cache.standardDeviation = std::sqrt(cache.variance); } - double terrama2::services::analysis::core::getOperationResult(OperatorCache& cache, StatisticOperation statisticOperation) { switch(statisticOperation)