summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorARATA Mizuki <minorinoki@gmail.com>2021-01-14 20:53:04 +0900
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-03-17 19:05:13 -0400
commitf11954b16c07703b5444eda4a8ab16eadaedc7e6 (patch)
tree890e3ab8430ff4ac4f5a1d8d07bc75366fde17db
parent43a64744458dcec82102d6daf488c33b99f24745 (diff)
downloadhaskell-f11954b16c07703b5444eda4a8ab16eadaedc7e6.tar.gz
Add a test for fromInteger :: Integer -> Float/Double (#15926, #17231, #17782)
-rw-r--r--testsuite/tests/numeric/should_run/IntegerToFloat.hs57
-rw-r--r--testsuite/tests/numeric/should_run/IntegerToFloat.stdout26
-rw-r--r--testsuite/tests/numeric/should_run/all.T2
3 files changed, 85 insertions, 0 deletions
diff --git a/testsuite/tests/numeric/should_run/IntegerToFloat.hs b/testsuite/tests/numeric/should_run/IntegerToFloat.hs
new file mode 100644
index 0000000000..4c5c1ea867
--- /dev/null
+++ b/testsuite/tests/numeric/should_run/IntegerToFloat.hs
@@ -0,0 +1,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
diff --git a/testsuite/tests/numeric/should_run/IntegerToFloat.stdout b/testsuite/tests/numeric/should_run/IntegerToFloat.stdout
new file mode 100644
index 0000000000..69d9a185b0
--- /dev/null
+++ b/testsuite/tests/numeric/should_run/IntegerToFloat.stdout
@@ -0,0 +1,26 @@
+0x1p63
+0x1p63
+0x1p63
+0x0.fffffffffffff8p63
+0x0.fffffffffffff8p63
+0x1p64
+0x1p64
+0x1p64
+0x1.fffffffffffffp63
+0x1.fffffffffffffp63
+0x1.b0f5c00737b63p103
+True
+0x1p28
+0x1.fffffep31
+0x1p32
+0x1.fffffep35
+0x1p36
+0x1.fffffcp59
+0x1p60
+0x1.fffffep59
+0x1p60
+0x1.fffffcp63
+0x1p64
+0x1.fffffep63
+0x1p64
+True
diff --git a/testsuite/tests/numeric/should_run/all.T b/testsuite/tests/numeric/should_run/all.T
index 74d4f220d4..028677e52d 100644
--- a/testsuite/tests/numeric/should_run/all.T
+++ b/testsuite/tests/numeric/should_run/all.T
@@ -74,3 +74,5 @@ test('T18499', normal, compile_and_run, [''])
test('T18509', normal, compile_and_run, [''])
test('T18515', normal, compile_and_run, [''])
test('T18604', normal, compile_and_run, [''])
+
+test('IntegerToFloat', normal, compile_and_run, [''])