September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,21 +1,28 @@
|
|||
import Data.Ratio
|
||||
import Text.Printf
|
||||
import Text.Printf (PrintfType, printf)
|
||||
|
||||
-- Map a value from the range [a1,a2] to the range [b1,b2]. We don't check
|
||||
-- for empty ranges.
|
||||
mapRange :: (Fractional a) => (a, a) -> (a, a) -> a -> a
|
||||
mapRange (a1,a2) (b1,b2) s = b1+(s-a1)*(b2-b1)/(a2-a1)
|
||||
mapRange
|
||||
:: Fractional a
|
||||
=> (a, a) -> (a, a) -> a -> a
|
||||
mapRange (a1, a2) (b1, b2) s = b1 + (s - a1) * (b2 - b1) / (a2 - a1)
|
||||
|
||||
main = do
|
||||
-- Perform the mapping over floating point numbers.
|
||||
main :: IO ()
|
||||
main
|
||||
-- Perform the mapping over floating point numbers.
|
||||
= do
|
||||
putStrLn "---------- Floating point ----------"
|
||||
mapM_ (\n -> prtD n . mapRange (0,10) (-1,0) $ fromIntegral n) [0..10]
|
||||
|
||||
mapM_ (\n -> prtD n . mapRange (0, 10) (-1, 0) $ fromIntegral n) [0 .. 10]
|
||||
-- Perform the same mapping over exact rationals.
|
||||
putStrLn "---------- Rationals ----------"
|
||||
mapM_ (\n -> prtR n . mapRange (0,10) (-1,0) $ n%1) [0..10]
|
||||
|
||||
where prtD :: PrintfType r => Integer -> Double -> r
|
||||
prtD n x = printf "%2d -> %6.3f\n" n x
|
||||
prtR :: PrintfType r => Integer -> Rational -> r
|
||||
prtR n x = printf "%2d -> %s\n" n (show x)
|
||||
mapM_ (\n -> prtR n . mapRange (0, 10) (-1, 0) $ n % 1) [0 .. 10]
|
||||
where
|
||||
prtD
|
||||
:: PrintfType r
|
||||
=> Integer -> Double -> r
|
||||
prtD = printf "%2d -> %6.3f\n"
|
||||
prtR
|
||||
:: PrintfType r
|
||||
=> Integer -> Rational -> r
|
||||
prtR n x = printf "%2d -> %s\n" n (show x)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue