Home > other >  How to check if double is NEGATIVE_INFINITY in Java
How to check if double is NEGATIVE_INFINITY in Java

Time:05-07

How do I check if double is NEGATIVE_INFINITY and not POSITIVE_INFINITY? There is only one method in Double class isInfinite() which checks whether the double is infinite or not but it returns true in both cases if it's positive and if it's negative. So is there another way to check the specific infinity (negative or positive)?

CodePudding user response:

Here are two possible ways to check if a double x specifically holds negative infinity:

if (Double.isInfinite(x) && x < 0) ...

or simply

if (x==Double.NEGATIVE_INFINITY) ...
  • Related