haskell - 如何显示更多的haskell pi 数字?

标签 haskell show digits

我想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常量只是存储了这个截断的?是否有一个选项可以打印到字符串更多的数字?

最佳答案

piFloating 的一种方法类(class):

class Fractional a => Floating a where
  pi :: a
  ...

所以pi是多态的,由实例的实现者来适本地定义它。

最常见的实例是 FloatDouble精度有限:
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/

相关文章:

Java正则表达式: match any number of digits in round brackets if the closing bracket is the last char in the String

haskell - 如何轻松查看 Template Haskell 语句的输出?

javascript - 有效的隐藏和表演类(class)

java - Android如何设置点后纯文本数字的数字

javascript - 在数字/数字和字母/字符之间添加空格

qt - 如何在Qt中的另一个tableView中显示QSqlTableModel过滤后的数据?

macos - Haskell openGL 和 GLUT 在 Mac OS X 上卡住?我可以在 GLUT 上使用 GLFW 吗?

java - 如何在java中实现haskell数据类型?

Haskell 似乎工作正常,但事实并非如此

jQuery 显示 div 然后消失