Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `NaN` a possible value for type `Int` in Elm?

Tags:

elm

I recently started learning Elm. When I saw the Int type I assumed it was something like Haskell's Int, which is (I think) a machine integer.

But I was surprised to see this:

> Result.withDefault 0 <| String.toInt "-"
NaN : Int

NaN is a floating point concept which seems like it shouldn't apply to integers. It seems like this might be leaking in from JS, which would imply that Int is represented as a JS Number.

Is that the case? Why was this design decision made, and where can I go to learn about it?

(Also, two minor questions:

  • Why isn't NaN a valid literal?
  • How do I check to ensure my Int value isn't a NaN? The function isNaN is of type Float -> Bool http://package.elm-lang.org/packages/elm-lang/core/latest/Basics#isNaN

)

like image 495
Eli Rose Avatar asked Jul 01 '17 14:07

Eli Rose


2 Answers

NaN isn't a valid Int value. It's a bug in the elm-lang/core package which has since been fixed on master, but hasn't been released yet.

like image 162
robertjlooby Avatar answered Oct 21 '22 11:10

robertjlooby


Answering second minor question (that's horrible it's still not outdated) NaN seems to be the only x such that x /= x

like image 32
radrow Avatar answered Oct 21 '22 12:10

radrow