如果结果对于int太大,请使用maxvalue

时间:2016-05-19 06:33:07

标签: c# int

当int超出范围时,C#中有没有办法使用maxvalue? 出于我的目的,我不能长时间使用。

实施例

Jose da Silva #1
Jose da Silva #2
Fulano de Tal
Jose da Silva #3
Sicrano Pereira #1
Ze Ruela
Sicrano Pereira #2
Jose da Silva #4

3 个答案:

答案 0 :(得分:3)

您需要事先对其进行测试或创建very special kind of integer(如链接中的CheckedInt)。

选项A

要预先测试它,你需要一个可以处理大整数值的数据类型(比如long):

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

选项B

要使用特殊类型的整数,您可能还需要重载基本运算符。

在链接的示例中,抛出了异常,但您可以将异常更改为实现int.MaxValueint.MinValue,如下所示:

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;
    }
}

答案 1 :(得分:2)

您可以通过启用overflow checking

来尝试此操作
int t;

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

   t = checked(a * b);
}
catch (System.OverflowException e)
{
   t = Int32.MaxValue;
}

答案 2 :(得分:-1)

如果您希望t成为int,而不是像Ian的答案那样更大的类型,则可以在OverflowException抓住并将t设置为MaxValue。确保将该部分包含在checked关键字中以允许溢出检查。

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

        t = a * b;
    }
    catch (OverflowException) 
    {
        t = int.MaxValue;
    }
}