📜  Java中的 Double isInfinite() 方法及示例

📅  最后修改于: 2022-05-13 01:54:28.055000             🧑  作者: Mango

Java中的 Double isInfinite() 方法及示例

Java Double 类的Java .lang.Double.isInfinite()方法是Java中的内置方法,如果此 Double 值或指定的 double 值的大小无限大,则返回 true,否则返回 false。

语法

public boolean isInfinite()
        or
public static boolean isInfinite(double val)

参数:该函数接受单个参数val ,该参数指定使用 Double 类作为静态方法直接调用时要检查的值。方法作为实例方法时不需要该参数。
返回值:如果 val 是无限的,则返回true ,否则返回false
下面的程序说明了Java中的isInfinite()方法:
方案一:

Java
// Java code to demonstrate
// Double isInfinite() method
// without parameter
 
class GFG {
    public static void main(String[] args)
    {
 
        // first example
        Double f1 = new Double(1.0 / 0.0);
 
        boolean res = f1.isInfinite();
 
        // printing the output
        if (res)
            System.out.println(f1 + " is infinity");
        else
            System.out.println(f1 + " is not infinity");
 
        // second example
        f1 = new Double(0.0 / 0.0);
 
        res = f1.isInfinite();
 
        // printing the output
        if (res)
            System.out.println(f1 + " is infinity");
        else
            System.out.println(f1 + " is not infinity");
    }
}


Java
// Java code to demonstrate
// Double isInfinite() method
// with parameter
 
class GFG {
    public static void main(String[] args)
    {
 
        // first example
        Double f1 = new Double(1.0 / 0.0);
 
        boolean res = f1.isInfinite(f1);
 
        // printing the output
        if (res)
            System.out.println(f1 + " is infinity");
        else
            System.out.println(f1 + " is not infinity");
 
        // second example
        f1 = new Double(0.0 / 0.0);
 
        res = f1.isInfinite(f1);
 
        // printing the output
        if (res)
            System.out.println(f1 + " is infinity");
        else
            System.out.println(f1 + " is not infinity");
    }
}


输出:
Infinity is infinity
NaN is not infinity

方案二:

Java

// Java code to demonstrate
// Double isInfinite() method
// with parameter
 
class GFG {
    public static void main(String[] args)
    {
 
        // first example
        Double f1 = new Double(1.0 / 0.0);
 
        boolean res = f1.isInfinite(f1);
 
        // printing the output
        if (res)
            System.out.println(f1 + " is infinity");
        else
            System.out.println(f1 + " is not infinity");
 
        // second example
        f1 = new Double(0.0 / 0.0);
 
        res = f1.isInfinite(f1);
 
        // printing the output
        if (res)
            System.out.println(f1 + " is infinity");
        else
            System.out.println(f1 + " is not infinity");
    }
}
输出:
Infinity is infinity
NaN is not infinity

参考: https: Java/lang/Double.html#isInfinite(double)