kabb
kabb

Reputation: 2502

Comprehensive list of ways to generate NaN in Java?

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.

Upvotes: 0

Views: 285

Answers (2)

Charles Goodwin
Charles Goodwin

Reputation: 6652

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
  • ∞ + (−∞)
  • (−∞) + ∞

Upvotes: 3

PlasmaPower
PlasmaPower

Reputation: 1878

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.

Upvotes: 0

Related Questions