module type S =sig
..end
type
t
val typerep_of_t : t Typerep_lib.Std.Typerep.t
val typename_of_t : t Typerep_lib.Std.Typename.t
typeouter =
t
val typerep_of_outer : outer Typerep_lib.Std.Typerep.t
val typename_of_outer : outer Typerep_lib.Std.Typename.t
include Floatable.S
include Identifiable.S
max
and min
will return nan if either argument is nan.
The validate_*
functions always fail if class is Nan
or Infinite
.
include Comparable.With_zero
include Robustly_comparable.S
nan
should be considered undefined.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
infinity
val min_value : t
neg_infinity
val zero : t
val one : t
val minus_one : t
val robust_comparison_tolerance : t
Robust_compare
val epsilon_float : t
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
2**63 - 2**52
.
Note both 0. and -0. map to 0L.val to_int64_preserve_order_exn : t -> int64
nan
if the absolute value of the argument is too largeval of_int64_preserve_order : int64 -> t
val one_ulp : [ `Down | `Up ] -> t -> t
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:[ `Down | `Nearest | `Up | `Zero ] -> 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:[ `Down | `Nearest | `Up | `Zero ] -> t -> int option
val iround_exn : ?dir:[ `Down | `Nearest | `Up | `Zero ] -> 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
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
val min_inan : t -> t -> t
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
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
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
val to_string_round_trippable : float -> string
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
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
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 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
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
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 Terse:sig
..end
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
max
and min
will return nan if either argument is nan.
The validate_*
functions always fail if class is Nan
or Infinite
.
The results of robust comparisons on nan
should be considered undefined.
validate_ordinary
fails if class is Nan
or Infinite
.
equal to infinity
equal to neg_infinity
See Robust_compare
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
min_positive_subnormal_value = 2 ** -1074
min_positive_normal_value = 2 ** -1022
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.
returns nan
if the absolute value of the argument is too large
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.
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
.f <= iround_lbound || f >= iround_ubound
, then iround*
functions will refuse
to round f
, returning None
or raising as appropriate.nan
. Returns
nan
if both arguments are nan
.modf
(-3.14)
returns { fractional = -0.14; integral = -3.; }
!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 %
.
These are for modules that inherit from t, since the infix operators are more
convenient
A sub-module designed to be opened to make working with floats more convenient.
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.
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.
defaults to '_'
defaults to 3
defaults to false
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"
ldexp x n
returns x *. 2 ** n
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
.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
is_finite t
returns true
iff classify t
is in Normal; Subnormal; Zero;
.
In particular, if 1 <= exponent <= 2046, then:
create_ieee_exn ~negative:false ~exponent ~mantissa =
2 ** (exponent - 1023) * (1 + (2 ** -52) * mantissa)
S-expressions contain at most 8 significant digits.