I've been facing a problem with NaN causing exceptions in a project I'm working on. I was wondering if somebody could provide a list of all of the possible ways an NaN might surface in Java, so I could know all of the possible things to look for while tracking it down.
Asked
Active
Viewed 271 times
0
-
You can post your code! We can work on the issue and it will be a lot easier for everyone to help you out in the best way. – Blue Ice Feb 28 '14 at 02:28
-
I have a fairly large code base, and I'm honestly not sure what code is relevant. – kabb Feb 28 '14 at 02:29
-
Possible duplicate of [When can Java produce a NaN?](http://stackoverflow.com/questions/2887131/when-can-java-produce-a-nan) – sigy Jan 31 '17 at 08:58
2 Answers
3
Source: https://stackoverflow.com/a/2887161/546060
NaN
is triggered by the following occurrences:
- results that are complex values
- √x where x is negative
- log(x) where x is negative
- tan(x) where x mod 180 is 90
- asin(x) or acos(x) where x is outside [-1..1]
- 0/0
- ∞/∞
- ∞/−∞
- −∞/∞
- −∞/−∞
- 0×∞
- 0×−∞
- 1∞
- ∞ + (−∞)
- (−∞) + ∞

Community
- 1
- 1

Charles Goodwin
- 6,402
- 3
- 34
- 63
-
Surprised I couldn't find that thread when I was trying to figure this out earlier. Thanks, that should (hopefully) help locate the problem. – kabb Feb 28 '14 at 02:39
0
Here are some:
Double.NaN + Double.NaN -> NaN
Float.NaN + 2.0 -> NaN
Float.NaN * 3.0 -> NaN
(0.0 / 0.0) * Float.POSITIVE_INFINITY -> NaN
Math.abs(0.0 / 0.0) -> NaN
Taken from here.

PlasmaPower
- 1,864
- 15
- 18