Browse Source

Better float comparison

Ivan Savenko 1 year ago
parent
commit
0d74959a33
2 changed files with 4 additions and 3 deletions
  1. 3 2
      Global.h
  2. 1 1
      lib/gameState/CGameState.cpp

+ 3 - 2
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<typename Floating1, typename Floating2>
 	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

+ 1 - 1
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<const CArmedInstance *>(object.get());
+				const auto * ai = dynamic_cast<const CArmedInstance *>(object.get());
 				if(ai && ai->getOwner() == player)
 				{
 					for(const auto & elem : ai->Slots()) //iterate through army