sci-ratio-0.2.0.1: Rational numbers in scientific notation.

Stabilityexperimental
Safe HaskellNone
LanguageHaskell98

Data.SciRatio

Contents

Description

 

Synopsis

The SciRatio type

data SciRatio a b Source

Represents a fractional number stored in scientific notation: a product of a fractional significand and an integral power of 10.

  • The significand has type a and should be both Fractional and Real. Although this could be a floating-point type, it is not recommended as floating-points use inexact arithmetic and strange bugs will occur as a result.
  • The exponent has type b and should be Integral.

SciRatio behaves in the same way as an ordinary Ratio and supports the same operations. The main property is that it is moxre efficient than Ratio when the exponent is large:

>>> read "(5 % 1) .^ 99999999" :: SciRational   -- works fine
>>> 5e99999999                 :: Rational      -- takes forever

Specialized functions are provided in cases where they can be implemented more efficiently than the default implementation.

The number is always stored in a unique, canonical form: the significand shall never contain factors of 2 and 5 simultaneously, and their multiplicities shall always be nonnegative. (Note that here we treat the significand as a rational number factorized into a product of prime numbers with integral exponents.)

Note: If inputs differ greatly in magnitude, (+) and (-) can be quite slow: complexity is linear with the absolute difference of the exponents. Furthermore, the complexity of toRational is linear with the magnitude of the exponent. These also apply to any functions that indirectly use these operations (including all operations in RealFrac and Enum).

Instances

(Fractional a, Real a, Integral b) => Enum (SciRatio a b) 
(Eq a, Eq b) => Eq (SciRatio a b) 
(Fractional a, Real a, Integral b) => Fractional (SciRatio a b) 
(Fractional a, Real a, Integral b) => Num (SciRatio a b) 
(Real a, Integral b, Ord a) => Ord (SciRatio a b) 
(Fractional a, Real a, Integral b, Read a, Read b) => Read (SciRatio a b) 
(Fractional a, Real a, Integral b) => Real (SciRatio a b) 
(Fractional a, Real a, Integral b) => RealFrac (SciRatio a b) 
(Show a, Show b) => Show (SciRatio a b) 
(Hashable a, Hashable b) => Hashable (SciRatio a b) 

type SciRational = SciRatio Rational Integer Source

A specialization of SciRatio.

(.^) infixl 7 Source

Arguments

:: (Fractional a, Real a, Integral b) 
=> a

significand

-> b

exponent

-> SciRatio a b 

Construct a number such that significand .^ exponent == significand * 10 ^^ exponent.

fracSignificand :: SciRatio a b -> a Source

Extract the fractional significand.

base10Exponent :: SciRatio a b -> b Source

Extract the base-10 exponent.

Specialized functions

(^!) :: (Real a, Integral b, Integral c) => SciRatio a b -> c -> SciRatio a b infixr 8 Source

Specialized, more efficient version of (^).

(^^!) :: (Fractional a, Real a, Integral b, Integral c) => SciRatio a b -> c -> SciRatio a b infixr 8 Source

Specialized, more efficient version of (^^).

fromSciRatio :: (Real a, Integral b, Fractional c) => SciRatio a b -> c Source

Convert into a Fractional number.

This is similar to realToFrac but much more efficient for large exponents.

Miscellaneous utilities

intLog Source

Arguments

:: (Integral a, Integral b) 
=> a

base

-> a

input integer

-> (a, b)

significand and exponent

Extract the largest power of the given base that divides the input integer. Returns the significand and exponent, satisfying:

input_integer = significand * base ^ exponent
  • If the input integer is zero, then zeros are returned.
  • If the input integer is negative, the significand is negative.