Discussion:
[Haskell-cafe] Dividing integers?
Matias Hernandez
2004-03-18 13:55:53 UTC
Permalink
Hello there!

Although you might not believe it since I'm going to ask a very very very basic question, I have been using Haskell for educational purposes for a couple semesters now. Nevertheless, I don't think I have ever worked with non-Int number types! :)

I don't understand why I get this error. I mean, apparently "/" is not defined for integers but I don't know how to "cast" the result of the length function into a Double...

Prelude> (length [1,2]) / 3
<interactive>:1:
No instance for (Fractional Int)
arising from use of `/' at <interactive>:1
In the definition of `it': it = (length [1, 2]) / 3

Besides, a simple integer division works:

Prelude> 2 / 3
0.6666666666666666

But I would guess that's because '2' and '3' are parsed as Floats because they are used in a divide function.

Any help will be appreciated!

Best regards,
- Matías
Keith Wansbrough
2004-03-18 14:05:23 UTC
Permalink
Post by Matias Hernandez
Hello there!
Although you might not believe it since I'm going to ask a very very
very basic question, I have been using Haskell for educational
purposes for a couple semesters now. Nevertheless, I don't think I
have ever worked with non-Int number types! :)
I don't understand why I get this error. I mean, apparently "/" is
not defined for integers but I don't know how to "cast" the result
of the length function into a Double...
fromInteger or fromIntegral. (the former converts an Integer to any
numeric type, the latter converts any integral type to any numeric type).

--KW 8-)
Arjan van IJzendoorn
2004-03-18 14:07:11 UTC
Permalink
Post by Matias Hernandez
I don't understand why I get this error. I mean, apparently "/" is not
defined for integers but I don't know
Post by Matias Hernandez
how to "cast" the result of the length function into a Double...
Use div for dividing integers with loss of precision:
3 `div` 2 (or: div 3 2) == 1

"Casting" an Int to a Float/Double can be done using fromIntegral:

fromIntegral (length [1,2]) / 3 == 0.6666666666666666
Post by Matias Hernandez
Prelude> 2 / 3
0.6666666666666666
This is not an integer division. Writing down a literal (like 2 or 3)
denotes a number, not necessarily an Int. The context will influence what
type it will be. Something I find really confusing myself, too (which is why
it is not in Helium :-).

Greetings, Arjan
Ferenc Wagner
2004-03-18 14:08:41 UTC
Permalink
Post by Matias Hernandez
I mean, apparently "/" is not defined for integers but I
don't know how to "cast" the result of the length function
into a Double...
Prelude> (length [1,2]) / 3
Prelude> fromIntegral (length [1,2])/3
0.666666666666667
Post by Matias Hernandez
Prelude> 2 / 3
0.6666666666666666
Literal 2 means fromIntegral 2, but length::[a]->Int.
--
Feri.
Jon Fairbairn
2004-03-18 15:21:26 UTC
Permalink
Post by Ferenc Wagner
Post by Matias Hernandez
I mean, apparently "/" is not defined for integers but I
don't know how to "cast" the result of the length function
into a Double...
Prelude> (length [1,2]) / 3
Literal 2 means fromIntegral 2, but length::[a]->Int.
Note also

Data.List.genericLength :: Num a => [b] -> a
--
Jón Fairbairn ***@cl.cam.ac.uk
Loading...