Check if a double is infinite in Java

后端 未结 7 498
深忆病人
深忆病人 2020-12-10 10:22

I\'m making a simple calculator for this homework, and Java is returning \"Infinity\" when divided by 0.

I need to display some error message, when I get infinity. T

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 10:59

    There are two fields for infinity in the Double class: POSITIVE_INFINITY and NEGATIVE_INFINITY which you can check for.

    Note that integer division by zero would throw an ArithmeticException thus your line would have to be 4.0/0, 4/0.0 or 4.0/0.0 since 4 and 0 are integers and thus result in integer math.

提交回复
热议问题