Why does integer division by zero 1/0 give error but floating point 1/0.0 returns quot;Infquot;?(为什么整数除以零 1/0 会出错,但浮点数 1/0.0 返回“Inf?)
问题描述
我只是对此感到好奇:
在 Java 中计算 1/0 时,会出现以下异常:
When evaluating 1/0 in Java, the following exception occurs:
线程main"中的异常java.lang.ArithmeticException:/by零在 Foo.main(Foo.java:3)
Exception in thread "main" java.lang.ArithmeticException: / by zero at Foo.main(Foo.java:3)
但是 1/0.0 被评估为 Infinity.
public class Foo {
public static void main (String[] args) {
System.out.println(1/0.0);
}
}
为什么会这样?
推荐答案
这是因为整数没有 +/-Inf、NaN 的值,并且不允许除以 0,而浮点数却有这些特殊值.
That's because integers don't have values for +/-Inf, NaN, and don't allow division by 0, while floats do have those special values.
这篇关于为什么整数除以零 1/0 会出错,但浮点数 1/0.0 返回“Inf"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么整数除以零 1/0 会出错,但浮点数 1/0.0 返回“Inf"?
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
