HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleexact-pi-0.5.0.2Haskell2010

Data.ExactPi

This type is sufficient to exactly express the closure of Q ∪ {π} under multiplication and division. As a result it is useful for representing conversion factors between physical units. Approximate values are included both to close the remainder of the arithmetic operations in the Num typeclass and to encode conversion factors defined experimentally.

  • 1 type
  • 12 values
  • ParallelListComp
  • RankNTypes
  • ExplicitForAll
datadata ExactPi
#

Represents an exact or approximate real value. The exactly representable values are rational multiples of an integer power of pi.

Constructors

  • Exact Integer Rational

    Exact z q = q * pi^z. Note that this means there are many representations of zero.

  • Approximate (forall a. Floating a => a)

    An approximate value. This representation was chosen because it allows conversion to floating types using their native definition of pi.

Instances6Floating, Fractional, Num, Show, Semigroup, Monoid
valueapproximateValue :: Floating a => ExactPi -> a
#

Approximates an exact or approximate value, converting it to a Floating type. This uses the value of pi supplied by the destination type, to provide the appropriate precision.

Converts an ExactPi to a list of increasingly accurate rational approximations. Note that Approximate values are converted using the Real instance for Double into a singleton list. Note that exact rationals are also converted into a singleton list.

Implementation is based on Chudnovsky's algorithm.

Utils

1 declaration
valuegetRationalLimit :: Fractional a => (a -> a -> Bool) -> [Rational] -> a
#

Given an infinite converging sequence of rationals, find their limit. Takes a comparison function to determine when convergence is close enough.

Example1 expression
getRationalLimit (==) (rationalApproximations (Exact 1 1)) :: Double3.141592653589793