package bigdecimal

  1. Overview
  2. Docs

Module BigdecimalSource

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.

Sourcetype 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
Sourceval bin_size_t : t Bin_prot.Size.sizer
Sourceval bin_write_t : t Bin_prot.Write.writer
Sourceval bin_read_t : t Bin_prot.Read.reader
Sourceval __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.

Sourceval bin_shape_t : Bin_prot.Shape.t
Sourceval zero : t
include Core.Comparable.S with type t := t
include Base.Comparable.S 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
type comparator_witness
val comparator : (t, comparator_witness) Base__.Comparator.comparator
Sourceval validate_lbound : min:t Core.Maybe_bound.t -> t Validate.check
Sourceval validate_ubound : max:t Core.Maybe_bound.t -> t Validate.check
Sourceval 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
Sourceval compare : t Base__Ppx_compare_lib.compare
include Ppx_hash_lib.Hashable.S with type t := t
Sourceval hash_fold_t : t Base__Ppx_hash_lib.hash_fold
Sourceval hash : t -> Base__Ppx_hash_lib.Std.Hash.hash_value
Sourcemodule Table : Core.Hashtbl.S with type key = t
Sourcemodule Hash_set : Core.Hash_set.S with type elt = t
Sourceval (+) : t -> t -> t
Sourceval (-) : t -> t -> t
Sourceval (*) : t -> t -> t
Sourceval scale_by : t -> power_of_ten:int -> t
Sourceval mantissa : t -> Bigint.t
Sourceval exponent : t -> int
Sourceval of_int : int -> t
Sourceval of_bigint : Bigint.t -> t
Sourceval to_bignum : t -> Bignum.t

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

Sourceval of_string : string -> t

of_string and to_string_no_sn are precise and round-trip.

Sourceval 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)

Sourceval 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.

Sourceval round : ?dir:[ `Down | `Up | `Nearest | `Zero ] -> t -> t

Default rounding direction is `Nearest.

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

Default rounding direction is `Nearest.

Sourceval to_int : t -> int option

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

Sourceval 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.

Sourceval 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.

Sourceval abs : t -> t
Sourceval neg : t -> t
Sourceval sign : t -> Core.Sign.t
Sourceval is_zero : t -> bool
Sourceval 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.

Sourceval of_float_short_exn : float -> t

An exception-throwing version of of_float_short

Sourceval 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.

Sourceval of_bignum_exn : Bignum.t -> t

An exception-throwing version of of_bignum.

Sourcemodule Stable : sig ... end
OCaml

Innovation. Community. Security.