summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorAnton Korshunov <anton.korshunov@mongodb.com>2019-12-10 12:01:36 +0000
committerevergreen <evergreen@mongodb.com>2019-12-10 12:01:36 +0000
commit2a455afd9775631c1ddb041cfdababc27b833810 (patch)
tree021eae9c38969370240160e72b3be3406838dadd /src/mongo
parenta5a12709b4ed00d9cdb579da3b2ce03ad6fb6fb4 (diff)
downloadmongo-2a455afd9775631c1ddb041cfdababc27b833810.tar.gz
SERVER-43764 Add more testing for special values in DoubleDoubleSummation
(cherry picked from commit abe90fb2090ead3fdbf9f62d5db50c88eb31d527)
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/db/pipeline/accumulator_sum.cpp10
-rw-r--r--src/mongo/util/summation_test.cpp28
2 files changed, 29 insertions, 9 deletions
diff --git a/src/mongo/db/pipeline/accumulator_sum.cpp b/src/mongo/db/pipeline/accumulator_sum.cpp
index 0200288cdfa..551bc181ee7 100644
--- a/src/mongo/db/pipeline/accumulator_sum.cpp
+++ b/src/mongo/db/pipeline/accumulator_sum.cpp
@@ -120,15 +120,7 @@ Value AccumulatorSum::getValue(bool toBeMerged) {
case NumberDouble:
return Value(nonDecimalTotal.getDouble());
case NumberDecimal: {
- double sum, error;
- std::tie(sum, error) = nonDecimalTotal.getDoubleDouble();
- Decimal128 total; // zero
- if (sum != 0) {
- total = total.add(Decimal128(sum, Decimal128::kRoundTo34Digits));
- total = total.add(Decimal128(error, Decimal128::kRoundTo34Digits));
- }
- total = total.add(decimalTotal);
- return Value(total);
+ return Value(decimalTotal.add(nonDecimalTotal.getDecimal()));
}
default:
MONGO_UNREACHABLE;
diff --git a/src/mongo/util/summation_test.cpp b/src/mongo/util/summation_test.cpp
index 72b29cc47de..c04e6b933ff 100644
--- a/src/mongo/util/summation_test.cpp
+++ b/src/mongo/util/summation_test.cpp
@@ -204,4 +204,32 @@ TEST(Summation, AddDoubles) {
ASSERT_EQUALS(sum.getDouble(), doubleValuesSum);
ASSERT(straightSum != sum.getDouble());
}
+
+TEST(Summation, ConvertInfinityToDecimal) {
+ constexpr double infinity = std::numeric_limits<double>::infinity();
+ DoubleDoubleSummation sum;
+
+ sum.addDouble(infinity);
+ ASSERT_EQUALS(infinity, sum.getDouble());
+ ASSERT_TRUE(sum.getDecimal().isInfinite());
+ ASSERT_FALSE(sum.getDecimal().isNaN());
+
+ sum.addDouble(1);
+ ASSERT_EQUALS(infinity, sum.getDouble());
+ ASSERT_TRUE(sum.getDecimal().isInfinite());
+ ASSERT_FALSE(sum.getDecimal().isNaN());
+}
+
+TEST(Summation, ConvertNaNToDecimal) {
+ constexpr double nan = std::numeric_limits<double>::quiet_NaN();
+ DoubleDoubleSummation sum;
+
+ sum.addDouble(nan);
+ ASSERT_TRUE(sum.getDecimal().isNaN());
+ ASSERT_FALSE(sum.getDecimal().isInfinite());
+
+ sum.addDouble(1);
+ ASSERT_TRUE(sum.getDecimal().isNaN());
+ ASSERT_FALSE(sum.getDecimal().isInfinite());
+}
} // namespace mongo