我想take
前奏曲的更多数字pi
值(value)。
Prelude> take 4 $ show pi
"3.14"
但
Prelude> take 17 $ show pi
"3.141592653589793"
Prelude> take 170 $ show pi
"3.141592653589793"
是
pi
常量只是存储了这个截断的?是否有一个选项可以打印到字符串更多的数字?
最佳答案
pi
是 Floating
的一种方法类(class):
class Fractional a => Floating a where
pi :: a
...
所以
pi
是多态的,由实例的实现者来适本地定义它。最常见的实例是
Float
和 Double
精度有限:Prelude> pi :: Float
3.1415927
Prelude> pi :: Double
3.141592653589793
但没有什么能阻止您使用其他软件包,例如
long-double
这在某些系统上提供了更多位:Numeric.LongDouble> pi :: LongDouble
3.1415926535897932385
或
rounded
它通过 MPFR 软件实现提供任意多的精度:Numeric.Rounded> pi :: Rounded TowardNearest 100
3.1415926535897932384626433832793
Numeric.Rounded> pi :: Rounded TowardNearest 500
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081283
numbers
包提供了构造(精确)实数的纯 Haskell 实现,可以根据需要显示为任意多的数字:Data.Number.CReal> showCReal 100 pi
"3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068"
您还可以使用
half
降低精度包,也许可以与 GPU 互操作:Numeric.Half> pi :: Half
3.140625
当您评估
pi
在没有给出特定类型的情况下,解释器的默认规则开始发挥作用。Each defaultable variable is replaced by the first type in the default list that is an instance of all the ambiguous variable’s classes. ... If no default declaration is given in a module then it assumed to be:
default (Integer, Double)
-- https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-790004.3.4
Prelude> :t pi
pi :: Floating a => a
Integer
不是 Floating
, 但是 Double
是,所以歧义类型默认为 Double
.您可以通过启用 -Wtype-defaults
获取更多信息:Prelude> :set -Wtype-defaults
Prelude> pi
<interactive>:2:1: warning: [-Wtype-defaults]
• Defaulting the following constraints to type ‘Double’
(Show a0) arising from a use of ‘print’ at <interactive>:2:1-2
(Floating a0) arising from a use of ‘it’ at <interactive>:2:1-2
• In a stmt of an interactive GHCi command: print it
3.141592653589793
(注意:我编写了
long-double
包并且是 rounded
的当前维护者。)
关于haskell - 如何显示更多的haskell pi 数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53941896/