Infinite and Not A Number

Using the following methods you can find out if a float value is infinite value or it is not a number(NaN).

ReturnMethodSummary
booleanisInfinite()Returns true if this Float value is infinitely large in magnitude, false otherwise.
static booleanisInfinite(float v)Returns true if the specified number is infinitely large in magnitude, false otherwise.
booleanisNaN()Returns true if this Float value is a Not-a-Number (NaN), false otherwise.
static booleanisNaN(float v)Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

public class Main {
  public static void main(String[] args) {
    Float floatObject2 = Float.valueOf((float)0.0/(float)(0.0));
    System.out.println(floatObject2);
    System.out.println("isInfinite:"+floatObject2.isInfinite());
    System.out.println("isNaN:"+floatObject2.isNaN());
    
  }
}

The output:


NaN
isInfinite:false
isNaN:true

public class Main {
  public static void main(String[] args) {
    Float floatObject2 = Float.valueOf(1/(float)(0.0));
    System.out.println(floatObject2);
    System.out.println("isInfinite:"+floatObject2.isInfinite());
    System.out.println("isNaN:"+floatObject2.isNaN());
    
  }
}

The output:


Infinity
isInfinite:true
isNaN:false
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.