summaryrefslogtreecommitdiff
path: root/testsuite/tests/numeric/should_run/IntegerToFloat.hs
blob: 4c5c1ea867d5e8a12438a1ad91255857dd1a2d11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Control.Monad
import Data.Word
import GHC.Float (word2Double, word2Float)
import Numeric (showHFloat)

t15926 :: IO ()
t15926 = do
  let sources = [ 2^63             -- exact
                , 2^63 - 1         -- round up
                , 2^63 - 2^9       -- round up
                , 2^63 - 2^9 - 1   -- round down
                , 2^63 - 2^10      -- exact
                , 2^64             -- exact
                , 2^64 - 1         -- round up
                , 2^64 - 2^10      -- round up
                , 2^64 - 2^10 - 1  -- round down
                , 2^64 - 2^11      -- exact
                ] :: [Integer]
  forM_ sources $ \i -> do
    putStrLn (showHFloat (fromInteger i :: Double) "")

t17231 :: IO ()
t17231 = do
  putStrLn (showHFloat (fromInteger $ 4141414141414141*4141414141414141 :: Double) "")

t17782 :: IO ()
t17782 = do
  let x = maxBound :: Word
  print (word2Double x == fromInteger (toInteger x))

toFloat :: IO ()
toFloat = do
  let sources = [ 0xFFFFFF8           -- round up
                , 0xFFFFFF7F          -- round down
                , 0xFFFFFF80          -- round up
                , 0xFFFFFF7FF         -- round down
                , 0xFFFFFF800         -- round up
                , 0xFFFFFE800000000   -- round down
                , 0xFFFFFF800000000   -- round up
                , 0xFFFFFF7FFFFFFFF   -- round down
                , 0xFFFFFF800000001   -- round up
                , 0xFFFFFE8000000000  -- round down
                , 0xFFFFFF8000000000  -- round up
                , 0xFFFFFF7FFFFFFFFF  -- round down
                , 0xFFFFFF8000000001  -- round up
                ] :: [Integer]
  forM_ sources $ \i -> do
    putStrLn (showHFloat (fromInteger i :: Float) "")
  let x = maxBound :: Word
  print (word2Float x == fromInteger (toInteger x))

main :: IO ()
main = do
  t15926
  t17231
  t17782
  toFloat