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
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
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 bin_t : t Bin_prot.Type_class.t
val zero : t
include Core.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
val descending : t -> t -> int
val between : t -> low:t -> high:t -> bool
val clamp_exn : t -> min:t -> max:t -> t
val clamp : t -> min:t -> max:t -> t Base__.Or_error.t
type comparator_witness
val comparator : (t, comparator_witness) Base__Comparator.comparator
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
module Replace_polymorphic_compare : sig ... end
module Map : sig ... end
module Set : sig ... end
include Core.Hashable.S with type t := t
val compare : t Base__Ppx_compare_lib.compare
val hash_fold_t : t Base__Ppx_hash_lib.hash_fold
val hash : t -> Base__Ppx_hash_lib.Std.Hash.hash_value
val hashable : t Core__.Hashtbl.Hashable.t
module Table : sig ... end
module Hash_set : sig ... end
module Hash_queue : sig ... end
val (+) : t -> t -> t
val (-) : t -> t -> t
val (*) : t -> t -> t
val scale_by : t -> power_of_ten: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 round : ?dir:[ `Down | `Up | `Nearest | `Zero ] -> t -> t

Default rounding direction is `Nearest.

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

Default rounding direction is `Nearest.

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