Class 合同。确保发生溢出异常

Class 合同。确保发生溢出异常,class,pex,contract,overflowexception,Class,Pex,Contract,Overflowexception,我有一个简单的方法,从给定的数字返回指数值: public int Exp(int num) { return Convert.ToInt32(System.Math.Exp(num)); } 当运行Pex时,我在摘要/异常字段中得到一个OverFlowException,该字段中有一个很大的数字:1969057606 如何使用Contract.sure()创建Post条件? 我尝试了以下操作,但没有任何效果: Contract.Ensures(Cont

我有一个简单的方法,从给定的数字返回指数值:

    public int Exp(int num)
    {
        return Convert.ToInt32(System.Math.Exp(num));
    }
当运行Pex时,我在摘要/异常字段中得到一个OverFlowException,该字段中有一个很大的数字:1969057606

如何使用
Contract.sure()
创建Post条件? 我尝试了以下操作,但没有任何效果:

Contract.Ensures(Contract.Result<int>() < 2147483647);

// This is because the max value an int variable can hold is 2147483647
Contract.sure(Contract.Result()<2147483647);
//这是因为int变量可以容纳的最大值是2147483647

Contract.sure用于在函数运行后断言类的状态,或者在本例中,不管输入是什么,都断言函数的结果。您需要添加一个合同。要求使用e^num而不是实际的数字,这样可以让查看代码的人更容易阅读和分析。非常感谢Gabriele。这是真的!我感谢你的帮助!布莱恩,非常感谢你的帮助!你的解释帮助我解决了主要问题。再次感谢!
Contract.Requires<ArgumentOutOfRangeException>(num <= Math.Floor(Math.Log(int.MaxValue)))
public static readonly int MAX_EXPONENT = Math.Floor(Math.Log(int.MaxValue));
...
Contract.Requires<ArgumentOutOfRangeException>(num <= MAX_EXPONENT)