Why does integer division by zero 1/0 give error but floating point 1/0.0 returns “Inf”?

前端 未结 5 768
别跟我提以往
别跟我提以往 2020-11-29 10:12

I\'m just curious about this:

When evaluating 1/0 in Java, the following exception occurs:

Exception in thread \"main\" java.la

5条回答
  •  庸人自扰
    2020-11-29 10:52

    1/0 is a division of two ints, and throws an exception because you can't divide by integer zero. However, 0.0 is a literal of type double, and Java will use a floating-point division. The IEEE floating-point specification has special values for dividing by zero (among other thing), one of these is double.Infinity.

    If you're interested in details, the floating-point spec (which is often cryptic) has a page at Wikipedia: http://en.wikipedia.org/wiki/IEEE_754-2008, and its full text can be also read online: http://ieeexplore.ieee.org/xpl/mostRecentIssue.jsp?punumber=4610933.

提交回复
热议问题