package bigdecimal

  1. Overview
  2. Docs

A high-precision representation of decimal numbers as mantissa * 10^exponent, where the mantissa is internally a Bigint.t and the exponent is an int.

type t
include Sexplib0.Sexpable.S with type t := t
val t_of_sexp : Sexplib0.Sexp.t -> t
val sexp_of_t : t -> Sexplib0.Sexp.t
include Core.Bin_prot.Binable.S with type t := t
include Bin_prot.Binable.S_only_functions with type t := t
val bin_size_t : t Bin_prot.Size.sizer
val bin_write_t : t Bin_prot.Write.writer
val bin_read_t : t Bin_prot.Read.reader
val __bin_read_t__ : (int -> t) Bin_prot.Read.reader

This function only needs implementation if t exposed to be a polymorphic variant. Despite what the type reads, this does *not* produce a function after reading; instead it takes the constructor tag (int) before reading and reads the rest of the variant t afterwards.

val bin_shape_t : Bin_prot.Shape.t
val bin_writer_t : t Bin_prot.Type_class.writer
val bin_reader_t : t Bin_prot.Type_class.reader
val zero : t
val one : t
include Core.Comparable.S with type t := t
include Base.Comparable.S with type t := t
include Base.Comparisons.S with type t := t
include Base.Comparisons.Infix with type 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 -> bool
val equal : t -> t -> bool
val min : t -> t -> t
val max : t -> t -> t
val ascending : t -> t -> int

ascending is identical to compare. descending x y = ascending y x. These are intended to be mnemonic when used like List.sort ~compare:ascending and List.sort ~cmp:descending, since they cause the list to be sorted in ascending or descending order, respectively.

val descending : t -> t -> int
val between : t -> low:t -> high:t -> bool

between t ~low ~high means low <= t <= high

val clamp_exn : t -> min:t -> max:t -> t

clamp_exn t ~min ~max returns t', the closest value to t such that between t' ~low:min ~high:max is true.

Raises if not (min <= max).

val clamp : t -> min:t -> max:t -> t Base.Or_error.t
include Base.Comparator.S with type t := t
type comparator_witness
val validate_lbound : min:t Core.Maybe_bound.t -> t Validate.check
val validate_ubound : max:t Core.Maybe_bound.t -> t Validate.check
val validate_bound : min:t Core.Maybe_bound.t -> max:t Core.Maybe_bound.t -> t Validate.check
include Core.Hashable.S with type t := t
include Ppx_compare_lib.Comparable.S with type t := t
val compare : t -> t -> int
include Ppx_hash_lib.Hashable.S with type t := t
val hash_fold_t : Base.Hash.state -> t -> Base.Hash.state
val hash : t -> Base.Hash.hash_value
val hashable : t Base.Hashable.t
module Table : Core.Hashtbl.S with type key = t
module Hash_set : Core.Hash_set.S with type elt = t
module Hash_queue : Core.Hash_queue.S with type key = t
val (+) : t -> t -> t
val (-) : t -> t -> t
val (*) : t -> t -> t
val div : ?decimals_precision:int -> ?rounding_dir:[< `Down | `Up | `Nearest | `Zero | `Bankers ] -> t -> t -> t

div ?decimals_precision a b = a/b, to decimals_precision decimals of precision, rounding to the nearest 10^(-decimals_precision) with the specified rounding dir.

decimals_precision defaults to 15. rounding_dir defaults to `Nearest.

val sqrt : ?decimals_precision:int -> t -> t

Computes the square root to decimals_precision decimals of precision, rounding to the nearest 10^(-decimals_precision).

decimals_precision defaults to 15.

val (**) : t -> int -> t

t ** p computes t raised to the pth power. p must be nonnegative!

val scale_by : t -> power_of_ten:int -> t
val scale_int : t -> int -> t
val mantissa : t -> Bigint.t
val exponent : t -> int
val of_int : int -> t
val of_bigint : Bigint.t -> t
val to_bignum : t -> Bignum.t

Lossless conversion to Bignum.t. Please note, however, that Bignum.to_string may lose precision.

val of_string : string -> t

of_string and to_string_no_sn are precise and round-trip.

val to_string_no_sn : t -> string

Converts to a string without using scientific notation (e.g., no e's show up in the middle, as in 1.3e12)

val to_string_no_sn_grouping : ?sep:char -> t -> string

Like to_string_no_sn but adds separators to group digits in the integral part into triplets, e.g. 1,234,567.890123. sep is comma by default.

val is_integral : t -> bool

true if and only if t is an integer.

val round : ?dir:[< `Down | `Up | `Nearest | `Zero | `Bankers ] -> t -> t

Default rounding direction is `Nearest.

val round_to_bigint : ?dir:[< `Down | `Up | `Nearest | `Zero | `Bankers ] -> t -> Bigint.t

Default rounding direction is `Nearest.

val round_to_power_of_ten : ?dir:[< `Down | `Up | `Nearest | `Zero | `Bankers ] -> t -> power_of_ten:int -> t
val to_bigint_exact : t -> Bigint.t option

Returns None iff is_integral returns false.

val to_bigint_exact_exn : t -> Bigint.t

Raises iff is_integral returns false.

val to_int : t -> int option

Returns t as an exact integer, if t is integral and fits within int; None otherwise.

val to_int_exn : t -> int

An exception-throwing version of to_int.

Floating-point conversions

to_float and of_float round-trip when starting with a float.

val to_float : t -> float

to_float is lossy, since not all decimals can be represented as floats. The result is the floating point number that is closest to the provided decimal.

val abs : t -> t
val neg : t -> t
val sign : t -> Core.Sign.t
val is_zero : t -> bool
val log10_int_exact : t -> int option

Calculate the log in base 10. If it is not representable as an integer, return Option.None

val of_float_short : float -> t Core.Or_error.t

Produces a decimal representation that, when converted back via to_float, produces the original floating point number. It doesn't, however, pick the decimal that is exactly equal to the float, even though this exists.

Instead, it aims to minimize the length of the generated decimal, subject to the roundtrip property described above. See Float.to_string for details on the semantics of the value chosen.

An error is returned in the case that the float is not representable as a decimal, e.g., NaN and infinity.

val of_float_short_exn : float -> t

An exception-throwing version of of_float_short

val of_bignum : Bignum.t -> t Core.Or_error.t

Produces a decimal representation that is exactly equal to the provided bignum, or an error if the bignum is not exactly representable as a decimal: e.g., infinity or 1/3.

val of_bignum_exn : Bignum.t -> t

An exception-throwing version of of_bignum.

module Stable : sig ... end
OCaml

Innovation. Community. Security.