kotlindoublenaninfinitydefensive-programming

Kotlin: Condition that double-value is a "normal" number


How can I test if a value of type Double in Kotlin is not Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN or some other special value?

I'd like to have something like require(Double.isNormal(x))


Solution

  • https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/is-finite.html

    require(x.isFinite()) is what you need.