package decimal

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

This is an implementation of decimal floating point arithmetic based on the General Decimal Arithmetic Specification:

http://speleotrove.com/decimal/decarith.html

and IEEE standard 854-1987:

http://en.wikipedia.org/wiki/IEEE_854-1987

Decimal floating point has finite precision with arbitrarily large bounds. The purpose of this module is to support arithmetic using familiar "schoolhouse" rules and to avoid some of the tricky representation issues associated with binary floating point. The package is especially useful for financial applications or for contexts where users have expectations that are at odds with binary floating point (for instance, in binary floating point, 1.00 mod 0.1 gives 0.09999999999999995 instead of 0.0; Decimal.(of_string "1.00" mod of_string "0.1") returns the expected "0.00").

module Signal : sig ... end

Signals are used to control the behaviour of the decimal functions under exceptional conditions.

module Context : sig ... end

Settings that control precision, rounding mode, exceptional behaviour, etc.

type t

A decimal floating-point number. All operations are done in radix (base) 10.

val infinity : t
val neg_infinity : t
val nan : t
val one : t
val zero : t
val of_bigint : Z.t -> t
val of_int : int -> t
val of_string : ?context:Context.t -> string -> t
val of_float : ?context:Context.t -> float -> t

of_float ?context float is the decimal representation of the float. This suffers from floating-point precision loss; the other constructors should be preferred.

val to_bool : t -> bool
val to_rational : t -> Q.t
val to_string : ?eng:bool -> ?context:Context.t -> t -> string
val pp : Format.formatter -> t -> unit
val to_tuple : t -> int * string * int

to_tuple t is a representation of the internals of t as a triple of (sign, coefficient, exponent) for debugging purposes.

val abs : ?round:bool -> ?context:Context.t -> t -> t

abs ?round ?context t is the absolute value of t, rounded only if round is true.

val copy_abs : t -> t

copy_abs t is the absolute value of t without rounding.

val adjusted : t -> int
val negate : ?context:Context.t -> t -> t

negate ?context t is t negated, and rounded under context if necessary.

val copy_negate : t -> t

copy_negate t is t negated without rounding.

val posate : ?context:Context.t -> t -> t

Opposite of negate; a no-op.

val sign : t -> int

sign t is -1 if t is negative, and 1 otherwise.

val compare : t -> t -> int

compare t1 t2 is -1 if t1 < t2, 0 if t1 = t2, 1 if t1 > t2.

val min : t -> t -> t

min t1 t2 is the smaller of t1 and t2.

val max : t -> t -> t

max t1 t2 is the larger of t1 and t2.

val add : ?context:Context.t -> t -> t -> t
val sub : ?context:Context.t -> t -> t -> t
val mul : ?context:Context.t -> t -> t -> t
val div : ?context:Context.t -> t -> t -> t
val div_rem : ?context:Context.t -> t -> t -> t * t

div_rem ?context t1 t2 is (t1 / t2, t1 mod t2).

val rem : ?context:Context.t -> t -> t -> t

rem ?context t1 t2 is t1 mod t2.

val fma : ?context:Context.t -> first_mul:t -> then_add:t -> t -> t

fma ?context ~first_mul ~then_add t is fused multiple-add: t * first_mul + then_add with no rounding of the intermediate product.

t and first_mul are multiplied together, then then_add is added to the product, then a final rounding is performed.

val (~-) : t -> t
val (~+) : t -> t
val (=) : t -> t -> bool
val (<) : t -> t -> bool
val (>) : t -> t -> bool
val (<=) : t -> t -> bool
val (>=) : t -> t -> bool
val (+) : t -> t -> t
val (-) : t -> t -> t
val (*) : t -> t -> t
val (/) : t -> t -> t
val (mod) : t -> t -> t
OCaml

Innovation. Community. Security.