I am trying to prevent overflow in two situations, one is when multipling a long with a float, and the other is when adding two longs.
Like:
public static long multiplyLong(long value, float multiplier)
{
if (value * multiplier > long.MaxValue)
{
Debug.Log("greater then max value");
return value = long.MaxValue;
}
else
{
Debug.Log("not greater then max value");
return (long) (value * multiplier);
}
}
public static long addLong(long value, long adder)
{
if(value + adder > long.MaxValue)
{
Debug.Log("greater then max value");
return value = long.MaxValue;
}
else
{
Debug.Log("not greater then max value");
return value + adder;
}
}
"multiplyLong" works perfectly, while "addLong" doesn't work like it should. When using addLong, and there will be a overflow it doesn't detect the overflow. I think this is weird because the code seems okay.
Can someone help me?
"checked" isn't an option btw.