Module Packed_float_option

Packed_float_option.t is the moral equivalent of float option represented under the hood as a regular float where Packed_float_option.is_none = Float.is_nan.

This type is often used whenever we have code dealing with floating point numbers in code and we want to avoid allocating on the OCaml heap.

type t = private float
include Core.Bin_prot.Binable.S__local with type t := t
include Bin_prot.Binable.S_only_functions__local with type t := t
include sig ... end
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__ : t Bin_prot.Read.vtag_reader

This 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.t
val bin_writer_t : t Bin_prot.Type_class.writer
val bin_reader_t : t Bin_prot.Type_class.reader
val globalize : t @ local -> t
include Sexplib0.Sexpable.S with type t := t
include Sexplib0.Sexpable.Of_sexp with type t := t
val t_of_sexp : Sexplib0.Sexp.t -> t
include Sexplib0.Sexpable.Sexp_of with type t := t
val t_sexp_grammar : t Sexplib0.Sexp_grammar.t @@ portable
include Typerep_lib.Typerepable.S with type t := t

This interface includes hash. However, the evaluation of hash t requires a comparison, and also it allocates, so this should not be used in highly performance-sensitive applications.

include Core.Immediate_option_intf.S_without_immediate_zero_alloc__local with type t := t and type value := float
include Core.Immediate_option_intf.S_without_immediate_plain_zero_alloc__local with type t := t with type value := float
module Optional_syntax : sig ... end
val none : t
val some_is_representable : float @ local -> Base.Bool.t
val is_none : t @ local -> Base.Bool.t
val is_some : t @ local -> Base.Bool.t
val some : float -> t
val value : t -> default:float -> float
val value_exn : t -> float
val unchecked_value : t -> float
val to_option : t -> float Base.Option.t
val of_option : float Base.Option.t -> t
include Core.Immediate_option_intf.S_derivers__local with type t := t
include Ppx_compare_lib.Comparable.S with type t := t
include Ppx_hash_lib.Hashable.S_any with type t := t
val hash_fold_t : t Ppx_hash_lib.hash_fold
val sexp_of_t : t -> Sexplib0.Sexp.t
include Typerep_lib.Typerepable.S with type t := t
val typerep_of_t : t Typerep_lib.Std_internal.Typerep.t @@ portable
val typename_of_t : t Typerep_lib.Typename.t @@ portable
val of_or_null : float or_null -> t
val to_or_null : t -> float or_null
include Core.Comparable.Map_and_set_binable with type t := t
include Core.Comparator.S with type t := t
type comparator_witness
module Map : sig ... end
module Set : sig ... end
module Array : sig ... end
val zero : t @@ portable
val compare : t -> t -> int @@ portable

Orders none before anything else

val between : t -> low:t -> high:t -> bool @@ portable
val equal : t -> t -> bool @@ portable

Unlike polymorphic compare, none=none.

val value_map : t -> f:(float -> 'a) -> default:'a -> 'a @@ portable
val of_float_nan_as_none : float -> t @@ portable

In some applications, float arthimetic is done where nan is used to denote an invalid value. This function can be used to interpret such results as a t with no overhead (internally, both this and unchecked_value are just the identity).

val to_float_none_as_nan : t -> float @@ portable
val abs : t -> t @@ portable

The result of arithmetic operations will be none if the operand is. In addition, if the float operation would give nan, the result is none.

val neg : t -> t @@ portable
val log : t -> t @@ portable
val log10 : t -> t @@ portable
val log1p : t -> t @@ portable
val sqrt : t -> t @@ portable
val square : t -> t @@ portable
val exp : t -> t @@ portable
val is_inf : t -> bool @@ portable

The result of is_inf will be: true: if operand is some inf or some -inf; false: otherwise, including when operand is none

val is_positive : t -> bool @@ portable
val is_non_positive : t -> bool @@ portable
val is_negative : t -> bool @@ portable
val is_non_negative : t -> bool @@ portable
val is_integer : t -> bool @@ portable
val is_finite : t -> bool @@ portable
val min : t -> t -> t @@ portable

The result of min will be none if either operand is.

val inv : t -> t @@ portable
val scale : t -> float -> t @@ portable
val first_some : t -> t -> t @@ portable

first_some x y returns x if x is not none, else returns y

val merge : t -> t -> f:(float -> float -> float) -> t @@ portable

merge x y ~f merges together the values from x and y using f. If both x and y are None, returns none. If only one is Some, returns that one, and if both are Some, returns the result of applying f to the contents of x and y.

val validate : none:unit Core.Validate.check -> some:float Core.Validate.check -> t Core.Validate.check @@ portable
val validate_option_bound : may_be_none:bool -> ?min:float Core.Maybe_bound.t -> ?max:float Core.Maybe_bound.t -> t Core.Validate.check @@ portable
val to_string : t -> string @@ portable
val divide_if_denominator_nonzero_else : numerator:t -> denominator:t -> else_:t -> t @@ portable

Computes numerator / denominator when denominator is nonzero, otherwise returns else_. This is done without allocating or branching by unboxing the arguments and then using Unboxed.select.

module Infix : sig ... end

The result of arithmetic operations will be none if either operand is. In addition, if the float operation would give nan, the result is none.

module Ieee_nan : sig ... end

Operations where we use NaN semantics even though they are inconsistent with the remaining operators, e.g., >= violates the equivalance a >= b <-> a > b || a = b because it returns false for two none inputs.

module Local : sig ... end
module Stable : sig ... end
module Unboxed : sig ... end

A variant of the above type using unboxed floats. The operations are largely the same (but we can't currently share the module type across layouts.)

val (+) : float -> float -> float
val (-) : float -> float -> float
val (*) : float -> float -> float
val (/) : float -> float -> float
val (**) : float -> float -> float

Returns true if both operands are none or (not none and equal).

val (=) : float -> float -> bool
val (<>) : float -> float -> bool
module Ref : sig ... end

For records not consisting purely of floats, fields of t are preferable to mutable fields of Packed_float_option.t because their value can be mutated without crossing the GC write barrier.

module Without_infix : sig ... end

Packed_float_option.t is the moral equivalent of float option represented under the hood as a regular float where Packed_float_option.is_none = Float.is_nan.

module type Unboxed_ref = sig ... end