Module Big_percent

An arbitrary-precision scale factor, not bounded between 0% and 100%.

type t = private Bignum.t
include Ppx_compare_lib.Comparable.S with type t := t
include Ppx_compare_lib.Comparable.S__local with type t := t
include Ppx_compare_lib.Equal.S with type t := t
include Ppx_compare_lib.Equal.S__local with type t := t
include Ppx_hash_lib.Hashable.S_any with type t := t
include Ppx_quickcheck_runtime.Quickcheckable.S with type t := t
val quickcheck_generator : t Base_quickcheck.Generator.t
val quickcheck_observer : t Base_quickcheck.Observer.t
val quickcheck_shrinker : t Base_quickcheck.Shrinker.t
val sexp_of_t : t -> Sexplib0.Sexp.t
include Base.Comparable.S__portable 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 @@ portable

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 @@ portable
val between : t -> low:t -> high:t -> bool @@ portable

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

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

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 @@ portable
include Base.Comparator.S__portable 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_any with type t := t
val hash_fold_t : t Ppx_hash_lib.hash_fold
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 @@ portable
val (+) : t -> t -> t @@ portable
val (-) : t -> t -> t @@ portable
val (/) : t -> t -> t @@ portable
val (//) : t -> t -> Bignum.t @@ portable
val zero : t @@ portable
val one_hundred_percent : t @@ portable
val neg : t -> t @@ portable
val abs : t -> t @@ portable
val is_zero : t -> bool @@ portable
val is_nan : t -> bool @@ portable
val is_inf : t -> bool @@ portable
val apply : t -> Bignum.t -> Bignum.t @@ portable

apply t x multiplies the percent t by x, returning a Bignum.t.

val scale : t -> Bignum.t -> t @@ portable

scale t x scales the percent t by x, returning a new t.

val of_mult : Bignum.t -> t @@ portable

of_mult 5. is 5x = 500% = 50_000bp.

val to_mult : t -> Bignum.t @@ portable
val of_percentage : Bignum.t -> t @@ portable

of_percentage 5. is 5% = 0.05x = 500bp.

val to_percentage : t -> Bignum.t @@ portable
val of_bp : Bignum.t -> t @@ portable

of_bp 5. is 5bp = 0.05% = 0.0005x.

val to_bp : t -> Bignum.t @@ portable
val of_bp_int : int -> t @@ portable

of_bp_int is like of_bp, except it accepts an int instead of a Bignum.t.

val to_bp_int : t -> int option @@ portable

to_bp_int t rounds down.

val to_bp_int_exn : t -> int @@ portable
  • raises [Zarith.Z.Overflow]
val of_percent_decimal : Core.Percent.t -> t @@ portable
val of_percent_dyadic : Core.Percent.t -> t @@ portable
val to_percent : t -> Core.Percent.t @@ portable
val round_decimal_mult : t -> digits:int -> t @@ portable

round_decimal_mult (of_percentage 0.0123456) ~decimal_digits:4 is 0.0001 = 1bp.

val round_decimal_percentage : t -> digits:int -> t @@ portable

round_decimal_percentage (of_percentage 0.0123456) ~decimal_digits:4 is 0.0123% = 1.23bp.

val round_decimal_bp : t -> digits:int -> t @@ portable

round_decimal_bp (of_percentage 0.0123456) ~decimal_digits:4 is 1.2346bp.

val sign_exn : t -> Core.Sign.t @@ portable

sign_exn returns the sign of a t. Both -0. and 0. map to zero.

  • raises [Bignum.NaN]
val of_string_exn : string -> t @@ portable
  • raises [Nan_or_inf]
val of_string_allow_nan_and_inf : string -> t @@ portable
val to_string_accurate : t -> string @@ portable

to_string_accurate uses Bignum.to_string_accurate under the hood before appending a percent suffix (e.g. bp, x, %). If necessary, the Bignum.t representation is parenthesized.

For example:

  to_string_accurate (Big_percent.of_string "1/3x") = "(33.333333333 + 1/3000000000)%"

If the percent is infinite, it returns INFx or -INFx. If the percent is nan, it returns NANbp. These follow the same convention as Core.Percent.

to_string_accurate is roundtrippable with of_string_allow_nan_and_inf.

val to_string_hum : ?delimiter:char -> ?decimals:int -> ?strip_zero:bool -> t -> string @@ portable

to_string_hum t pretty prints t in approximate decimal form, or prints INFx, -INFx, or NANbp.

For example:

  to_string_hum ~delimiter:',' ~decimals:3 ~strip_zero:false 1234.1999x = "1,234.200x"

No delimiters are inserted to the right of the decimal.

Note that 99.9999....% will be rounded as 100% instead of 1x and similarly for bps.

module Stable : sig ... end
module Unstable : sig ... end
module Always_percentage : sig ... end

Always_percentage does not format small values as 3bp or large ones as 2x, but rather always uses percentages (e.g. 0.0003% or 200%).