Module Relude_Decimal

Relude.Decimal contains a type t which represents arbitrary precision numeric values, backed by an int mantissa and an int exponent. This can be useful for representing currency values without loss in precision or floating point errors.

Because the mantissa and exponent are backed by ints, there is a fairly restrictive range of values that can be represented.

let a = Decimal(12345, -2); // 123.45
let b = Decimal(6789, 3); // 6789000
type mantissa = int;

The type of the base of our Decimal

type exponent = int;

The type of the exponent of our Decimal

type t =
| Decimal(mantissa, exponent)
;

Represents an arbitrary precision number, backed by an integer mantissa, and integer exponent.

type rounding =
| Truncate
| RoundUp
| RoundDown
| RoundUpOrDown
;

Represents a preference for rounding a Decimal value, when applicable.

let make: int => int => t;

Decimal.make constructs a Decimal from a mantissa and exponent.

let fromInt: int => t;

Decimal.fromInt constructs a Decimal from an int.

let show: t => string;

Decimal.fromString attempts to parse a Decimal from a string

Decimal.show renders the Decimal value to a string, as if the Decimal was represented as a float.

Decimal(12345, -2) |> Decimal.toString // "123.45"
Decimal(12345, 3) |> Decimal.toString // "12345000"
let tenToThePowerOfPositive: int => int;

Decimal.round rounds a Decimal using the given rounding strategy.

Decimal.tenToThePowerOfPositive computes the value of 10^exponent.

The return value of this function is undefined for exponent values < 0.

Decimal.tenToThePowerOf(3) == 1000;
let normalize: t => t => (t, t, int);

Decimal.normalize normalizes the exponent to the minimal exponent for two given Decimal values.

let a = Decimal(12345, -2); // 123.45
let b = Decimal(12345, 3); // 12345000
let res = Decimal.normalize(a, b);

res == (Decimal(12345, -2), Decimal(1234500000, -2), -2)
let add: t => t => t;

Decimal.add adds two Decimal values with no attempt at avoiding overflow.

Note: the arguments are in order of lhs, rhs.

let (+..): t => t => t;

Decimal.(+..) is the infix operator for add.

let subtract: t => t => t;

Decimal.subtract subtracts two Decimal values with no attempt at avoiding overflow.

Note: the arguments are in order of lhs, rhs.

let (-..): t => t => t;

Decimal.(-..) is the infix operator for subtract.