Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Elm, why is this an Int-Float type mismatch?

Tags:

elm

I am new to elm, and functional programming in general. I was getting a puzzling type mismatch when doing division with a call to 'show'. This code produces the mismatch:

import Graphics.Element exposing (..)

columns = 2
main = placePiece 10

placePiece: Int -> Element
placePiece index =
  show (index/columns)

The code produces this error:

Type mismatch between the following types on line 9, column 3 to 22:

  Int

  Float

It is related to the following expression:

  show (index / columns)

Which I read to mean that it expects and Int, but got a Float. But show works with any type. If I use floor to force the division into an Int, I get the same error. But, if I hard code the numbers, e.g. show (10/2) It works fine.

So what part of the code above is expecting to get an Int?

like image 808
Tony Avatar asked Jun 11 '15 01:06

Tony


People also ask

What happens if we compare int and float?

Casting the int to a float explicitly will do absolutely nothing. The int will be promoted to a float for purposes of comparison anyway.

Why does using float instead of int give me different results when all of my inputs are integers?

The reason you get different output is you perform a different computation: division behaves differently for integers and floating point numbers, except when the numerator is a multiple of the denominator.

Are int and float interchangeable?

Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.

What is a limitation of ints compared to floats?

As you probably know, both of these types are 32-bits. int can hold only integer numbers, whereas float also supports floating point numbers (as the type names suggest).


1 Answers

Reason for the error

Actually in this case the compiler is expecting a Float but getting an Int. The Int is the argument index of the placePiece function, and it expects a Float because Basics.(/) expects Float arguments.

Why literal numbers work

When you just hard code numbers, the compiler can figure out that although you're using whole numbers, you may want to use them as Float instead of Int.

Fixing the error

There are three ways to fix this error. If you really want to accept an Int but want floating point division, you'll have to turn the integer into a floating point number:

import Graphics.Element exposing (..)

columns = 2
main = placePiece 10

placePiece: Int -> Element
placePiece index =
  show (toFloat index / columns)

If you're ok with the placePiece function taking a floating point number you can change the type signature:

import Graphics.Element exposing (..)

columns = 2
main = placePiece 10

placePiece: Float -> Element
placePiece index =
  show (index/columns)

If you wanted integer division, you can use the Basics.(//) operator:

import Graphics.Element exposing (..)

columns = 2
main = placePiece 10

placePiece: Int -> Element
placePiece index =
  show (index//columns)
like image 55
Apanatshka Avatar answered Oct 20 '22 23:10

Apanatshka