诠释VS双和除以零异常 [英] Int vs Double and divide by zero exception

查看:130
本文介绍了诠释VS双和除以零异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们得到编译时错误整数除以零,而在案件的双重没有编译错误,但是在运行时,我们得到无穷/结果为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.

这篇关于诠释VS双和除以零异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆