Exercise 4.2


From the text:

intToFloat :: Int -> Float
intToFloat i = fromInteger (toInteger i)

The exercise:

pixelToInch :: Int -> Float
pixelToInch x = intToFloat x / 100

{-
pixelToInchBad :: Int -> Float
pixelToInchBad x = intToFloat (x / 100)
-}

pixelToInchBad :: Int -> Float
pixelToInchBad x = intToFloat (x `div` 100)

Apart from anything else, the commented out version of pixelToInchBad gives an error, viz.: Instance of Fractional Int required for definition of pixelToInchBad. Assuming the second version is what the book is hinting at, this has the same issues with loss of precision that we saw in Exercise 4.1.

Edit: this is indeed what the book should properly say (see erratum 4).


Leave a Reply

Your email address will not be published. Required fields are marked *