Skip to content

Commit

Permalink
Use fabs instead of == to compare double
Browse files Browse the repository at this point in the history
Signed-off-by: Kunlin Yu <[email protected]>
  • Loading branch information
kunlinyu committed Dec 27, 2024
1 parent e9b0f65 commit 324f90d
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions include/cql2cpp/node_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace cql2cpp {

constexpr double kEpsilon = 1e-5;

template <typename ValueType>
bool CheckValueNumberType(const std::string& op, size_t num,
const std::vector<ValueT>& vs, std::string* errmsg) {
Expand Down Expand Up @@ -49,7 +51,8 @@ inline bool isVariantEqual(const ValueT& a, const ValueT& b) {

if (std::holds_alternative<uint64_t>(a)) return TypedEqual<uint64_t>(a, b);

if (std::holds_alternative<double>(a)) return TypedEqual<double>(a, b);
if (std::holds_alternative<double>(a))
return fabs(std::get<double>(a) - std::get<double>(b)) < kEpsilon;

if (std::holds_alternative<std::string>(a))
return TypedEqual<std::string>(a, b);
Expand Down Expand Up @@ -242,7 +245,7 @@ const std::map<NodeType, std::map<Operator, NodeEval>> node_evals = {
}

if (lhs != nullptr and rhs != nullptr) {
*value = fabs(*lhs - *rhs) < 1e-5;
*value = fabs(*lhs - *rhs) < kEpsilon;
delete lhs;
delete rhs;
return true;
Expand Down

0 comments on commit 324f90d

Please sign in to comment.