Up

Module Float

Signature

include Float_intf.S with type t = float
type t = float
val typerep_of_t : t Typerep_lib.Std.Typerep.t
val typename_of_t : t Typerep_lib.Std.Typename.t
type outer = t
val typerep_of_outer : outer Typerep_lib.Std.Typerep.t
val typename_of_outer : outer Typerep_lib.Std.Typename.t
val outer_of_sexp : Sexplib.Sexp.t -> outer
val sexp_of_outer : outer -> Sexplib.Sexp.t
val bin_outer : outer Bin_prot.Type_class.t
val bin_read_outer : outer Bin_prot.Read.reader
val __bin_read_outer__ : (int -> outer) Bin_prot.Read.reader
val bin_reader_outer : outer Bin_prot.Type_class.reader
val bin_size_outer : outer Bin_prot.Size.sizer
val bin_write_outer : outer Bin_prot.Write.writer
val bin_writer_outer : outer Bin_prot.Type_class.writer
include Floatable.S with type t := t
type t
val of_float : float -> t
val to_float : t -> float
include Identifiable.S with type t := t
type t
val t_of_sexp : Sexplib.Sexp.t -> t
val sexp_of_t : t -> Sexplib.Sexp.t
val bin_read_t : t Bin_prot.Read.reader
val __bin_read_t__ : (int -> t) Bin_prot.Read.reader
val bin_reader_t : t Bin_prot.Type_class.reader
val bin_size_t : t Bin_prot.Size.sizer
val bin_write_t : t Bin_prot.Write.writer
val bin_writer_t : t Bin_prot.Type_class.writer
include Stringable.S with type t := t
type t
val of_string : string -> t
val to_string : t -> string
include Comparable.S_binable with type t := t
include Comparable_intf.S_common
include Comparable_intf.Polymorphic_compare
include Polymorphic_compare_intf.Infix
type 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 compare : t -> t -> int
val min : t -> t -> t
val max : t -> t -> t
val ascending : t -> t -> int

ascending is identical to compare. descending x y = ascending y x. These are intended to be mnemonic when used like List.sort ~cmp: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
val between : t -> low:t -> high:t -> bool
val clamp_exn : t -> min:t -> max:t -> t

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 Or_error.t
include Comparator.S with type t := t
type t
type comparator_witness
include Comparable_intf.Validate with type t := t
type t
val validate_lbound : min:t Maybe_bound.t -> t Validate.check
val validate_ubound : max:t Maybe_bound.t -> t Validate.check
val validate_bound : min:t Maybe_bound.t -> max:t Maybe_bound.t -> t Validate.check
include Comparable_intf.Map_and_set_binable with type t := t with type comparator_witness := comparator_witness
type t
include Comparator.S with type t := t
type t
type comparator_witness
include Hashable.S_binable with type t := t
type t
val hash : t -> int
module Table : Hashable.Hashtbl.S_binable with type key = t
module Hash_set : Hash_set.S_binable with type elt = t
module Hash_queue : Hash_queue.S with type Key.t = t
include Pretty_printer.S with type t := t
type t
val pp : Format.formatter -> t -> unit
include Comparable.With_zero with type t := t
type t
val validate_positive : t Validate.check
val validate_non_negative : t Validate.check
val validate_negative : t Validate.check
val validate_non_positive : t Validate.check
val is_positive : t -> bool
val is_non_negative : t -> bool
val is_negative : t -> bool
val is_non_positive : t -> bool
include Robustly_comparable.S with type t := t
type 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 robustly_compare : t -> t -> int
include Quickcheckable.S with type t := t
type 'a gen
type 'a obs
type 'a shr
type t
val gen : t gen
val obs : t obs
val shrinker : t shr
val validate_ordinary : t Validate.check

validate_ordinary fails if class is Nan or Infinite.

val nan : t
val infinity : t
val neg_infinity : t
val max_value : t

equal to infinity

val min_value : t

equal to infinity

equal to neg_infinity

val zero : t

equal to neg_infinity

val one : t
val minus_one : t
val robust_comparison_tolerance : t

See Robust_compare

val epsilon_float : t

The difference between 1.0 and the smallest exactly representable floating-point number greater than 1.0. That is:

epsilon_float = (one_ulp `Up 1.0) -. 1.0

This gives the relative accuracy of type t, in the sense that for numbers on the order of x, the roundoff error is on the order of x *. float_epsilon.

See also: http://en.wikipedia.org/wiki/Machine_epsilon

val max_finite_value : t
val min_positive_subnormal_value : t

min_positive_subnormal_value = 2 ** -1074 min_positive_normal_value = 2 ** -1022

val min_positive_normal_value : t
val to_int64_preserve_order : t -> int64 option

An order-preserving bijection between all floats except for nans, and all int64s with absolute value smaller than or equal to 2**63 - 2**52. Note both 0. and -0. map to 0L.

val to_int64_preserve_order_exn : t -> int64

returns nan if the absolute value of the argument is too large

val of_int64_preserve_order : int64 -> t

returns nan if the absolute value of the argument is too large

val one_ulp : [
| `Up
| `Down
] -> t -> t

The next or previous representable float. ULP stands for "unit of least precision", and is the spacing between floating point numbers. Both one_ulp `Up infinity and one_ulp `Down neg_infinity return a nan.

val of_int : int -> t
val to_int : t -> int
val of_int64 : int64 -> t
val to_int64 : t -> int64
val round : ?dir:[
| `Zero
| `Nearest
| `Up
| `Down
] -> t -> t
ERROR: float_intf.ml:103:62-103:63
21:62-21:63 :
unterminated '['
val iround : ?dir:[
| `Zero
| `Nearest
| `Up
| `Down
] -> t -> int option
val iround_exn : ?dir:[
| `Zero
| `Nearest
| `Up
| `Down
] -> t -> int
val round_towards_zero : t -> t
val round_down : t -> t
val round_up : t -> t
val round_nearest : t -> t
val iround_towards_zero : t -> int option
val iround_down : t -> int option
val iround_up : t -> int option
val iround_nearest : t -> int option
val iround_towards_zero_exn : t -> int
val iround_down_exn : t -> int
val iround_up_exn : t -> int
val iround_nearest_exn : t -> int
val int63_round_nearest_exn : t -> Core_int63.t
val iround_lbound : t
ERROR: float_intf.ml:161:21-161:22
21:21-21:22 :
unterminated '['
val iround_ubound : t
val is_nan : t -> bool
val is_inf : t -> bool

includes positive and negative Float.infinity

val min_inan : t -> t -> t

min and max that return the other value if one of the values is a nan. Returns nan if both arguments are nan.

val max_inan : t -> t -> t
val (+) : t -> t -> t
val (-) : t -> t -> t
val (*) : t -> t -> t
val (/) : t -> t -> t
val (~-) : t -> t
module Parts : sig .. end
Returns the fractional part and the whole (i.e.
val modf : t -> Parts.t
val mod_float : t -> t -> t

mod_float x y returns a result with the same sign as x. It returns nan if y is 0. It is basically

 let mod_float x y = x -. float(truncate(x/.y)) *. y

not

 let mod_float x y = x -. floor(x/.y) *. y 

and therefore resembles mod on integers more than %.

val add : t -> t -> t
Ordinary functions for arithmetic operations

These are for modules that inherit from t, since the infix operators are more convenient

val sub : t -> t -> t
val neg : t -> t
val scale : t -> t -> t
val abs : t -> t
module O : sig .. end
A sub-module designed to be opened to make working with floats more convenient.
val to_string_round_trippable : float -> string

Like to_string, but guaranteed to be round-trippable.

It usually yields as few significant digits as possible. That is, it won't print 3.14 as 3.1400000000000001243. The only exception is that occasionally it will output 17 significant digits when the number can be represented with just 16 (but not 15 or less) of them.

val to_string_hum : ?delimiter:char -> ?decimals:int -> ?strip_zero:bool -> float -> string

Pretty print float, for example to_string_hum ~decimals:3 1234.1999 = "1_234.200" to_string_hum ~decimals:3 ~strip_zero:true 1234.1999 = "1_234.2" . No delimiters are inserted to the right of the decimal.

val to_padded_compact_string : float -> string
ERROR: float_intf.ml:277:7-277:8
42:7-42:8 :
text expected
val int_pow : t -> int -> t

int_pow x n computes x ** float n via repeated squaring. It is generally much faster than **.

Note that int_pow x 0 always returns 1., even if x = nan. This coincides with x ** 0. and is intentional.

For n >= 0 the result is identical to an n-fold product of x with itself under *., with a certain placement of parentheses. For n < 0 the result is identical to int_pow (1. /. x) (-n).

The error will be on the order of |n| ulps, essentially the same as if you perturbed x by up to a ulp and then exponentiated exactly.

Benchmarks show a factor of 5-10 speedup (relative to **) for exponents up to about 1000 (approximately 10ns vs. 70ns). For larger exponents the advantage is smaller but persists into the trillions. For a recent or more detailed comparison run the benchmarks.

Depending on context, calling this function might or might not allocate 2 minor words. Even if called in a way that causes allocation, it still appears faster than **.

val ldexp : t -> int -> t

ldexp x n returns x *. 2 ** n

val frexp : t -> t * int

frexp f returns the pair of the significant and the exponent of f. When f is zero, the significant x and the exponent n of f are equal to zero. When f is non-zero, they are defined by f = x *. 2 ** n and 0.5 <= x < 1.0.

module Class : sig .. end
val classify : t -> Class.t

return the Class.t. Excluding nan the floating-point "number line" looks like:

               t                Class.t    example
             ^ neg_infinity     Infinite   neg_infinity
             | neg normals      Normal     -3.14
             | neg subnormals   Subnormal  -.2. ** -1023.
             | (-/+) zero       Zero       0.
             | pos subnormals   Subnormal  2. ** -1023.
             | pos normals      Normal     3.14
             v infinity         Infinite   infinity
      
val is_finite : t -> bool

is_finite t returns true iff classify t is in Normal; Subnormal; Zero;.

module Sign : sig .. end
val sign : t -> Sign.t
val create_ieee : negative:bool -> exponent:int -> mantissa:Core_int63.t -> t Or_error.t

These functions construct and destruct 64-bit floating point numbers based on their IEEE representation with sign bit, 11-bit non-negative (biased) exponent, and 52-bit non-negative mantissa (or significand). See wikipedia for details of the encoding: http://en.wikipedia.org/wiki/Double-precision_floating-point_format.

In particular, if 1 <= exponent <= 2046, then:


        create_ieee_exn ~negative:false ~exponent ~mantissa
        = 2 ** (exponent - 1023) * (1 + (2 ** -52) * mantissa)
      
val create_ieee_exn : negative:bool -> exponent:int -> mantissa:Core_int63.t -> t
val ieee_negative : t -> bool
val ieee_exponent : t -> int
val ieee_mantissa : t -> Core_int63.t
module Nan_dist : sig .. end
val gen_between : nan:Nan_dist.t -> lower_bound:t Maybe_bound.t -> upper_bound:t Maybe_bound.t -> t Quickcheck.Generator.t

gen_between ~nan ~lower_bound ~upper_bound creates a Quickcheck generator producing t values that are either finite numbers satisfying lower_bound and upper_bound, or NaN values satisfying the nan distribution. Raises an exception if no values satisfy lower_bound and upper_bound. ~nan:Without produces no NaN values, ~nan:With_single produces only the single NaN value Float.nan, and ~nan:With_all produces all valid IEEE NaN values.

val gen_finite : t Quickcheck.Generator.t

gen_finite produces all finite t values, excluding infinities and all NaN values.

val gen_without_nan : t Quickcheck.Generator.t

gen_without_nan produces all finite and infinite t values, excluding all NaN values.

module Terse : sig .. end
S-expressions contain at most 8 significant digits.