Java Double isInfinite() and isNaN()

Introduction

Float and Double provide the methods isInfinite() and isNaN().

There are two unique value defined by the IEEE floating-point specification: infinity and NaN (not a number).

isInfinite() returns true if the value being tested is infinitely large or small in magnitude.

isNaN() returns true if the value being tested is not a number.

// Demonstrate isInfinite() and isNaN().
public class Main {
  public static void main(String args[]) {
    Double d1 = new Double(1/0.);
    Double d2 = new Double(0/0.);

    System.out.println(d1 + ": " + d1.isInfinite() + ", " + d1.isNaN());
    System.out.println(d2 + ": " + d2.isInfinite() + ", " + d2.isNaN());
  }/*from ww w.  ja  v a 2  s.c  o m*/
}



PreviousNext

Related