variable-precision-0.2.1: variable-precision floating point

PortabilityBangPatterns, DeriveDataTypeable, Rank2Types
Safe HaskellSafe-Infered



Variable precision software floating point based on (Integer, Int) as used by decodeFloat. Supports infinities and NaN, but not negative zero or denormalization.

Accuracy has not been extensively verified, and termination of numerical algorithms has not been proven.



data VFloat p Source

A software implementation of floating point arithmetic, using a strict pair of Integer and Int, scaled similarly to decodeFloat, along with additional values representing:

  • positive infinity (1/0),
  • negative infinity (-1/0),
  • not a number (0/0).

The Floating instance so far only implements algorithms for:

These Floating methods transit via Double and so have limited precision:

floatRange is arbitrarily limited to mitigate the problems that occur when enormous integers might be needed during some number type conversions (worst case consequence: program abort in gmp).

class HasPrecision t => Normed t whereSource

A selection of norms.



:: (Num t, RealFloat r) 
=> (t -> r)


-> t 
-> t 
-> Int 

A measure of meaningful precision in the difference of two finite non-zero values.

Values of very different magnitude have little meaningful difference, because a + b approxEq a when |a| >> |b|.

Very close values have little meaningful difference, because a + (a - b) approxEq a as |a| >> |a - b|.

effectivePrecisionWith attempts to quantify this.

effectivePrecision :: (NaturalNumber p, HasPrecision t, Normed t, Num (t p)) => t p -> t p -> IntSource

Much like effectivePrecisionWith combined with normInfinity.

(-@?) :: (NaturalNumber p, HasPrecision t, Normed t, Num (t p)) => t p -> t p -> IntSource

An alias for effectivePrecision.

data DFloat Source

A concrete format suitable for storage or wire transmission.

fromDFloat :: NaturalNumber p => DFloat -> Maybe (VFloat p)Source

Thaw a DFloat. Results in Nothing on precision mismatch.

withDFloat :: DFloat -> (forall p. NaturalNumber p => VFloat p -> r) -> rSource

Thaw a DFloat to its natural precision.