【问题标题】:Check if a double is infinite in Java检查Java中的双精度数是否无限
【发布时间】:2011-11-29 13:34:24
【问题描述】:

我正在为这个作业制作一个简单的计算器,Java 在除以 0 时返回“Infinity”。

当我得到无穷大时,我需要显示一些错误消息。问题是我不知道如何处理这个条件

double result;
result = 4/0;
//if result == infinity then some message - need help with this

【问题讨论】:

  • catch ArithmeticException 检查除数是否为零。显示错误信息。
  • 您是否有理由想要获得无穷大而不是一开始就停止除法?
  • 是的,删除了我的评论,虽然它在数学上并不正确
  • 不过,这是两个独立的问题:处理无穷大和处理除以零(如果操作数是双精度数,这不是错误)
  • 4.0/0.0 => Infinity, 4/0 是整数运算,会产生异常。

标签: java


【解决方案1】:

你可以使用Double.isInfinite(double)

这里是double doc

【讨论】:

    【解决方案2】:

    以上代码产生

    ArithmeticException: / by zero
    

    您可以在 try/catch 块中捕获此异常。

    【讨论】:

      【解决方案3】:

      Double 类中有两个无穷大字段:POSITIVE_INFINITYNEGATIVE_INFINITY,您可以检查它们。

      请注意,整数除以零会抛出 ArithmeticException,因此您的行必须是 4.0/04/0.04.0/0.0,因为 4 和 0 是整数,因此会产生整数数学运算。

      【讨论】:

        【解决方案4】:

        请看是否等于 Double.POSITIVE_INFINITY

        double result;
        result = 4.0 / 0.0;
        

        【讨论】:

        • 我相信,那是他不想要的。他想看到一个错误。
        【解决方案5】:

        复活一个超级老问题,因为它出现在我的 Java 课程中。所以我确定你尝试了他们都建议的 try/catch,但你发现像我一样,它不适用于Double。带有 ArithmeticException 的 try/catch 不适用于 DoubleFloat,因为它们返回“Infinity”而不是返回异常。 (注意,删除了旧的“答案”,因为它不是答案)。

        将几个不同的问题/答案拼凑在一起,我想出了以下试验。

        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.");
              }
          }
        }
        

        上面的代码应该给你结果“答案是无穷大”。至少它对我有用,因为它是double

        使用int,它不需要下面的if 语句,因为它会抛出异常。但由于它是一个Double,下面的if 语句将导致它抛出一个异常,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.");
            }
          }
        }
        

        【讨论】:

          【解决方案6】:

          Jacek Kwiecień 试试这个代码

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

          `

          【讨论】:

            【解决方案7】:

            这种错误称为异常。您可以使用 try-catch 块来捕获此异常。

             try{
                  result = 4/0;
             }
             catch(ArithmeticException e){
                 System.out.println("You divided by zero");
             }
            

            您可以阅读有关异常处理的信息here

            【讨论】:

            • 如果您只是在寻找专门的异常,请不要捕获一般异常。
            • 更好的是,当一个简单的测试会做得更好时,不要捕获异常。
            猜你喜欢
            • 2015-04-11
            • 2020-06-02
            • 1970-01-01
            • 2011-11-09
            • 2021-01-30
            • 1970-01-01
            • 2010-12-30
            • 2010-10-08
            相关资源
            最近更新 更多