BignumSourceArbitrary-precision rational numbers.
include Ppx_hash_lib.Hashable.S with type t := tSexp 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.Sexpable with type t := tinclude Core.Comparable with type t := tinclude Base.Comparable.S with type t := tascending 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.
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 comparator : (t, comparator_witness) Base__.Comparator.comparatormodule Map :
Core.Map.S
with type Key.t = t
with type Key.comparator_witness = comparator_witnessmodule Set :
Core.Set.S
with type Elt.t = t
with type Elt.comparator_witness = comparator_witnessinclude Core.Hashable with type t := tinclude Ppx_compare_lib.Comparable.S with type t := tval compare : t Base__Ppx_compare_lib.compareinclude Core.Equal.S with type t := tval equal : t Base__Equal.equalgen 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.
val zero : tval (//) : int -> int -> tm // n is equivalent to of_int m / of_int n. Example: Bigint.O.(2 // 3).
Beware: 2 ** 8_000_000 will 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.
Default rounding direction is `Nearest. to_multiple_of defaults to one and must not be zero.
val iround :
?dir:[ `Down | `Up | `Nearest | `Zero ] ->
?to_multiple_of:int ->
t ->
int optionNone if the result would overflow or to_multiple_of is zero.
Exception if the result would overflow or to_multiple_of is zero.
Convenience wrapper around round to round to the specified number of decimal digits. This raises if the number is infinite or undefined.
val to_float : t -> floatAccurate if possible. If this number is not representable as a finite decimal fraction, it raises instead.
As above, returns Or_error.t instead of raising
true if and only if to_string_decimal_accurate_exn doesn't raise.
val is_real : t -> booltrue if and only if the number is non-infinity and non-undefined.
Returns Some bigint if is_integer t would return true.
Pretty 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.
Always 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 t returns 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_string is 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.
Transforming a float into a Bignum.t needs 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 a float into a Bignum.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 the Bignum from a short string-representation of the float that guarantees the round-trip float |> 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.t value for it may be built using of_float_dyadic.
For example:
3.14 is 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_dyadic used to be called of_float but we think it is not the right default choice, thus of_float was deprecated, and we introduced different names for this operation to force some explicit decision at call site.
After some time has passed, of_float_decimal will be renamed to of_float, thus re-introducing of_float in the API.
Do not use this function in new code. See sign_exn or sign_or_nan instead.
Returns -1, 0, or 1 according to the sign of the input. Due to an accidental oversight, sign nan = -1.
The sign of a Bignum. Raises on nan.
val of_int : int -> tgen_finite is like gen but excludes values with zero in the denominator.
gen_uniform_excl lower_bound upper_bound produces a uniform distribution between lower_bound and upper_bound, exclusive, in units based on the fractional parts of the bounds plus a number of decimal places proportional to Quickcheck.Generator.size.
gen_incl lower_bound upper_bound produces a distribution of values between lower_bound and upper_bound, inclusive, that is approximately uniform with extra weight given to producing the endpoints lower_bound and upper_bound.