Module Bignum.Bignum0
val hash_fold_t : Base.Hash.state -> t -> Base.Hash.stateval hash : t -> Base.Hash.hash_value
Sexp conversions represent values as decimals if possible, or defaults to (x + y/z) where x is decimal and y and z are integers. So for example, 1/3 <-> (0.333333333 + 1/3000000000). In string and sexp conversions, values with denominator of zero are special-cased: 0/0 <-> "nan", 1/0 <-> "inf", and -1/0 <-> "-inf".
include Core_kernel.Sexpable with type t := t
val t_of_sexp : Base.Sexp.t -> tval sexp_of_t : t -> Base.Sexp.t
include Core_kernel.Comparable with type t := t
include Core_kernel__.Comparable_intf.S_common
include Base.Comparable.S
include Base__.Comparable_intf.Polymorphic_compare
val ascending : t -> t -> intascendingis identical tocompare.descending x y = ascending y x. These are intended to be mnemonic when used likeList.sort ~compare:ascendingandList.sort ~cmp:descending, since they cause the list to be sorted in ascending or descending order, respectively.
val descending : t -> t -> intval between : t -> low:t -> high:t -> boolbetween t ~low ~highmeanslow <= t <= high
val clamp_exn : t -> min:t -> max:t -> tclamp_exn t ~min ~maxreturnst', the closest value totsuch thatbetween t' ~low:min ~high:maxis 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
val comparator : (t, comparator_witness) Base.Comparator.comparator
include Base__.Comparable_intf.Validate with type t := t
val validate_lbound : min:t Base.Maybe_bound.t -> t Base.Validate.checkval validate_ubound : max:t Base.Maybe_bound.t -> t Base.Validate.checkval validate_bound : min:t Base.Maybe_bound.t -> max:t Base.Maybe_bound.t -> t Base.Validate.check
module Replace_polymorphic_compare : Core_kernel__.Comparable_intf.Polymorphic_compare with type t := tmodule Map : Core_kernel.Map.S with type Key.t = t with type Key.comparator_witness = comparator_witnessmodule Set : Core_kernel.Set.S with type Elt.t = t with type Elt.comparator_witness = comparator_witnessinclude Core_kernel.Hashable with type t := t
include Core_kernel.Hashable.Common
val compare : t -> t -> Core_kernel__.Import.intval hash_fold_t : Base.Hash.state -> t -> Base.Hash.stateval hash : t -> Base.Hash.hash_value
val hashable : t Core_kernel.Hashtbl.Hashable.t
module Table : Core_kernel.Hashtbl.S with type key = tmodule Hash_set : Core_kernel.Hash_set.S with type elt = tmodule Hash_queue : Core_kernel.Hash_queue.S with type Key.t = tinclude Core_kernel.Binable with type t := t
include Core_kernel__.Binable0.S_only_functions with type t := t
val bin_size_t : t Bin_prot.Size.sizerval bin_write_t : t Bin_prot.Write.writerval bin_read_t : t Bin_prot.Read.readerval __bin_read_t__ : (int -> t) Bin_prot.Read.readerThis function only needs implementation if
texposed 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 varianttafterwards.
val bin_shape_t : Bin_prot.Shape.tval bin_writer_t : t Bin_prot.Type_class.writerval bin_reader_t : t Bin_prot.Type_class.readerval bin_t : t Bin_prot.Type_class.t
gen produces values with an order of magnitude (roughly the number of digits) in the numerator and denominator proportional to Quickcheck.Generator.size. Also includes values with zero in the denominator.
include Core_kernel.Quickcheckable with type t := t
val quickcheck_generator : t Base_quickcheck.Generator.tval quickcheck_observer : t Base_quickcheck.Observer.tval quickcheck_shrinker : t Base_quickcheck.Shrinker.t
val zero : tval one : tval ten : tval hundred : tval thousand : tval million : tval billion : tval trillion : tval tenth : tval hundredth : tval thousandth : tval millionth : tval billionth : tval trillionth : tval (+) : t -> t -> tval (-) : t -> t -> tval (/) : t -> t -> tNote that division by zero will not raise, but will return inf, -inf, or nan.
val (//) : int -> int -> tm // nis equivalent toof_int m / of_int n. Example:Bigint.O.(2 // 3).
val (*) : t -> t -> tval (**) : t -> int -> tBeware:
2 ** 8_000_000will take at least a megabyte to store the result, and multiplying numbers a megabyte long is slow no matter how clever your algorithm. Be careful to ensure the second argument is reasonably-sized.
val abs : t -> tval neg : t -> tval inverse : t -> tNote that
inverse zeroisinfinity, not an error.
val sum : t list -> tval round : ?dir:[ `Down | `Up | `Nearest | `Zero ] -> ?to_multiple_of:t -> t -> tDefault rounding direction is
`Nearest.to_multiple_ofdefaults tooneand must not bezero.
val iround : ?dir:[ `Down | `Up | `Nearest | `Zero ] -> ?to_multiple_of:int -> t -> int optionNoneif the result would overflow orto_multiple_ofis zero.
val round_as_bigint : ?dir:[ `Down | `Up | `Nearest | `Zero ] -> ?to_multiple_of:Bigint.t -> t -> Bigint.t optionval iround_exn : ?dir:[ `Down | `Up | `Nearest | `Zero ] -> ?to_multiple_of:int -> t -> intException if the result would overflow or
to_multiple_ofis zero.
val round_as_bigint_exn : ?dir:[ `Down | `Up | `Nearest | `Zero ] -> ?to_multiple_of:Bigint.t -> t -> Bigint.tval round_decimal : ?dir:[ `Down | `Up | `Nearest | `Zero ] -> digits:int -> t -> tConvenience wrapper around
roundto round to the specified number of decimal digits. This raises if the number is infinite or undefined.
val round_decimal_to_nearest_half_to_even : digits:int -> t -> tval to_float : t -> floatval to_string_decimal_accurate_exn : t -> stringAccurate if possible. If this number is not representable as a finite decimal fraction, it raises instead.
val to_string_decimal_accurate : t -> string Core_kernel.Or_error.tAs above, returns Or_error.t instead of raising
val is_representable_as_decimal : t -> booltrueif and only ifto_string_decimal_accurate_exndoesn't raise.
val is_real : t -> booltrueif and only if the number is non-infinity and non-undefined.
val is_nan : t -> booltrueif and only if the number is undefined.
val to_string_hum : ?delimiter:char -> ?decimals:int -> ?strip_zero:bool -> t -> stringPretty print bignum in an approximate decimal form or print inf, -inf, nan. For example
to_string_hum ~delimiter:',' ~decimals:3 ~strip_zero:false 1234.1999 = "1,234.200". No delimiters are inserted to the right of the decimal.
val to_string_accurate : t -> stringAlways accurate. If the number is representable as a finite decimal, it will return this decimal string. If the denomiator is zero, it would return "nan", "inf" or "-inf". Finally, if the bignum is a rational non representable as a decimal,
to_string_accurate treturns an expression that evaluates to the right value. Example:to_string_accurate (Bignum.of_string "1/3") = "(0.333333333 + 1/3000000000)".Since the introduction of that function in the API,
of_stringis able to read any value returned by this function, and would yield the original bignum. That is:fun bignum -> bignum |> to_string_accurate |> of_stringis the identity in
Bignum.
val of_float_decimal : float -> tTransforming a
floatinto aBignum.tneeds to be done with care. Most rationals and decimals are not exactly representable as floats, thus their float representation includes some small imprecision at the end of their decimal form (typically after the 17th digits). It is very likely that when transforming afloatinto aBignum.t, it is best to try to determine which was the original value and retrieve it instead of honoring the noise coming from its imprecise float representation.Given that the original value is not available in the context of a function whose type is
float -> Bignum.t, it is not possible to solve that problem in a principled way. However, a very reasonable approximation is to build theBignumfrom a short string-representation of the float that guarantees the round-tripfloat |> to_string |> of_string. In particular, if the float was obtained from a short decimal string, this heuristic in practice succeeds at retrieving the original value.In the context where it is assumed that a float is a perfect representative of the value meant to be modelled, the actual
Bignum.tvalue for it may be built usingof_float_dyadic.For example:
3.14is not a representable decimal, thus:of_float_dyadic (Float.of_string "3.14") = (3.14 + 7/56294995342131200)of_float_decimal (Float.of_string "3.14") = 3.14of_float_dyadicused to be calledof_floatbut we think it is not the right default choice, thusof_floatwas deprecated, and we introduced different names for this operation to force some explicit decision at call site.After some time has passed,
of_float_decimalwill be renamed toof_float, thus re-introducingof_floatin the API.
val of_float_dyadic : float -> tval of_float : float -> tval to_int : t -> int optionRounds toward zero.
Noneif the conversion would overflow
val to_int_exn : t -> intval is_zero : t -> boolval sign : t -> intDo not use this function in new code. See
sign_exnorsign_or_naninstead.Returns -1, 0, or 1 according to the sign of the input. Due to an accidental oversight,
sign nan= -1.
val sign_exn : t -> Core_kernel.Sign.tThe sign of a Bignum. Raises on nan.
val sign_or_nan : t -> Core_kernel.Sign_or_nan.tval of_string : string -> tval of_int : int -> tval num : t -> tnum treturns the numerator of the numeric
val of_bigint : Bigint.t -> tval num_as_bigint : t -> Bigint.tval den_as_bigint : t -> Bigint.tval pp_hum : Stdlib.Format.formatter -> t -> unitval pp_accurate : Stdlib.Format.formatter -> t -> unitval gen_finite : t Core_kernel.Quickcheck.Generator.tgen_finiteis likegenbut excludes values with zero in the denominator.
val gen_uniform_excl : t -> t -> t Core_kernel.Quickcheck.Generator.tgen_uniform_excl lower_bound upper_boundproduces a uniform distribution betweenlower_boundandupper_bound, exclusive, in units based on the fractional parts of the bounds plus a number of decimal places proportional toQuickcheck.Generator.size.
val gen_incl : t -> t -> t Core_kernel.Quickcheck.Generator.tgen_incl lower_bound upper_boundproduces a distribution of values betweenlower_boundandupper_bound, inclusive, that is approximately uniform with extra weight given to producing the endpointslower_boundandupper_bound.
module Stable : sig ... endmodule O : sig ... end