How does `isFinite()` work?

0106

The isFinite() method in Java is used to determine whether a given double value is finite. It returns a boolean value:

  • Returns true if the value is a finite number (i.e., not NaN, positive infinity, or negative infinity).
  • Returns false if the value is NaN (Not a Number) or either positive or negative infinity.

Here's a simple example of how it works:

double d1 = 100.0;
double d2 = Double.POSITIVE_INFINITY;
double d3 = Double.NEGATIVE_INFINITY;
double d4 = Double.NaN;

boolean b1 = Double.isFinite(d1); // true
boolean b2 = Double.isFinite(d2); // false
boolean b3 = Double.isFinite(d3); // false
boolean b4 = Double.isFinite(d4); // false

System.out.println("Is d1 finite? " + b1); // Output: Is d1 finite? true
System.out.println("Is d2 finite? " + b2); // Output: Is d2 finite? false
System.out.println("Is d3 finite? " + b3); // Output: Is d3 finite? false
System.out.println("Is d4 finite? " + b4); // Output: Is d4 finite? false

In this example, isFinite() checks each variable and returns the appropriate boolean value based on whether the value is finite or not.

0 Comments

no data
Be the first to share your comment!