How can I give from Integer to Fractional

Say I have the following description of a Haskell type:

divide_by_hundred :: Integer -> IO() divide_by_hundred n = print(n/100) 

Why am I trying to run this through ghc, I get:

 No instance for (Fractional Integer) arising from a use of `/' Possible fix: add an instance declaration for (Fractional Integer) In the first argument of `print', namely `(n / 100)' In the expression: print (n / 100) In an equation for `divide_by_hundred': divide_by_hundred n = print (n / 100) 

By running :t (/) I get:

 (/) :: Fractional a => a -> a -> a 

which, in my opinion, assumes that (/) can take any number, which can be expressed as a fractional (which I was impressed with, should include Integer, although I'm not sure how to check this) if both inputs / have same type.

This is clearly inaccurate. What for? And how would I write a simple function to divide the integer by 100?

+6
source share
2 answers

Haskell loves to adhere to the mathematically acceptable meaning of operators. / should be the opposite of multiplication, but, for example, 5 / 4 * 4 cannot give 5 for an instance of Fractional Integer 1 .

So, if you really want to do truncated integer division, the language forces you 2 to make it explicit with a div or quot . OTOH, if you really want to get the result as a fraction, you can use / fine, but first you need to convert it to a type with a Fractional instance. For instance,

Prelude> let x = 5
Prelude>: tx
x :: Integer
Prelude> let y = fromIntegral x / 100
Prelude> y
5.0E-2
Prelude>: ty
y :: Double

Note that GHCi chose the Double instance here because it is just the default; you can also do

Prelude> let y '= fromIntegral x / 100 :: Rational
Prelude> y '
1% 20


1 Strictly speaking, this inverse identity is not entirely true for the Double instance, either because of floating point smoothing, but there it is true at least approximately.

2 Actually, not a language, but standard libraries. You can define

 instance Fractional Integer where (/) = div 

then your source code will work fine. Only, this is a bad idea!

+11
source

You can use div for integer division:

 div :: Integral a => a -> a -> a 

Or you can convert integers to fractional parts using fromIntegral :

 fromIntegral :: (Integral a, Num b) => a -> b 

So essentially:

 divide_by_hundred :: Integer -> IO() divide_by_hundred n = print $ fromIntegral n / 100 

Entire functions do not implement Fractional, which you can see in the manual .

+6
source

All Articles