intToFloat :: Int -> Float
intToFloat n = fromInteger (toInteger n)
pixelToInch :: Int -> Float
pixelToInch n = intToFloat n / 100
{-
pixelToInch' :: Int -> Float
pixelToInch' n = intToFloat (n / 100)
The definition of pixelToInch' will fail with error:
Instance of Fractional Int required for definition of pixelToInch'
Other languages have kind of "integer division",
for example 7/2 = 3 (the result is always integer)
So, this will have the same problem as ex.4.1: "lost precision".
BTW. Tried this:
x :: Int
x = 20
y :: Int
y = 100
:t (x/y)
ERROR - Cannot infer instance
*** Instance : Fractional Int
*** Expression : x / y
hmm....
:t (/)
(/) :: Fractional a => a -> a -> a
I will study Haskell's numeric type classes later to understand what happend.
-}
This entry was posted
on Tuesday, May 15th, 2007 at 5:26 pm and is filed under Haskell - SOE.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.