双d = 1 / 0.0对双d = 1/0 [英] double d=1/0.0 vs double d=1/0

查看:203
本文介绍了双d = 1 / 0.0对双d = 1/0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

double d=1/0.0;
    System.out.println(d);

它打印 Infinity ,但如果我们写 double d = 1/0; 并打印,我们会得到这个例外:线程mainjava.lang中的异常
.ArithmeticException:/ by零
在D.main(D.java:3)
为什么Java知道在一种情况下潜水为零是无穷大,但是对于int 0,它没有被定义?
在这两种情况下,d是双重的,在这两种情况下,结果是无穷大。

It prints Infinity , but if we will write double d=1/0; and print it we'll get this exception: Exception in thread "main" java.lang.ArithmeticException: / by zero at D.main(D.java:3) Why does Java know in one case that diving by zero is infinity but for the int 0 it is not defined? In both cases d is double and in both cases the result is infinity.

推荐答案

浮点数据类型具有保留的特殊值以表示无穷大,整数值不。

Floating point data types have a special value reserved to represent infinity, integer values do not.

在你的代码中 1/0 是一个整数除法,当然会失败。但是, 1 / 0.0 是一个浮点分区,因此导致 Infinity

In your code 1/0 is an integer division that, of course, fails. However, 1/0.0 is a floating point division and so results in Infinity.

这篇关于双d = 1 / 0.0对双d = 1/0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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