HORIZON HASKELLDocslts/ghc-9.14.x7a6c33e2026-10-12Search packagesCtrl K
Theme: |

GHC 9.14.1 · lts/ghc-9.14.x · 7a6c33e · 2026-10-12

Moduleexact-pi-0.5.1.0Haskell2010

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.

  • ParallelListComp
  • PostfixOperators
  • RankNTypes
  • ExplicitForAll
Contents
  1. Utils
Synopsis
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

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

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