当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Float isInfinite()用法及代码示例


Float类中的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 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"); 
    } 
}
输出:
Infinity is infinity
NaN is not infinity

示例2:使用非静态isFinity()方法

// 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://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#isInfinite()



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Float isInfinite() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。