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

Data.SciRatio

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 more efficient than `Ratio` when the exponent is large:

````>>> ````5 .^ 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. (The significand is treated as a rational number factorized into a product of prime numbers with integral exponents that are not necessarily positive.)

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)

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

Arguments

 :: (Integral a, Integral b) => a base -> a input integer -> (a, b) significand and exponent

Factorize a nonzero integer into a significand and a power of the base such that the exponent is maximized:

`inputInteger = significand * base ^ exponent`

That is, the significand shall not divisible by the base. The base must be greater than one.

Arguments

 :: (Integral a, Integral b) => a base -> a input integer -> b floor of the logarithm

Calculate the floored logarithm of a positive integer. The base must be greater than one.

# Deprecated

intLog :: (Integral a, Integral b) => a -> a -> (a, b) Source

Deprecated: use `factorizeBase` instead.

Alias of `factorizeBase`.

Note: Despite what the name suggests, the function does not compute the floored logarithm.