Suma
Suma

Reputation: 34403

How to test if a Float or Double is infinite or NaN?

In Java there is an API to test if a number is infinite or NaN.

I cannot find anything like this in Scala, and to call the Java functions it seems I need to box the value or to call java.lang.Double static method:

Double.box(x).isNaN

java.lang.Double.isNaN(x)

Is there really nothing more "native" to Scala to test for infiniteness / NaN-ness?

Upvotes: 8

Views: 7008

Answers (1)

Michael Zajac
Michael Zajac

Reputation: 55569

These are methods on the boxed scala.Double. No need to manually box them.

scala> 1.2.isNaN
res1: Boolean = false

scala> 1.2.isInfinity
res2: Boolean = false

scala> (0.0 / 0.0).isNaN
res8: Boolean = true

scala> (1.0 / 0.0).isInfinity
res5: Boolean = true

Upvotes: 16

Related Questions