1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-10 00:43:59 +02:00

Better float comparison

This commit is contained in:
Ivan Savenko 2024-02-14 12:07:01 +02:00
parent 6d6137accc
commit 0d74959a33
2 changed files with 4 additions and 3 deletions

View File

@ -689,13 +689,14 @@ namespace vstd
bool isAlmostZero(const Floating & value) bool isAlmostZero(const Floating & value)
{ {
constexpr Floating epsilon(0.00001); constexpr Floating epsilon(0.00001);
return std::abs(value) < epsilon; return std::abs(value) <= epsilon;
} }
template<typename Floating1, typename Floating2> template<typename Floating1, typename Floating2>
bool isAlmostEqual(const Floating1 & left, const Floating2 & right) 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 ///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr

View File

@ -1383,7 +1383,7 @@ bool CGameState::checkForVictory(const PlayerColor & player, const EventConditio
int total = 0; //creature counter int total = 0; //creature counter
for(auto object : map->objects) for(auto object : map->objects)
{ {
const CArmedInstance *ai = dynamic_cast<const CArmedInstance *>(object.get()); const auto * ai = dynamic_cast<const CArmedInstance *>(object.get());
if(ai && ai->getOwner() == player) if(ai && ai->getOwner() == player)
{ {
for(const auto & elem : ai->Slots()) //iterate through army for(const auto & elem : ai->Slots()) //iterate through army