Core.PercentA scale factor, not bounded between 0% and 100%, represented as a float.
type t = private Base.Float.tExposing that this is a float allows for more optimization. E.g. compiler can optimize some local refs and not box them.
include Ppx_compare_lib.Comparable.S with type t := tinclude Ppx_compare_lib.Comparable.S__local with type t := tinclude Ppx_compare_lib.Equal.S with type t := tinclude Ppx_compare_lib.Equal.S__local with type t := tinclude Ppx_hash_lib.Hashable.S_any with type t := tval hash_fold_t : t Ppx_hash_lib.hash_foldval hash : t -> Ppx_hash_lib.Std.Hash.hash_valueinclude Typerep_lib.Typerepable.S with type t := tval typerep_of_t : t Typerep_lib.Std_internal.Typerep.t @@ portableval typename_of_t : t Typerep_lib.Typename.t @@ portableof_string and t_of_sexp disallow nan, inf, etc. Furthermore, they round to 6 significant digits. They are equivalent to Stable.V2 sexp conversion.
include Interfaces.Stringable with type t := tval of_string : string -> tval to_string : t @ local -> Base.String.t @@ portableto_string can accept a locally-allocated argument.
val to_string_round_trippable : t @ local -> Base.String.t @@ portableEquivalent to Stable.V3.to_string
Sexps are of the form 5bp or 0.05% or 0.0005x.
Warning: equal (t) (t_of_sexp (sexp_of_t t)) is not guaranteed.
First, sexp_of_t truncates to 6 significant digits. Second, multiple serialization round-trips may cause further multiple small drifts.
The sexp conversion here is V2 and not V3 to avoid breaking existing code at the time V3 was introduced (Nov 2022).
New code should explicitly use Percent.Stable.V3 for faithful round-trippable sexp conversion.
include Interfaces.Sexpable with type t := tinclude Sexplib0.Sexpable.Of_sexp with type t := tval t_of_sexp : Sexplib0.Sexp.t -> tinclude Sexplib0.Sexpable.Sexp_of with type t := tinclude Sexplib0.Sexpable.Sexp_of__stack with type t := tval sexp_of_t : t -> Sexplib0.Sexp.tinclude Sexplib.Sexp_grammar.S with type t := tval t_sexp_grammar : t Sexplib0.Sexp_grammar.t @@ portableinclude Bin_prot.Binable.S__local with type t := tinclude Bin_prot.Binable.S_only_functions__local with type t := tval bin_read_t : t Bin_prot.Read.readerval __bin_read_t__ : t Bin_prot.Read.vtag_readerThis 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.
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.tinclude Base.Comparable.S__local__portable with type t := tinclude Base.Comparisons.S__local with type t := tcompare t1 t2 returns 0 if t1 is equal to t2, a negative integer if t1 is less than t2, and a positive integer if t1 is greater than t2.
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.
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 @@ portableinclude Base.Comparator.S__portable with type t := tmodule Replace_polymorphic_compare :
Base.Comparable.Comparisons__local with type t := tinclude Comparator.S__portable
with type t := t
with type comparator_witness := comparator_witnessval comparator : (t, comparator_witness) Base.Comparator.T.comparatormodule Map :
Map_intf.S_binable__portable
with type Key.t = t
with type Key.comparator_witness = comparator_witnessmodule Set :
Set_intf.S_binable__portable
with type Elt.t = t
with type Elt.comparator_witness = comparator_witnessinclude Comparable.With_zero with type t := tval validate_lbound : min:t Maybe_bound.t -> t Validate.checkval validate_ubound : max:t Maybe_bound.t -> t Validate.checkval validate_bound :
min:t Maybe_bound.t ->
max:t Maybe_bound.t ->
t Validate.checkval validate_positive : t Validate.checkval validate_non_negative : t Validate.checkval validate_negative : t Validate.checkval validate_non_positive : t Validate.checkinclude Diffable.S_atomic with type t := tmodule Diff : sig ... endinclude Robustly_comparable.S with type t := tinclude Quickcheckable.S with type t := tval quickcheck_generator : t Base_quickcheck.Generator.tval quickcheck_observer : t Base_quickcheck.Observer.tval quickcheck_shrinker : t Base_quickcheck.Shrinker.tmodule Option : sig ... endThe value nan cannot be represented as an Option.t
val (//) : t -> t -> Base.Float.t @@ portableval zero : t @@ portableval one_hundred_percent : t @@ portableThe functions below come from a Comparable functor and do not handle NaN in any principled way. As of 2025-06-06, the non-legacy versions are updated to match the behavior of their Float counterparts w.r.t. NaN.
val clamp_legacy : t -> min:t -> max:t -> t Or_error.t @@ portableval is_positive_legacy : t -> Base.Bool.t @@ portableval is_non_negative_legacy : t -> Base.Bool.t @@ portableval is_negative_legacy : t -> Base.Bool.t @@ portableval is_non_positive_legacy : t -> Base.Bool.t @@ portableval is_zero : t @ local -> Base.Bool.t @@ portableval is_nan : t @ local -> Base.Bool.t @@ portableval is_inf : t @ local -> Base.Bool.t @@ portableval apply : t -> Base.Float.t -> Base.Float.t @@ portableapply t x multiplies the percent t by x, returning a float.
val scale : t -> Base.Float.t -> t @@ portablescale t x scales the percent t by x, returning a new t.
val of_mult : Base.Float.t -> t @@ portableof_mult 5. is 5x = 500% = 50_000bp
val to_mult : t -> Base.Float.t @@ portableval of_percentage : Base.Float.t @ local -> t @@ portableof_percentage 5. is 5% = 0.05x = 500bp. Note: this function performs float division by 100.0 and it may introduce rounding errors, for example:
of_percentage 70.18 |> to_mult = 0.70180000000000009It is also not consistent with of_string or t_of_sexp for "%"-ending strings. The results can be off by an ulp. If this matters to you, use of_percentage_slow_more_accurate instead.
val of_percentage_slow_more_accurate : Base.Float.t -> t @@ portableLike of_percentage, but consistent with of_string and t_of_sexp, that is, of_percentage_slow_more_accurate x = of_string (Float.to_string x ^ "%")
val to_percentage : t @ local -> Base.Float.t @@ portableto_percentage (Percent.of_string "5%") is 5.0. Note: this function performs float multiplication by 100.0 and it may introduce rounding errors, for example:
to_percentage (Percent.of_mult 0.56) = 56.000000000000007It is also not consistent with Stable.V3.sexp_of_t or to_string_round_trippable. If this matters to you, use to_percentage_slow_more_accurate instead.
val to_percentage_slow_more_accurate : t -> Base.Float.t @@ portableLike to_percentage, but consistent with Stable.V3.sexp_of_t and to_string_round_trippable.
val of_bp : Base.Float.t @ local -> t @@ portableof_bp 5. is 5bp = 0.05% = 0.0005x. Note: this function performs float division by 10,000.0 and it may introduce rounding errors, for example:
of_bp 70.18 |> to_mult = 0.0070180000000000008It is also not consistent with of_string or t_of_sexp for "bp"-ending strings. The results can be off by an ulp. If this matters to you, use of_bp_slow_more_accurate instead.
val of_bp_slow_more_accurate : Base.Float.t -> t @@ portableLike of_bp, but consistent with of_string and t_of_sexp, that is, of_bp_slow_more_accurate x = of_string (Float.to_string x ^ "bp")
val to_bp : t @ local -> Base.Float.t @@ portableto_bp (Percent.of_bp "4bp") is 4.0. Note: this function performs float multiplication by 10000.0 and and it may introduce rounding errors, for example:
to_bp (Percent.of_mult 0.56) = 5600.0000000000009It is also not consistent with Stable.V3.sexp_of_t or to_string_round_trippable. If this matters to you, use to_bp_slow_more_accurate instead.
val to_bp_slow_more_accurate : t -> Base.Float.t @@ portableLike to_bp, but consistent with Stable.V3.sexp_of_t and to_string_round_trippable.
val of_bp_int : Base.Int.t -> t @@ portableval to_bp_int : t @ local -> Base.Int.t @@ portablerounds down
val round_significant : t -> significant_digits:Base.Int.t -> t0.0123456% ~significant_digits:4 is 1.235bp
val round_decimal_mult : t -> decimal_digits:Base.Int.t -> t0.0123456% ~decimal_digits:4 is 0.0001 = 1bp
val round_decimal_percentage :
t @ local ->
(decimal_digits:Base.Int.t ->
t) @ local @@ portable0.0123456% ~decimal_digits:4 is 0.0123% = 1.23bp
val round_decimal_bp :
t @ local ->
(decimal_digits:Base.Int.t ->
t) @ local @@ portable0.0123456% ~decimal_digits:4 is 1.2346bp
val of_string_allow_nan_and_inf : Base.String.t -> t @@ portablemodule Format : sig ... endA Format.t tells Percent.format how to render a floating-point value as a string, like a printf conversion specification.
val format : t -> Format.t -> Base.String.t @@ portableval validate : t -> Validate.t @@ portableThe sign of a Percent.t. Both -0. and 0. map to Zero. Raises on nan. All other values map to Neg or Pos.
val arg_type : t Command.Arg_type.t @@ portableval arg_type_allow_nan_and_inf : t Command.Arg_type.t @@ portablemodule Stable : sig ... endmodule Always_percentage : sig ... endDoes not format small values as "3bp" or large ones as "2x"; always uses percentages ("0.0003%" or "200%"). The standard of_sexp can read these just fine.
module Almost_round_trippable : sig ... endSimilar to Stable.V3, but rounds to 14 significant digits in order to make the output more palatable to humans, at the cost of making it not exactly round-trippable, e.g.