3

Now and then my code calculates a double and returns Infinite. The code does not get stuck and I can even see these results in a .csv file that is saved by the code (the word "Infinite" is written in the .csv file).

But I want to avoid my code to further use this infinite result, so I have to detect wether the result is infinite or not. How can I do this ?

I tried something like

if(a==Infinite){    }

or

if(a.isInfinite()){    }

but none of these seem to be proper code.

Veronique
  • 381
  • 1
  • 3
  • 13
  • @Tunaki The question you are referring to is not about infinity, but about a double with an infinite number of digits after the comma (something like 4/3 = 1.333333). I found that question but it did not solve mine. – Veronique Feb 22 '17 at 10:45
  • No it's not, it's talking about a double value being infinite, to which the answer is to use `Double.isInfinite(double)`, like accepted here. There is no mention of a double value with an infinite decimal representation there. – Tunaki Feb 22 '17 at 10:52
  • ok sorry, now it is. Either I'm turning mad or you changed the link. – Veronique Feb 22 '17 at 10:56
  • I didn't change the link, no (you have [the history here](http://stackoverflow.com/posts/42387417/revisions)). Misclick happens :)! – Tunaki Feb 22 '17 at 11:00

2 Answers2

10

Read the Javadoc:

public static boolean isInfinite(double v)

Returns true if the specified number is infinitely large in magnitude, false otherwise.

So, something like:

if (Double.isInfinite(myValue)) { ... }
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 1
    OP is asking whether the number is infinite, not whether the number is not finite. I've commented on your answer to that effect. – Andy Turner Feb 22 '17 at 09:30
2

If you have a primitive double then use

a == Double.POSITIVE_INFINITY || a == Double.NEGATIVE_INFINITY

as the test for infinity. Note that if your infinity is a result of floating point division by zero then you should also check for

Double.NAN

which will be the result of 0.0 / 0.0. Unfortunately everything compares false with NaN (including NaN itself), so you need to use Double.isNaN(a) for that.


Then bin the whole thing and use Double.isFinite(a), which will be false for +Infinity, -Infinity, and NaN, and true in all other cases.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I'm not sure whether your handling of `NaN` is necessary here. OP hasn't said that it is; whether that is because they've not considered the case, or actually don't want to handle it, is unclear. – Andy Turner Feb 22 '17 at 09:31
  • I'm just trying to be helpful ;-) – Bathsheba Feb 22 '17 at 09:31
  • And I think you are being; I'm just saying that you're perhaps adding your own requirements here. Plus, there's no point in using explicit comparison over `Double.isInfinite`. – Andy Turner Feb 22 '17 at 09:32
  • I put that in just to get the quiche eaters who think you can't use `==` in floating point to have a ponder. But I agree with you that using functions is superior. Particularly in C++ where the specification of floating point types is much looser. – Bathsheba Feb 22 '17 at 09:35
  • "But this answer still remains about as useful as a trapdoor in a canoe." Great simile. – Andy Turner Feb 22 '17 at 09:47
  • I've added both Double.isInfinite(myValue) and Double.isNaN(a) to my code, just to be on the safe side, although the code never blocked on NaN so far. Your suggestion to use Double.isFinite seems to be even better (1 test combines the other 2) but this method is unknown in my code : what do I have to import to use Double.isFinite ? – Veronique Feb 22 '17 at 09:50
  • I'd use `!Double.isFinite(a)` if I were you as it covers all bases. – Bathsheba Feb 22 '17 at 09:53
  • @Veronique If you check the Javadoc link in this answer, it shows it was added in "1.8" (which is Java 8). If you're using an earlier version of Java, you have to stick with the two checks. – Andy Turner Feb 22 '17 at 09:53
  • @Andy I installed the latest version, uninstalled the previous, and rebooted, but this didn't help : "cannot find symbol : method isFinite(Double)" ... – Veronique Feb 22 '17 at 10:41
  • 1
    Did you set the source version in your IDE to version 8? – Bathsheba Feb 22 '17 at 10:57
  • I can't find anything about "source version" in the Netbeans IDE 7.3.1 help section ... – Veronique Feb 22 '17 at 11:03