2

Is there a way in C# to use maxvalue when an int would be out of range. For my purposes I can't use a long.

Example

int a = 842673832;
int b = 2131231321;

int t = a * b;
//if t out of range, t = Int32.MinValue;
Dawson
  • 573
  • 6
  • 24
  • Could you explain what restrictions are in place? As in, what are your "purposes"? – David Pilkington May 19 '16 at 06:35
  • I'm not a C# programmer but I think in this case you could probably use a conditional with your logic in it. If you wanted it to be compact you could use a ternary operator. – jeteon May 19 '16 at 06:42
  • I'm working with a legacy system and cannot make larger changes to the system. – Dawson May 19 '16 at 06:43

3 Answers3

3

You would need to test it before hand or create a very special kind of integer (like CheckedInt in the link).

Option A

To test it before hand, you need a data type which can handle big integer value (say, long):

long t = (long)a * b;
int r = t > int.MaxValue ? int.MaxValue : t < int.MinValue ? : int.MinValue : (int)t;

Option B

To use the special kind of integer, you may need to overload the basic operators too.

In the example in the link, the exception is thrown, but you could change the exception into implementing int.MaxValue or int.MinValue, something like this:

public struct CheckedInt {
    private int Value { get; set; }
    public CheckedInt(int value)
        : this() {
        Value = value;
    }

    public static implicit operator CheckedInt(int me) {
        return new CheckedInt(me);
    }

    public static CheckedInt operator +(CheckedInt lhs, CheckedInt rhs) {
        double testResult = (double)lhs.Value + (double)rhs.Value;
        if (testResult > int.MaxValue)
            return int.MaxValue;
        if (testResult < int.MinValue)
            return int.MinValue; 
        return new CheckedInt(lhs.Value + rhs.Value); //note that direct lhs+rhs will cause StackOverflow
    }

    public static CheckedInt operator -(CheckedInt lhs, CheckedInt rhs) {
        double testResult = (double)lhs.Value - (double)rhs.Value;
        if (testResult > int.MaxValue)
            return int.MaxValue;
        if (testResult < int.MinValue)
            return int.MinValue; 
        return new CheckedInt(lhs.Value - rhs.Value); //note that direct lhs-rhs will cause StackOverflow
    }

    public static CheckedInt operator *(CheckedInt lhs, CheckedInt rhs) {
        double testResult = (double)lhs.Value * (double)rhs.Value;
        if (testResult > int.MaxValue)
            return int.MaxValue;
        if (testResult < int.MinValue)
            return int.MinValue; 
        return new CheckedInt(lhs.Value * rhs.Value); //note that direct lhs*rhs will cause StackOverflow
    }

    public static CheckedInt operator /(CheckedInt lhs, CheckedInt rhs) {
        double testResult = (double)lhs.Value / (double)rhs.Value;
        if (testResult > int.MaxValue)
            return int.MaxValue;
        if (testResult < int.MinValue)
            return int.MinValue; 
        return new CheckedInt(lhs.Value / rhs.Value); //note that direct lhs-rhs will cause StackOverflow
    }

    //Add any other overload that you want

    public override string ToString() { //example
        return Value.ToString();
    }

    public bool Equals(CheckedInt otherInt) { //example
        return Value == otherInt.Value;
    }
}
Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
2

You can try this by enabling overflow checking

int t;

try
{
   int a = 842673832;
   int b = 2131231321;

   t = checked(a * b);
}
catch (System.OverflowException e)
{
   t = Int32.MaxValue;
}
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
-1

If you want t to be an int and not a larger type like Ian's answer, you can catch at OverflowException and set t to MaxValue. Make sure to wrap the section in a checked keyword to allow overflow checking.

int t = 0;
checked {
    try 
    {
        int a = 842673832;
        int b = 2131231321;

        t = a * b;
    }
    catch (OverflowException) 
    {
        t = int.MaxValue;
    }
}
Steve
  • 9,335
  • 10
  • 49
  • 81