Skip to content

Instantly share code, notes, and snippets.

@deepak
Created September 12, 2016 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepak/96529b3c7cfa4899d925fc59c07f834d to your computer and use it in GitHub Desktop.
Save deepak/96529b3c7cfa4899d925fc59c07f834d to your computer and use it in GitHub Desktop.
elm // operator

http://guide.elm-lang.org/core_language.html says

Unlike JavaScript, Elm makes a distinction between integers and floating point numbers. Just like Python 3, there is both floating point division (/) and integer division (//).

> 9 / 2
4.5

> 9 // 2
4

The comparison to python is slightly confusing because:

  1. semantics are not the same. which is ok as these are different languages
  2. python supports the // operator on Floats while elm does not
> 18 / 7
2.5714285714285716 : Float
> 18 // 7
2 : Int
> 18.0 // 7.0
-- TYPE MISMATCH --------------------------------------------- repl-temp-000.elm

The left argument of (//) is causing a type mismatch.

3|   18.0 // 7.0
     ^^^^
(//) is expecting the left argument to be a:

    Int

But the left argument is:

    Float

Hint: Elm does not automatically convert between Ints and Floats. Use `toFloat`
and `round` to do specific conversions.
<http://package.elm-lang.org/packages/elm-lang/core/latest/Basics#toFloat>

in python:

>>> 18 / 7
2
>>> 18 // 7
2
>>> 18.0 / 7.0
2.5714285714285716
>>> 18.0 // 7.0
2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment