Attempt to prevent overflow by adding two lengths

I try to prevent overflow in two situations: one when multiplying a long by a float, and the other by adding two lengths.

how

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 fine, and "addLong" doesn't work as it should. When using addLong, and overflow occurs, it will not detect overflow. I think this is strange because the code looks fine.

Can someone help me?

"checked" is not a variant of btw.

+4
source share
3 answers

Like @Bradley, you get an overflow in your overflow check. But the problem is bigger.

Let's look at this line first

if(value + adder > long.MaxValue)

value adder , . , longs , , , , - , , .

, , , multiplyLong. ,

if (value * multiplier > long.MaxValue)

value , multiplier - float, , long, .

, value * multiplier , long.MaxValue long?

, long.MinValue, , , long.MaxValue

, , , .

, ? decimal . -, , long (decimal 79228162514264337593543950335, long 9223372036854775807), , .

,

public static long multiplyLong(decimal value, decimal multiplier)
{
    try
    {
        return value*multiplier;
    }
    catch (OverflowException e)
    {
        Debug.Log("greater then max value");
        return decimal.MaxValue;
    }

}

public static long addLong(decimal value, decimal adder)
{
    try
    {
        return value+adder;
    }
    catch (OverflowException e)
    {
        Debug.Log("greater then max value");
        return decimal.MaxValue;
    }
}
+3

value + adder ( long), .

-, , ..

if(value + (Decimal)adder > long.MaxValue)

Overflow , . , , - float, .

, , , Decimal add long; , .

+2

:

  • value adder
  • value adder

, :

  if ((value + adder < value && value + adder < adder) || 
      (value + adder > value && value + adder > adder)) {
    // Overflow
  }

, , ,

public static long addLong(long value, long adder) {
  unchecked { // we'll detect overflow manually
    if (value + adder < value && value + adder < adder) {
      Debug.Log("greater then max value");
      return long.MaxValue;
    }
    else if (value + adder > value && value + adder > adder) {
      Debug.Log("less then min value");
      return long.MinValue;
    }
    else {
      Debug.Log("within the [min..max] range");
      return value + adder;
    }
  }
}

:

addLong(2, 3);                   // within the [min..max] range
addLong(long.MaxValue - 3, 5);   // greater then max value
addLong(long.MinValue + 2, -5);  // less then min value
+1

Source: https://habr.com/ru/post/1664205/


All Articles