package core_extended

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
include module type of struct include Core.Float end
type t = Base.Float.t
include sig ... end
val typerep_of_t : t Typerep_lib.Std.Typerep.t
val typename_of_t : t Typerep_lib.Std.Typename.t
module Robust_compare = Core.Float.Robust_compare

The results of robust comparisons on nan should be considered undefined.

include Robust_compare.S
val robust_comparison_tolerance : Base.Float.t

intended to be a tolerance on human-entered floats

include Core_kernel.Robustly_comparable.S with type t := Base.Float.t
val (>=.) : Base.Float.t -> Base.Float.t -> bool
val (<=.) : Base.Float.t -> Base.Float.t -> bool
val (=.) : Base.Float.t -> Base.Float.t -> bool
val (>.) : Base.Float.t -> Base.Float.t -> bool
val (<.) : Base.Float.t -> Base.Float.t -> bool
val (<>.) : Base.Float.t -> Base.Float.t -> bool
val robustly_compare : Base.Float.t -> Base.Float.t -> int
module O = Core.Float.O
module Terse = Core.Float.Terse
include Core_kernel.Identifiable.S with type t := t and type comparator_witness := Base.Float.comparator_witness
include sig ... end
val bin_read_t : t Bin_prot.Read.reader
val __bin_read_t__ : (Base.Int.t -> 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
val bin_shape_t : Bin_prot.Shape.t
include Core_kernel.Identifiable.S_common with type t := t
include sig ... end
include Base.Stringable.S with type t := t
include Base.Pretty_printer.S with type t := t
include Core_kernel.Hashable.S_binable with type t := t
include sig ... end
module Table = Core.Float.Table
module Hash_set = Core.Float.Hash_set
module Hash_queue = Core.Float.Hash_queue
include module type of struct include Base.Float end with type t := t with module O := Base.Float.O with module Terse := Base.Float.Terse
include sig ... end
include Base.Floatable.S with type t := t
val of_float : float -> t
val to_float : t -> float

max and min will return nan if either argument is nan.

The validate_* functions always fail if class is Nan or Infinite.

include Base.Identifiable.S with type t := t
include sig ... end
val hash_fold_t : Base.Hash.state -> t -> Base.Hash.state
val hash : t -> Base.Hash.hash_value
val t_of_sexp : Base.Sexp.t -> t
val sexp_of_t : t -> Base.Sexp.t
include Base.Stringable.S with type t := t
val of_string : string -> t
include Base.Comparable.S with type t := t
include Base.Comparable_intf.Polymorphic_compare 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 compare : t -> t -> int

compare 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.

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 Base.Or_error.t
include Base.Comparator.S with type t := t
type comparator_witness = Core.Float.comparator_witness
include Base.Comparable_intf.Validate with type t := t
val validate_lbound : min:t Base.Maybe_bound.t -> t Base.Validate.check
val validate_ubound : max:t Base.Maybe_bound.t -> t Base.Validate.check
val validate_bound : min:t Base.Maybe_bound.t -> max:t Base.Maybe_bound.t -> t Base.Validate.check
include Base.Pretty_printer.S with type t := t
val pp : Caml.Format.formatter -> t -> unit
include Base.Comparable.With_zero with type t := t
val validate_positive : t Base.Validate.check
val validate_non_negative : t Base.Validate.check
val validate_negative : t Base.Validate.check
val validate_non_positive : t Base.Validate.check
val is_positive : t -> bool
val is_non_negative : t -> bool
val is_negative : t -> bool
val is_non_positive : t -> bool
val validate_ordinary : t Base.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 neg_infinity

val zero : t
val one : t
val minus_one : t
val pi : t

The constant pi.

val sqrt_pi : t

The constant sqrt(pi).

val sqrt_2pi : t

The constant sqrt(2 * pi).

val euler : t

Euler-Mascheroni constant (γ).

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

(Not to be confused with robust_comparison_tolerance.)

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
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

round rounds a float to an integer float. iround{,_exn} rounds a float to an int. Both round according to a direction dir, with default dir being `Nearest.

      | `Down    | rounds toward Float.neg_infinity                             |
      | `Up      | rounds toward Float.infinity                                 |
      | `Nearest | rounds to the nearest int ("round half-integers up")         |
      | `Zero    | rounds toward zero                                           |

iround_exn raises when trying to handle nan or trying to handle a float outside the range [float min_int, float max_int).

Here are some examples for round for each direction:

      | `Down    | [-2.,-1.)   to -2. | [-1.,0.)   to -1. | [0.,1.) to 0., [1.,2.) to 1. |
      | `Up      | (-2.,-1.]   to -1. | (-1.,0.]   to -0. | (0.,1.] to 1., (1.,2.] to 2. |
      | `Zero    | (-2.,-1.]   to -1. | (-1.,1.)   to 0.  | [1.,2.) to 1.                |
      | `Nearest | [-1.5,-0.5) to -1. | [-0.5,0.5) to 0.  | [0.5,1.5) to 1.              |

For convenience, versions of these functions with the dir argument hard-coded are provided. If you are writing performance-critical code you should use the versions with the hard-coded arguments (e.g. iround_down_exn). The _exn ones are the fastest.

The following properties hold:

  • of_int (iround_*_exn i) = i for any float i that is an integer with min_int <= i <= max_int.
  • round_* i = i for any float i that is an integer.
  • iround_*_exn (of_int i) = i for any int i with -2**52 <= i <= 2**52.
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

rounds half integers up

val round_nearest_half_to_even : t -> t

rounds half integers to the even integer

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_down_exn : t -> Base.Int63.t
val int63_round_up_exn : t -> Base.Int63.t
val int63_round_nearest_exn : t -> Base.Int63.t
val iround_lbound : t

If f <= iround_lbound || f >= iround_ubound, then iround* functions will refuse to round f, returning None or raising as appropriate.

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 = Core.Float.Parts

Returns the fractional part and the whole (i.e. integer) part. For example, modf (-3.14) returns { fractional = -0.14; integral = -3.; }!

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_dot = Core.Float.O_dot

Similar to O, except that operators are suffixed with a dot, allowing one to have both int and float operators in scope simultaneously.

val to_padded_compact_string : t -> string

Produce a lossy compact string representation of the float. The float is scaled by an appropriate power of 1000 and rendered with one digit after the decimal point, except that the decimal point is written as '.', 'k', 'm', 'g', 't', or 'p' to indicate the scale factor. (However, if the digit after the "decimal" point is 0, it is suppressed.) The smallest scale factor that allows the number to be rendered with at most 3 digits to the left of the decimal is used. If the number is too large for this format (i.e., the absolute value is at least 999.95e15), scientific notation is used instead. E.g.:

  • to_padded_compact_string (-0.01) = "-0 "
  • to_padded_compact_string 1.89 = "1.9"
  • to_padded_compact_string 999_949.99 = "999k9"
  • to_padded_compact_string 999_950. = "1m "

In the case where the digit after the "decimal", or the "decimal" itself are omitted, the numbers are padded on the right with spaces to ensure the last two columns of the string always correspond to the decimal and the digit afterward (except in the case of scientific notation, where the exponent is the right-most element in the string and could take up to four characters).

  • to_padded_compact_string 1. = "1 "
  • to_padded_compact_string 1.e6 = "1m "
  • to_padded_compact_string 1.e16 = "1.e+16"
  • to_padded_compact_string max_finite_value = "1.8e+308"

Numbers in the range -.05 < x < .05 are rendered as "0 " or "-0 ".

Other cases:

  • to_padded_compact_string nan = "nan "
  • to_padded_compact_string infinity = "inf "
  • to_padded_compact_string neg_infinity = "-inf "

Exact ties are resolved to even in the decimal:

  • to_padded_compact_string 3.25 = "3.2"
  • to_padded_compact_string 3.75 = "3.8"
  • to_padded_compact_string 33_250. = "33k2"
  • to_padded_compact_string 33_350. = "33k4"
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.

val log10 : t -> t

Base 10 logarithm

val expm1 : t -> t

expm1 x computes exp x -. 1.0, giving numerically-accurate results even if x is close to 0.0.

val log1p : t -> t

log1p x computes log(1.0 +. x) (natural logarithm), giving numerically-accurate results even if x is close to 0.0.

val copysign : t -> t -> t

copysign x y returns a float whose absolute value is that of x and whose sign is that of y. If x is nan, returns nan. If y is nan, returns either x or -. x, but it is not specified which.

val cos : t -> t

Cosine. Argument is in radians.

val sin : t -> t

Sine. Argument is in radians.

val tan : t -> t

Tangent. Argument is in radians.

val acos : t -> t

Arc cosine. The argument must fall within the range [-1.0, 1.0]. Result is in radians and is between 0.0 and pi.

val asin : t -> t

Arc sine. The argument must fall within the range [-1.0, 1.0]. Result is in radians and is between -pi/2 and pi/2.

val atan : t -> t

Arc tangent. Result is in radians and is between -pi/2 and pi/2.

val atan2 : t -> t -> t

atan2 y x returns the arc tangent of y /. x. The signs of x and y are used to determine the quadrant of the result. Result is in radians and is between -pi and pi.

val hypot : t -> t -> t

hypot x y returns sqrt(x *. x + y *. y), that is, the length of the hypotenuse of a right-angled triangle with sides of length x and y, or, equivalently, the distance of the point (x,y) to origin.

val cosh : t -> t

Hyperbolic cosine. Argument is in radians.

val sinh : t -> t

Hyperbolic sine. Argument is in radians.

val tanh : t -> t

Hyperbolic tangent. Argument is in radians.

val sqrt : t -> t

Square root.

val exp : t -> t

Exponential.

val log : t -> t

Natural logarithm.

module Class = Core.Float.Class
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;.

val sign_exn : t -> Base.Sign.t

The sign of a float. Both -0. and 0. map to Zero. Raises on nan. All other values map to Neg or Pos.

module Sign_or_nan = Core.Float.Sign_or_nan
val sign_or_nan : t -> Sign_or_nan.t
val create_ieee : negative:bool -> exponent:int -> mantissa:Base.Int63.t -> t Base.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:Base.Int63.t -> t
val ieee_negative : t -> bool
val ieee_exponent : t -> int
val ieee_mantissa : t -> Base.Int63.t
val to_string_12 : t -> Base.String.t

to_string_12 x builds a string representing x using up to 12 significant digits. It loses precision. You can use "%{Float#12}" in formats, but consider "%.12g", "%{Float#hum}", or "%{Float}" as alternatives.

val to_string : t -> Base.String.t

to_string x builds a string s representing the float x that guarantees the round trip, i.e., Float.equal x (Float.of_string s).

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 fewer) of them.

val to_string_round_trippable : t -> Base.String.t
  • deprecated [since 2017-04] Use [Float.to_string]
val sign : t -> Core_kernel.Sign.t
  • deprecated [since 2016-01] Replace [sign] with [robust_sign] or [sign_exn]
val robust_sign : t -> Core_kernel.Sign.t

(Formerly sign) Uses robust comparison (so sufficiently small numbers are mapped to Zero). Also maps NaN to Zero. Using this function is weakly discouraged.

val gen_uniform_excl : t -> t -> t Core_kernel.Quickcheck.Generator.t

gen_uniform_excl lo hi creates a Quickcheck generator producing finite t values between lo and hi, exclusive. The generator approximates a uniform distribution over the interval (lo, hi). Raises an exception if lo is not finite, hi is not finite, or the requested range is empty.

The implementation chooses values uniformly distributed between 0 (inclusive) and 1 (exclusive) up to 52 bits of precision, then scales that interval to the requested range. Due to rounding errors and non-uniform floating point precision, the resulting distribution may not be precisely uniform and may not include all values between lo and hi.

gen_incl lo hi creates a Quickcheck generator that produces values between lo and hi, inclusive, approximately uniformly distributed, with extra weight given to generating the endpoints lo and hi. Raises an exception if lo is not finite, hi is not finite, or the requested range is empty.

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

gen_positive produces all (strictly) positive finite t values.

gen_negative produces all (strictly) negative finite t values.

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

include module type of struct include Extended_float end
val pretty : ?on_negative:[ `Blow_up | `Normal | `Print_dir ] -> float -> string

pretty-print a float using no more than five characters, using abberviations k, m, g, t.

if on_negative is not set to `Normal then the resulting is never over four chars but upon negative number we either:

  • raise a failure
  • or print "<0"
val to_string_hum : float -> string
val order_of_magnitude_difference : float -> float -> int

order_of_magnitude_difference a b by how many orders of magnitude do a and b differ? The return value is non-negative.

examples:

  • order_of_magnitude_difference 11. 1001. = 2
  • order_of_magnitude_difference 1001. 11. = 2
  • order_of_magnitude_difference 131. 11. = 1
  • order_of_magnitude_difference 200. 0.003 = 5
include Number.Verified_std with type repr = Core.Float.t
type repr = Core.Float.t
module type S = Extended_float.S

Abbreviations

module type S0 = Extended_float.S0
module Pos = Extended_float.Pos

Positive and negative numbers with and without zero.

module Pos0 = Extended_float.Pos0
module Neg = Extended_float.Neg
module Neg0 = Extended_float.Neg0
module type Bounded_spec = Extended_float.Bounded_spec

Specification of bounded numbers

module type Bounded = Extended_float.Bounded

Signature of bounded numbers

module Make_bounded = Extended_float.Make_bounded

Functor of creating bounded numbers

Unsafe modules and functors that still fully expose the representation for extensibility.

module Pos_unsafe = Extended_float.Pos_unsafe
module Pos0_unsafe = Extended_float.Pos0_unsafe
module Neg_unsafe = Extended_float.Neg_unsafe
module Neg0_unsafe = Extended_float.Neg0_unsafe
module Make_bounded_unsafe = Extended_float.Make_bounded_unsafe
module type Fraction = Extended_float.Fraction
module Fraction = Extended_float.Fraction
module Fraction_unsafe = Extended_float.Fraction_unsafe
OCaml

Innovation. Community. Security.