诠释VS双和除以零异常异常、VS

由网友(无视沵的存在)分享简介:我们得到编译时错误整数除以零,而在案件的双重没有编译错误,但是在运行时,我们得到无穷/结果为NaN。任何想法,为什么INT和放大器;双有不同的行为,当谈到零异常分裂?无效的主要(){INT编号= 20;VAR RESULT1 =号/ 0; //除以零编译时异常双doubleNumber = 20;VAR结果2 = d...

我们得到编译时错误整数除以零,而在案件的双重没有编译错误,但是在运行时,我们得到无穷/结果为NaN。任何想法,为什么INT和放大器;双有不同的行为,当谈到零异常分裂?

 无效的主要()
{
    INT编号= 20;
    VAR RESULT1 =号/ 0; //除以零编译时异常

    双doubleNumber = 20;
    VAR结果2 = doubleNumber / 0.0; //没有编译时错误。结果是无穷大或NaN
}
 

解决方案 打击异常流量,看国双大数据和人工智能技术如何赋能智能营销

由于这是它的定义 。而用整数有对无穷大和NaN没有什么特别的价值,所以编译器将抛出一个错误,如果它可以在编译时发现的问题。

We get compile time error when integer is divided by zero whereas in case of double there is no compilation error but at run-time we get infinity/NaN as the result. Any idea why int & double have different behavior when it comes to divide by zero exception?

void Main()
{
    int number = 20;
    var result1 = number/0; // Divide by zero compile time exception

    double doubleNumber = 20;
    var result2 = doubleNumber/0.0; // no compile time error. Result is infinity or NaN
}

解决方案

Because that's how it's defined. Whereas with integers there are no special values for infinity and NaN, so the compiler throws an error if it can spot the problem at compile time.

阅读全文

相关推荐

最新文章