Merge pull request #12107 from pchote/fix-atan-overflow

Fix integer overflows in Wangle.ArcTan.
This commit is contained in:
Oliver Brakmann
2016-09-26 18:35:54 +02:00
committed by GitHub

View File

@@ -91,11 +91,13 @@ namespace OpenRA
var ax = Math.Abs(x);
// Find the closest angle that satisfies y = x*tan(theta)
var bestVal = int.MaxValue;
// Uses a long to store bestVal to eliminate integer overflow issues in the common cases
// (may still fail for unrealistically large ax and ay)
var bestVal = long.MaxValue;
var bestAngle = 0;
for (var i = 0; i < 256; i += stride)
{
var val = Math.Abs(1024 * ay - ax * TanTable[i]);
var val = Math.Abs(1024 * ay - (long)ax * TanTable[i]);
if (val < bestVal)
{
bestVal = val;