mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Fix float comparisons
Replace this "==" with a more tolerant comparison operation. Floating point numbers should not be tested for equality cpp:S1244
This commit is contained in:
		| @@ -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; | ||||
|   | ||||
| @@ -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 ""; | ||||
| 		} | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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<double> & defaultValue) | ||||
| { | ||||
| 	if(!defaultValue || defaultValue.value() != value) | ||||
| 	if(!defaultValue || !vstd::isAlmostEqual(defaultValue.value(), value)) | ||||
| 		currentObject->operator[](fieldName).Float() = value; | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user