From 2ea78a588382278bc6be92e8203bfbdd48dada6e Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Wed, 14 Feb 2024 23:30:29 +0100 Subject: [PATCH] Fix float comparisons Replace this "==" with a more tolerant comparison operation. Floating point numbers should not be tested for equality cpp:S1244 --- AI/Nullkiller/Pathfinding/AINodeStorage.cpp | 4 ++-- lib/json/JsonValidator.cpp | 2 +- lib/pathfinder/CGPathNode.h | 2 +- lib/serializer/JsonSerializer.cpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/AI/Nullkiller/Pathfinding/AINodeStorage.cpp b/AI/Nullkiller/Pathfinding/AINodeStorage.cpp index 863549dd7..310f9d867 100644 --- a/AI/Nullkiller/Pathfinding/AINodeStorage.cpp +++ b/AI/Nullkiller/Pathfinding/AINodeStorage.cpp @@ -1265,8 +1265,8 @@ bool AINodeStorage::hasBetterChain( && nodeActor->heroFightingStrength >= candidateActor->heroFightingStrength && node.getCost() <= candidateNode->getCost()) { - if(nodeActor->heroFightingStrength == candidateActor->heroFightingStrength - && node.getCost() == candidateNode->getCost() + if(vstd::isAlmostEqual(nodeActor->heroFightingStrength, candidateActor->heroFightingStrength) + && vstd::isAlmostEqual(node.getCost(), candidateNode->getCost()) && &node < candidateNode) { continue; diff --git a/lib/json/JsonValidator.cpp b/lib/json/JsonValidator.cpp index c2941081c..db1ee2196 100644 --- a/lib/json/JsonValidator.cpp +++ b/lib/json/JsonValidator.cpp @@ -232,7 +232,7 @@ namespace std::string multipleOfCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data) { double result = data.Float() / schema.Float(); - if (floor(result) != result) + if (!vstd::isAlmostEqual(floor(result), result)) return validator.makeErrorMessage((boost::format("Value is not divisible by %d") % schema.Float()).str()); return ""; } diff --git a/lib/pathfinder/CGPathNode.h b/lib/pathfinder/CGPathNode.h index 28ee2f920..52e8e7d82 100644 --- a/lib/pathfinder/CGPathNode.h +++ b/lib/pathfinder/CGPathNode.h @@ -102,7 +102,7 @@ struct DLL_LINKAGE CGPathNode STRONG_INLINE void setCost(float value) { - if(value == cost) + if(vstd::isAlmostEqual(value, cost)) return; bool getUpNode = value < cost; diff --git a/lib/serializer/JsonSerializer.cpp b/lib/serializer/JsonSerializer.cpp index 32575e2e0..05b8e34a2 100644 --- a/lib/serializer/JsonSerializer.cpp +++ b/lib/serializer/JsonSerializer.cpp @@ -35,7 +35,7 @@ void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & val void JsonSerializer::serializeInternal(const std::string & fieldName, double & value, const std::optional & defaultValue) { - if(!defaultValue || defaultValue.value() != value) + if(!defaultValue || !vstd::isAlmostEqual(defaultValue.value(), value)) currentObject->operator[](fieldName).Float() = value; }