admin管理员组文章数量:1245697
It's a known fact that numbers like Double which correspond to IEEE 754 floating-point cannot represent numbers like 0.1 accurately.
But when I played around with it in REPL, I'm confused by the behaviour:
ghci> let x = 0.1 :: Double
ghci> :t x
x :: Double
ghci> import Text.Printf
ghci> printf "%.20f\n" x
0.10000000000000000000
To cross check myself, I used Python repl to do the same where I'm getting expected results:
>>> x = 0.1
>>> type(x)
<class 'float'>
>>> print(f"{x:.20f}")
0.10000000000000000555
Why am I seeing different and a unexpected result in the GHC repl ?
It's a known fact that numbers like Double which correspond to IEEE 754 floating-point cannot represent numbers like 0.1 accurately.
But when I played around with it in REPL, I'm confused by the behaviour:
ghci> let x = 0.1 :: Double
ghci> :t x
x :: Double
ghci> import Text.Printf
ghci> printf "%.20f\n" x
0.10000000000000000000
To cross check myself, I used Python repl to do the same where I'm getting expected results:
>>> x = 0.1
>>> type(x)
<class 'float'>
>>> print(f"{x:.20f}")
0.10000000000000000555
Why am I seeing different and a unexpected result in the GHC repl ?
Share Improve this question asked Feb 15 at 10:58 SibiSibi 48.7k18 gold badges104 silver badges171 bronze badges 2 |1 Answer
Reset to default 5The libraries that come with GHC are all based around the idea that when converting to decimal, you want the shortest representation that rounds to your float. If you want to see the most accurate representation instead, I believe you have to pull some tricks. Here's one example of a trick you can pull, using the numbers package:
Data.Number.CReal> showCReal 100 (realToFrac 0.1)
"0.1000000000000000055511151231257827021181583404541015625"
This is <100 digits despite requesting 100 digits because showCReal
drops trailing zeros. Be careful not to drop the realToFrac
-- it is secretly there to suggest to GHC that it choose Double
for the type of the 0.1
, using defaulting and possibly ghci's extended defaulting. Contrast:
Data.Number.CReal> showCReal 100 0.1
"0.1"
本文标签: Haskell39s double behaviourStack Overflow
版权声明:本文标题:Haskell's double behaviour - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740241710a2247481.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
formatRealFloatAlt
, which I believe tries to prefer trailing zeros when possible (?). I am not very sure about this, though -- its code is fairly complex, so mine is only a wild guess. Printing(1.0 / 49.0) * 49.0
seems to confirm this: trailing zeros are preferred over nines in 0.99999.... – chi Commented Feb 15 at 11:25