Check if a double is infinite in Java

后端 未结 7 477
深忆病人
深忆病人 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:48

    Resurrecting a super old question because it came up in my Java class. So I'm sure you tried the try/catch like they all suggested, but you found, like me, that it didn't work with Double. The try/catch with ArithmeticException doesn't work on Double or Float since they return "Infinity" instead of returning an exception. (note, delted myold "answer" since it wasn't an answer).

    Piecing together a few different questions/answers on this, I came up with the following trials.

    public class trial
    {
      public static void main(String[] args)
      {
          double a;
         try {
              a = 4/0;
             System.out.println(" The answer is " +a);
          } catch(ArithmeticException e) {
         System.out.println(" You can't divide by zero. Please try again.");
          }
      }
    }
    

    The above code should give you the result "The answer is Infinity." At least it did for me since it is a double.

    With an int, it would not need the below if statement since it would throw an exception. But since it is a Double, the if statement below will cause it to throw an exception which the catch will... well... catch.

    public class trial
    {
    public static void main(String[] args)
      {
          double a;
              try {
            a = 4/0;
       // if statement to throw an AtithmeticException if ans equals infinity
          if(a == Double.POSITIVE_INFINITY){
              throw new ArithmeticException();
    
            }
            else{
    
              System.out.println(" The answer is " +a);
            }
    
      } catch(ArithmeticException e) {
         System.out.println(" You can't divide by zero. Please try again.");
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-10 10:54

    You can use Double.isInfinite(double)

    Here's double doc

    0 讨论(0)
  • 2020-12-10 10:57

    Please see if it equal to Double.POSITIVE_INFINITY

    double result;
    result = 4.0 / 0.0;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-10 10:59

    Jacek Kwiecień try this code

    double result;
    try{
        result=4.0/0.0;
    }catch(ArithmeticException e){
    
        System.out.println("Math error "+ e.getMessage())
    }
    

    `

    0 讨论(0)
  • 2020-12-10 11:04

    The above code produces

    ArithmeticException: / by zero
    

    You can catch this exception in a try/catch block.

    0 讨论(0)
提交回复
热议问题