在哈斯克尔划分巨大的数字

时间:2018-04-15 20:02:22

标签: haskell

如何在执行1/200时获得十进制格式的数字!

Prelude>factorial x = product([1..x])
Prelude>x = factorial 200
Prelude>1/x
0.0

1 个答案:

答案 0 :(得分:5)

您可以使用CReal

Data.Number.CReal> showCReal 400 (1/product [1..200])
"0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012679769534809624217530164"
Data.Number.CReal> showCReal 30 (1e375/product [1..200])
"1.267976953480962421753016371075"

400 / 30有多少位数要显示。

如果你喜欢Double的速度,你可以考虑在计算产品之前缩放每个数字。

>  product (map (100/) [1..200])
1.2679769534809638e25

但这需要对输出进行一些重新解释。