From 0d74959a3320d805f0631b9c28f8410bf53643c3 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Wed, 14 Feb 2024 12:07:01 +0200 Subject: [PATCH] Better float comparison --- Global.h | 5 +++-- lib/gameState/CGameState.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Global.h b/Global.h index 43ee6d7ec..7834b9a76 100644 --- a/Global.h +++ b/Global.h @@ -689,13 +689,14 @@ namespace vstd bool isAlmostZero(const Floating & value) { constexpr Floating epsilon(0.00001); - return std::abs(value) < epsilon; + return std::abs(value) <= epsilon; } template bool isAlmostEqual(const Floating1 & left, const Floating2 & right) { - return isAlmostZero(left - right); + const auto relativeEpsilon = std::max(std::abs(left), std::abs(right)) * 0.00001; + return std::abs(left - right) <= relativeEpsilon; } ///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index 545c2b43c..ebe7b6710 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -1383,7 +1383,7 @@ bool CGameState::checkForVictory(const PlayerColor & player, const EventConditio int total = 0; //creature counter for(auto object : map->objects) { - const CArmedInstance *ai = dynamic_cast(object.get()); + const auto * ai = dynamic_cast(object.get()); if(ai && ai->getOwner() == player) { for(const auto & elem : ai->Slots()) //iterate through army