Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 393
Suma Avatar asked Jul 15 '15 20:07

Suma


People also ask

How do I know if my float is NaN?

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.

How do you know if a float is infinity?

To check if a Float is isInfinite, use the isInfinite() method and to check for NAN, use the isNaN() method.

Can a float be NaN?

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.


1 Answers

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
like image 116
Michael Zajac Avatar answered Oct 28 '22 19:10

Michael Zajac