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?
To check whether a floating point or double number is NaN (Not a Number) in C++, we can use the isnan() function. The isnan() function is present into the cmath library. This function is introduced in C++ version 11.
To check if a Float is isInfinite, use the isInfinite() method and to check for NAN, use the isNaN() method.
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With