Module Base__Float
Floating-point representation and utilities.
If using 32-bit OCaml, you cannot quite assume operations act as you'd expect for IEEE 64-bit floats. E.g., one can have let x = ~-. (2. ** 62.) in x = x -. 1. evaluate to false while let x = ~-. (2. ** 62.) in let y = x -. 1 in x = y evaluates to true. This is related to 80-bit registers being used for calculations; you can force representation as a 64-bit value by let-binding.
val t_sexp_grammar : Base.Sexp.Private.Raw_grammar.t
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
val hash_fold_t : Base.Hash.state -> t -> Base.Hash.stateval hash : t -> Base.Hash.hash_value
include Base.Sexpable.S with type t := t
val t_of_sexp : Sexplib0.Sexp.t -> tval sexp_of_t : t -> Sexplib0.Sexp.t
include Base.Stringable.S with type t := t
include Base.Comparable.S with type t := t
include Base__.Comparable_intf.Polymorphic_compare
val ascending : t -> t -> intascendingis identical tocompare.descending x y = ascending y x. These are intended to be mnemonic when used likeList.sort ~compare:ascendingandList.sort ~cmp:descending, since they cause the list to be sorted in ascending or descending order, respectively.
val descending : t -> t -> intval between : t -> low:t -> high:t -> boolbetween t ~low ~highmeanslow <= t <= high
val clamp_exn : t -> min:t -> max:t -> tclamp_exn t ~min ~maxreturnst', the closest value totsuch thatbetween t' ~low:min ~high:maxis 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
val comparator : (t, comparator_witness) Base.Comparator.comparator
include Base__.Comparable_intf.Validate with type t := t
val validate_lbound : min:t Base.Maybe_bound.t -> t Base.Validate.checkval validate_ubound : max:t Base.Maybe_bound.t -> t Base.Validate.checkval validate_bound : min:t Base.Maybe_bound.t -> max:t Base.Maybe_bound.t -> t Base.Validate.check
include Base.Comparable.With_zero with type t := t
val validate_positive : t Base.Validate.checkval validate_non_negative : t Base.Validate.checkval validate_negative : t Base.Validate.checkval validate_non_positive : t Base.Validate.checkval is_positive : t -> boolval is_non_negative : t -> boolval is_negative : t -> boolval is_non_positive : t -> boolval sign : t -> Base__.Sign0.tReturns
Neg,Zero, orPosin a way consistent with the above functions.
val validate_ordinary : t Base.Validate.checkvalidate_ordinaryfails if class isNanorInfinite.
val min_value : tEqual to
neg_infinity.
val sqrt_pi : tThe constant sqrt(pi).
val sqrt_2pi : tThe constant sqrt(2 * pi).
val euler : tEuler-Mascheroni constant (γ).
val epsilon_float : tThe 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.0This gives the relative accuracy of type
t, in the sense that for numbers on the order ofx, the roundoff error is on the order ofx *. float_epsilon.See also: Machine epsilon.
val max_finite_value : t
val min_positive_subnormal_value : tval min_positive_normal_value : tval to_int64_preserve_order : t -> int64 optionAn 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 -> int64val of_int64_preserve_order : int64 -> tReturns
nanif the absolute value of the argument is too large.
val one_ulp : [ `Up | `Down ] -> t -> tThe next or previous representable float. ULP stands for "unit of least precision", and is the spacing between floating point numbers. Both
one_ulp `Up infinityandone_ulp `Down neg_infinityreturn a nan.
val of_int : int -> tNote that this doesn't round trip in either direction. For example,
Float.to_int (Float.of_int max_int) <> max_int.
val to_int : t -> intval of_int63 : Base.Int63.t -> tval of_int64 : int64 -> tval to_int64 : t -> int64val round : ?dir:[ `Zero | `Nearest | `Up | `Down ] -> t -> troundrounds a float to an integer float.iround{,_exn}rounds a float to an int. Both round according to a directiondir, with defaultdirbeing`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_exnraises 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
roundfor 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
dirargument 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_exnones are the fastest.The following properties hold:
of_int (iround_*_exn i) = ifor any floatithat is an integer withmin_int <= i <= max_int.
round_* i = ifor any floatithat is an integer.
iround_*_exn (of_int i) = ifor any intiwith-2**52 <= i <= 2**52.
val iround : ?dir:[ `Zero | `Nearest | `Up | `Down ] -> t -> int optionval iround_exn : ?dir:[ `Zero | `Nearest | `Up | `Down ] -> t -> intval round_towards_zero : t -> tval round_down : t -> tval round_up : t -> tval round_nearest : t -> tRounds half integers up.
val iround_towards_zero : t -> int optionval iround_down : t -> int optionval iround_up : t -> int optionval iround_nearest : t -> int optionval iround_towards_zero_exn : t -> intval iround_down_exn : t -> intval iround_up_exn : t -> intval iround_nearest_exn : t -> intval int63_round_down_exn : t -> Base.Int63.tval int63_round_up_exn : t -> Base.Int63.tval int63_round_nearest_exn : t -> Base.Int63.tval iround_lbound : tIf
f <= iround_lbound || f >= iround_ubound, theniround*functions will refuse to roundf, returningNoneor raising as appropriate.
val iround_ubound : tval round_significant : float -> significant_digits:int -> floatround_significant x ~significant_digits:nrounds to the nearest number withnsignificant digits. More precisely: it returns the representable float closest tox rounded to n significant digits. It is meant to be equivalent tosprintf "%.*g" n x |> Float.of_stringbut faster (10x-15x). Exact ties are resolved as round-to-even.However, it might in rare cases break the contract above.
It might in some cases appear as if it violates the round-to-even rule:
let x = 4.36083208835;; let z = 4.3608320883;; assert (z = fast_approx_round_significant x ~sf:11)But in this case so does sprintf, since
xas a float is slightly under-represented:sprintf "%.11g" x = "4.3608320883";; sprintf "%.30g" x = "4.36083208834999958014577714493"More importantly,
round_significantmight sometimes give a different result thansprintf ... |> Float.of_stringbecause it round-trips through an integer. For example, the decimal fraction 0.009375 is slightly under-represented as a float:sprintf "%.17g" 0.009375 = "0.0093749999999999997"But:
0.009375 *. 1e5 = 937.5Therefore:
round_significant 0.009375 ~significant_digits:3 = 0.00938whereas:
sprintf "%.3g" 0.009375 = "0.00937"In general we believe (and have tested on numerous examples) that the following holds for all x:
let s = sprintf "%.*g" significant_digits x |> Float.of_string in s = round_significant ~significant_digits x || s = round_significant ~significant_digits (one_ulp `Up x) || s = round_significant ~significant_digits (one_ulp `Down x)Also, for float representations of decimal fractions (like 0.009375),
round_significantis more likely to give the "desired" result thansprintf ... |> of_string(that is, the result of rounding the decimal fraction, rather than its float representation). But it's not guaranteed either--see the4.36083208835example above.
val round_decimal : float -> decimal_digits:int -> floatround_decimal x ~decimal_digits:nroundsxto the nearest10**(-n). For positivenit is meant to be equivalent tosprintf "%.*f" n x |> Float.of_string, but faster.All the considerations mentioned in
round_significantapply (both functions use the same code path).
val min_inan : t -> t -> tval max_inan : t -> t -> tval (+) : t -> t -> tval (-) : t -> t -> tval (/) : t -> t -> tval (*) : t -> t -> tval (**) : t -> t -> tval (~-) : t -> t
module Parts : sig ... end with type outer := tReturns 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.tval mod_float : t -> t -> tmod_float x yreturns a result with the same sign asx. It returnsnanifyis0. It is basicallylet mod_float x y = x -. float(truncate(x/.y)) *. ynot
let mod_float x y = x -. floor(x/.y) *. yand therefore resembles
modon integers more than%.
val add : t -> t -> tOrdinary functions for arithmetic operations
These are for modules that inherit from
t, since the infix operators are more convenient.
module O : sig ... endA sub-module designed to be opened to make working with floats more convenient.
module O_dot : sig ... endSimilar to
O, except that operators are suffixed with a dot, allowing one to have both int and float operators in scope simultaneously.
val to_string : t -> stringto_string xbuilds a stringsrepresenting the floatxthat guarantees the round trip, that is such thatFloat.equal x (Float.of_string s).It usually yields as few significant digits as possible. That is, it won't print
3.14as3.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 -> t -> stringPretty 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 : t -> stringProduce 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 is 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"
to_padded_compact_stringis defined in terms ofto_padded_compact_string_custombelow aslet to_padded_compact_string t = to_padded_compact_string_custom t ?prefix:None ~kilo:"k" ~mega:"m" ~giga:"g" ~tera:"t" ~peta:"p" ()
val to_padded_compact_string_custom : t -> ?prefix:string -> kilo:string -> mega:string -> giga:string -> tera:string -> ?peta:string -> unit -> stringSimilar to
to_padded_compact_stringbut allows the user to provide different abbreviations. This can be useful to display currency values, e.g. $1mm3, where prefix="$", mega="mm".
val int_pow : t -> int -> tint_pow x ncomputesx ** float nvia repeated squaring. It is generally much faster than**.Note that
int_pow x 0always returns1., even ifx = nan. This coincides withx ** 0.and is intentional.For
n >= 0the result is identical to an n-fold product ofxwith itself under*., with a certain placement of parentheses. Forn < 0the result is identical toint_pow (1. /. x) (-n).The error will be on the order of
|n|ulps, essentially the same as if you perturbedxby 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 to be faster than
**.
val frexp : t -> t * intfrexp freturns the pair of the significant and the exponent off. Whenfis zero, the significantxand the exponentnoffare equal to zero. Whenfis non-zero, they are defined byf = x *. 2 ** nand0.5 <= x < 1.0.
val expm1 : t -> texpm1 xcomputesexp x -. 1.0, giving numerically-accurate results even ifxis close to0.0.
val log1p : t -> tlog1p xcomputeslog(1.0 +. x)(natural logarithm), giving numerically-accurate results even ifxis close to0.0.
val copysign : t -> t -> tcopysign x yreturns a float whose absolute value is that ofxand whose sign is that ofy. Ifxisnan, returnsnan. Ifyisnan, returns eitherxor-. x, but it is not specified which.
val acos : t -> tArc cosine. The argument must fall within the range
[-1.0, 1.0]. Result is in radians and is between0.0andpi.
val asin : t -> tArc sine. The argument must fall within the range
[-1.0, 1.0]. Result is in radians and is between-pi/2andpi/2.
val atan2 : t -> t -> tatan2 y xreturns the arc tangent ofy /. x. The signs ofxandyare used to determine the quadrant of the result. Result is in radians and is between-piandpi.
val hypot : t -> t -> thypot x yreturnssqrt(x *. x + y *. y), that is, the length of the hypotenuse of a right-angled triangle with sides of lengthxandy, or, equivalently, the distance of the point(x,y)to origin.
module Class : sig ... endExcluding nan the floating-point "number line" looks like:
val classify : t -> Class.tval is_finite : t -> boolis_finite treturnstrueiffclassify tis inNormal; Subnormal; Zero;.
val sign : t -> Base.Sign.tval sign_exn : t -> Base.Sign.tThe sign of a float. Both
-0.and0.map toZero. Raises on nan. All other values map toNegorPos.
val sign_or_nan : t -> Base.Sign_or_nan.tThe sign of a float, with support for NaN. Both
-0.and0.map toZero. All NaN values map toNan. All other values map toNegorPos.
val create_ieee : negative:bool -> exponent:int -> mantissa:Base.Int63.t -> t Base.Or_error.tThese functions construct and destruct 64-bit floating point numbers based on their IEEE representation with a sign bit, an 11-bit non-negative (biased) exponent, and a 52-bit non-negative mantissa (or significand). See Wikipedia for details of the encoding.
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 -> tval ieee_negative : t -> boolval ieee_exponent : t -> intval ieee_mantissa : t -> Base.Int63.t
module Terse : sig ... endS-expressions contain at most 8 significant digits.