Division by zero: int vs. float

后端 未结 6 497
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  猫巷女王i
    2021-01-16 05:44

    Basically, it's a purely arbitrary decision.

    The traditional int tries to use all the bits for representing possible numbers, whereas IEEE 754 standard reserves a special value for NaN.

    The standard could be changed for ints to include special values, at a cost of less efficient operations. The developers usually expect int operations to be very efficient, whereas the operations with floating point numbers are (purely psychologically) more allowed to be slower.

提交回复
热议问题