exact-pi-0.5.0.2: Exact rational multiples of pi (and integer powers of pi)

License | MIT
Maintainer | douglas.mcclean@gmail.com
Stability | experimental
Safe Haskell | Safe-Inferred
Language | Haskell2010

Extensions

- ParallelListComp
- RankNTypes
- ExplicitForAll

Data.ExactPi

Contents

- Utils

Description

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.

Synopsis

- data ExactPi
  - = Exact Integer Rational
  - | Approximate (forall a. Floating a => a)
- approximateValue :: Floating a => ExactPi -> a
- isZero :: ExactPi -> Bool
- isExact :: ExactPi -> Bool
- isExactZero :: ExactPi -> Bool
- isExactOne :: ExactPi -> Bool
- areExactlyEqual :: ExactPi -> ExactPi -> Bool
- isExactInteger :: ExactPi -> Bool
- toExactInteger :: ExactPi -> Maybe Integer
- isExactRational :: ExactPi -> Bool
- toExactRational :: ExactPi -> Maybe Rational
- rationalApproximations :: ExactPi -> [Rational]
- getRationalLimit :: Fractional a => (a -> a -> Bool) -> [Rational] -> a

# Documentation

data ExactPi Source #

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`.

#### Instances

Instances details

Monoid ExactPi Source # | The multiplicative monoid over `Rational`s augmented with multiples of `pi`.
Instance details Defined in Data.ExactPi Methods mempty :: ExactPi Source # mappend :: ExactPi -> ExactPi -> ExactPi Source # mconcat :: [ExactPi] -> ExactPi Source #
Semigroup ExactPi Source # | The multiplicative semigroup over `Rational`s augmented with multiples of `pi`.
Instance details Defined in Data.ExactPi Methods (<>) :: ExactPi -> ExactPi -> ExactPi Source # sconcat :: NonEmpty ExactPi -> ExactPi Source # stimes :: Integral b => b -> ExactPi -> ExactPi Source #
Floating ExactPi Source # |

Instance details

Defined in Data.ExactPi

Methods

pi :: ExactPi Source #

exp :: ExactPi -> ExactPi Source #

log :: ExactPi -> ExactPi Source #

sqrt :: ExactPi -> ExactPi Source #

(**) :: ExactPi -> ExactPi -> ExactPi Source #

logBase :: ExactPi -> ExactPi -> ExactPi Source #

sin :: ExactPi -> ExactPi Source #

cos :: ExactPi -> ExactPi Source #

tan :: ExactPi -> ExactPi Source #

asin :: ExactPi -> ExactPi Source #

acos :: ExactPi -> ExactPi Source #

atan :: ExactPi -> ExactPi Source #

sinh :: ExactPi -> ExactPi Source #

cosh :: ExactPi -> ExactPi Source #

tanh :: ExactPi -> ExactPi Source #

asinh :: ExactPi -> ExactPi Source #

acosh :: ExactPi -> ExactPi Source #

atanh :: ExactPi -> ExactPi Source #

log1p :: ExactPi -> ExactPi Source #

expm1 :: ExactPi -> ExactPi Source #

log1pexp :: ExactPi -> ExactPi Source #

log1mexp :: ExactPi -> ExactPi Source #

Num ExactPi Source # |
Instance details Defined in Data.ExactPi Methods (+) :: ExactPi -> ExactPi -> ExactPi Source # (-) :: ExactPi -> ExactPi -> ExactPi Source # (*) :: ExactPi -> ExactPi -> ExactPi Source # negate :: ExactPi -> ExactPi Source # abs :: ExactPi -> ExactPi Source # signum :: ExactPi -> ExactPi Source # fromInteger :: Integer -> ExactPi Source #
Fractional ExactPi Source # |
Instance details Defined in Data.ExactPi Methods (/) :: ExactPi -> ExactPi -> ExactPi Source # recip :: ExactPi -> ExactPi Source # fromRational :: Rational -> ExactPi Source #
Show ExactPi Source # |
Instance details Defined in Data.ExactPi Methods showsPrec :: Int -> ExactPi -> ShowS Source # show :: ExactPi -> String Source # showList :: [ExactPi] -> ShowS Source #

approximateValue :: Floating a => ExactPi -> a Source #

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.

isZero :: ExactPi -> Bool Source #

Identifies whether an `ExactPi` is an exact or approximate representation of zero.

isExact :: ExactPi -> Bool Source #

Identifies whether an `ExactPi` is an exact value.

isExactZero :: ExactPi -> Bool Source #

Identifies whether an `ExactPi` is an exact representation of zero.

isExactOne :: ExactPi -> Bool Source #

Identifies whether an `ExactPi` is an exact representation of one.

areExactlyEqual :: ExactPi -> ExactPi -> Bool Source #

Identifies whether two `ExactPi` values are exactly equal.

isExactInteger :: ExactPi -> Bool Source #

Identifies whether an `ExactPi` is an exact representation of an integer.

toExactInteger :: ExactPi -> Maybe Integer Source #

Converts an `ExactPi` to an exact `Integer` or `Nothing`.

isExactRational :: ExactPi -> Bool Source #

Identifies whether an `ExactPi` is an exact representation of a rational.

toExactRational :: ExactPi -> Maybe Rational Source #

Converts an `ExactPi` to an exact `Rational` or `Nothing`.

rationalApproximations :: ExactPi -> [Rational] Source #

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

getRationalLimit :: Fractional a => (a -> a -> Bool) -> [Rational] -> a Source #

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

    >>> getRationalLimit (==) (rationalApproximations (Exact 1 1)) :: Double
    3.141592653589793