From 1d22c3b500f0ca0df238657bb3f43baee51cbfcb Mon Sep 17 00:00:00 2001 From: Jon Malkin Date: Thu, 5 Mar 2020 16:05:46 -0800 Subject: [PATCH 1/4] check for NaN and inifite weights in varopt updates --- sampling/include/var_opt_sketch_impl.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sampling/include/var_opt_sketch_impl.hpp b/sampling/include/var_opt_sketch_impl.hpp index ddf0716a..a36a5793 100644 --- a/sampling/include/var_opt_sketch_impl.hpp +++ b/sampling/include/var_opt_sketch_impl.hpp @@ -724,8 +724,8 @@ std::string var_opt_sketch::items_to_string() const { template void var_opt_sketch::update(const T& item, double weight, bool mark) { - if (weight <= 0.0) { - throw std::invalid_argument("Item weights must be strictly positive. Found: " + if (weight <= 0.0 || std::isnan(weight) || std::isinf(weight)) { + throw std::invalid_argument("Item weights must be strictly positive and finite. Found: " + std::to_string(weight)); } ++n_; From 92aae69baee91a0c1d29e1a03e1a8ad6f74a4b5c Mon Sep 17 00:00:00 2001 From: Jon Malkin Date: Thu, 12 Mar 2020 18:56:08 -0700 Subject: [PATCH 2/4] ignore nonpositive, NaN or infinite weights instead of throwing --- sampling/include/var_opt_sketch_impl.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sampling/include/var_opt_sketch_impl.hpp b/sampling/include/var_opt_sketch_impl.hpp index a36a5793..0661994e 100644 --- a/sampling/include/var_opt_sketch_impl.hpp +++ b/sampling/include/var_opt_sketch_impl.hpp @@ -724,9 +724,8 @@ std::string var_opt_sketch::items_to_string() const { template void var_opt_sketch::update(const T& item, double weight, bool mark) { - if (weight <= 0.0 || std::isnan(weight) || std::isinf(weight)) { - throw std::invalid_argument("Item weights must be strictly positive and finite. Found: " - + std::to_string(weight)); + if (weight <= 0.0 || std::isnan(weight) || std::isinf(weight)) { + return; } ++n_; From cc6f49054082f0830e4d4e16f65dc4b7793166e7 Mon Sep 17 00:00:00 2001 From: Jon Malkin Date: Tue, 17 Mar 2020 11:52:24 -0700 Subject: [PATCH 3/4] ignore only 0.0 weights, throw on invalid values --- sampling/include/var_opt_sketch_impl.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sampling/include/var_opt_sketch_impl.hpp b/sampling/include/var_opt_sketch_impl.hpp index 0661994e..93f10b75 100644 --- a/sampling/include/var_opt_sketch_impl.hpp +++ b/sampling/include/var_opt_sketch_impl.hpp @@ -724,7 +724,10 @@ std::string var_opt_sketch::items_to_string() const { template void var_opt_sketch::update(const T& item, double weight, bool mark) { - if (weight <= 0.0 || std::isnan(weight) || std::isinf(weight)) { + if (weight < 0.0 || std::isnan(weight) || std::isinf(weight)) { + throw std::invalid_argument("Item weights must be nonnegativge and finite. Found: " + + std::to_string(weight)); + } else if (weight == 0.0) { return; } ++n_; From e2580a6b2beebf86d504dd627e4615dcb93400f4 Mon Sep 17 00:00:00 2001 From: Jon Malkin Date: Tue, 17 Mar 2020 12:00:28 -0700 Subject: [PATCH 4/4] check that 0.0 weight is ignored --- sampling/test/var_opt_sketch_test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sampling/test/var_opt_sketch_test.cpp b/sampling/test/var_opt_sketch_test.cpp index 2472a3a2..34811b50 100644 --- a/sampling/test/var_opt_sketch_test.cpp +++ b/sampling/test/var_opt_sketch_test.cpp @@ -240,6 +240,10 @@ class var_opt_sketch_test: public CppUnit::TestFixture { CPPUNIT_ASSERT_THROW_MESSAGE("update() accepted a negative weight", sk.update("invalid_weight", -1.0), std::invalid_argument); + + // should not throw but sketch shoulds till be empty + sk.update("zero weight", 0.0); + CPPUNIT_ASSERT(sk.is_empty()); } void corrupt_serialized_weight() {