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

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

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

Float Class中的isInfinite()方法是Java中的内置方法,如果此 Float 值或指定的 float 值的大小无限大,则返回 true,否则返回 false。
句法:

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

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

Java
// Java code to demonstrate
// Float isInfinite() method
// without parameter
 
class GFG {
    public static void main(String[] args)
    {
 
        // first example
        Float f1 = new Float(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 Float(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
// Float isInfinite() method
// with parameter
 
class GFG {
    public static void main(String[] args)
    {
 
        // first example
        Float f1 = new Float(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 Float(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

程序 2:使用非静态 isFinity() 方法

Java

// Java code to demonstrate
// Float isInfinite() method
// with parameter
 
class GFG {
    public static void main(String[] args)
    {
 
        // first example
        Float f1 = new Float(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 Float(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/Float.html#isInfinite()