Incorrect comparison? If statement not returning proper results.
9593793706205999104 < 18446744073709551615 Now, call me crazy, but this statement is true right?
But these two numbers are dist2 and range, the variables being compared below, and it keeps resulting false. Am I doing something wrong here?
(P.S) isMapped is not the problem, I've already ran the debug and it definitely is the inequality.
List<Point> pointsWithinRange(Point point, ulong range)
{
List<Point> points = new List<Point>();
List<Orbital> sector = new List<Orbital>();
if (point.parent == null)
{
for (int i = 0; i < galaxy.solarSystems.Count; i++)
sector.Add(galaxy.solarSystems[i]);
}
else
{
for (int i = 0; i < point.parent.children.Count; i++)
sector.Add(point.parent.children[i]);
}
for (int i = 0; i < sector.Count; i++)
{
ulong dist2 = Distance2((ulong)point.pos.x, (ulong)point.pos.y, (ulong)sector[i].position.x, (ulong)sector[i].position.y);
if (dist2 < (range*range) && !isMapped(sector[i], centerPoint))
{
points.Add(new Point(sector[i]));
Debug.Log("Point Found within Range");
}
else
{
Debug.Log("Point found outside range. Dist is: "+dist2+" while range is: "+range);
}
}
if (points.Count <= 0)
return null;
return points;
}
ulong Distance2(ulong x1, ulong y1, ulong x2, ulong y2)
{
return ((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1));
}
Did you set
ulong range = 18446744073709551615; // ulong.$$anonymous$$axValue
for your test?
Then range*range in your if-statement becomes 1 because of an overflow. (Writing ulong.$$anonymous$$axValue * ulong.$$anonymous$$axValue directly would even throw an error: The operation overflows at compile time in checked mode)
If range is 18446744073709551615, then (range * range) creates an overflow. Or did you mean (range * range) is 18446744073709551615 ?
Answer by A20T3M4 · Oct 07, 2016 at 04:33 PM
No way, that makes sense. But I was squaring range (which range is ulong.max for testing), because I'm not sure how to sqrt dist, since Mathf returns floats. Any suggestions there?