From 8fab99e920a56cab7b16640e3be74dbb9926d453 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 10 Feb 2022 17:56:17 -0500 Subject: [PATCH] LibSQL: Use absolute value when comparing against floating point epsilon Otherwise, any value that is less than another value would be considered about equal by mistake. --- Userland/Libraries/LibSQL/Value.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibSQL/Value.cpp b/Userland/Libraries/LibSQL/Value.cpp index 80ddeea19e7603..0e1146e10e2d5d 100644 --- a/Userland/Libraries/LibSQL/Value.cpp +++ b/Userland/Libraries/LibSQL/Value.cpp @@ -773,8 +773,11 @@ int FloatImpl::compare(Value const& other) const if (!casted.has_value()) { return 1; } + auto diff = value() - casted.value(); - return (diff < NumericLimits::epsilon()) ? 0 : ((diff > 0) ? 1 : -1); + if (fabs(diff) < NumericLimits::epsilon()) + return 0; + return diff < 0 ? -1 : 1; } String BooleanImpl::to_string() const