除数为零:INT与浮动 [英] Division by zero: int vs. float

查看:204
本文介绍了除数为零:INT与浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被零除一个int,将抛出一个异常,而是一个浮动不会 - 至少在Java中。为什么一个浮动有额外的NaN的信息,而int类型不?

Dividing an int by zero, will throw an exception, but a float won't - at least in Java. Why does a float have additional NaN info, while an int type doesn't?

推荐答案

浮点数的表示已设计这样,有保留的存储如NaN,无穷大,等等特殊值数位的一些特殊的组合。

The representation of a float has been designed such that there are some special combination of bits reserved to store special values such as NaN, infinity, etc.

有一个int型的未使用的作品 - 每一个位模式对应于一个整数。这样做有很多优点:

There are no unused representations for an int type - every bit pattern corresponds to an integer. This has many advantages:


  • 整数类型的范围尽可能大 - 无位模式被浪费

  • 整数的表示是很容易理解,因为没有特殊情况。

  • 整数运算,可以在极高的速度甚至做得非常简单的处理器。

这篇关于除数为零:INT与浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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