diff --git a/sampling/include/var_opt_sketch_impl.hpp b/sampling/include/var_opt_sketch_impl.hpp index ddf0716a..93f10b75 100644 --- a/sampling/include/var_opt_sketch_impl.hpp +++ b/sampling/include/var_opt_sketch_impl.hpp @@ -724,9 +724,11 @@ 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 nonnegativge and finite. Found: " + std::to_string(weight)); + } else if (weight == 0.0) { + return; } ++n_; 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() {