Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show more digits of haskell's pi?

I'd like to take more digits of the Prelude pi value.

Prelude> take 4 $ show pi
"3.14"

But

Prelude> take 17 $ show pi
"3.141592653589793"

Prelude> take 170 $ show pi
"3.141592653589793"

Is the pi constant just stored this truncated? Is there an option in show to print to string more digits?

like image 770
Mittenchops Avatar asked Dec 27 '18 08:12

Mittenchops


1 Answers

pi is a method of the Floating class:

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

So pi is polymorphic, and it is up to the implementor of the instance to define it appropriately.

The most common instances are Float and Double which have limited precision:

Prelude> pi :: Float
3.1415927
Prelude> pi :: Double
3.141592653589793

but nothing is stopping you from using other packages, like long-double which gives a few more bits on some systems:

Numeric.LongDouble> pi :: LongDouble 
3.1415926535897932385

Or rounded which gives arbitrarily many bits of precision via the MPFR software implementation:

Numeric.Rounded> pi :: Rounded TowardNearest 100
3.1415926535897932384626433832793
Numeric.Rounded> pi :: Rounded TowardNearest 500
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081283

The numbers package provides a pure-Haskell implementation of constructive (exact) real numbers, which can be shown to as many digits as desired:

Data.Number.CReal> showCReal 100 pi
"3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068"

You can also have lower precision with the half package, maybe interoperable with GPU:

Numeric.Half> pi :: Half
3.140625

When you evaluate pi without giving a specific type, the interpreter's defaulting rules come into play.

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 is not Floating, but Double is, so the ambiguous type gets resolved by defaulting to Double. You can get more information by enabling -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

(Note: I wrote the long-double package and am the current maintainer of rounded.)

like image 120
Claude Avatar answered Oct 01 '22 06:10

Claude