Division by zero: int vs. float

后端 未结 6 435
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 05:18

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?

6条回答
  •  伪装坚强ぢ
    2021-01-16 05:44

    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.

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

    • The range of an integer type is as large as possible - no bit patterns are wasted.
    • The representation of an integer is easy to understand because there are no special cases.
    • Integer arithmetic can be done at extremely high speed even on very simple processors.

提交回复
热议问题