base
-
library base
-
module Base
-
module Applicative
-
module type Applicative_infix
-
module type Applicative_infix2
-
module type Applicative_infix3
-
module type Basic
-
module type Basic2
-
module type Basic2_using_map2
-
module type Basic3
-
module type Basic3_using_map2
-
module type Basic_using_map2
-
module Compose
-
argument 1-F
-
module Applicative_infix
-
-
argument 2-G
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module type Let_syntax
-
module Let_syntax
-
module Let_syntax
-
-
module Open_on_rhs_intf
-
-
module type Let_syntax2
-
module Let_syntax
-
module Let_syntax
-
-
module Open_on_rhs_intf
-
-
module type Let_syntax3
-
module Let_syntax
-
module Let_syntax
-
-
module Open_on_rhs_intf
-
-
module Make
-
argument 1-X
-
module Applicative_infix
-
-
module Make2
-
argument 1-X
-
module Applicative_infix
-
-
module Make2_using_map2
-
argument 1-X
-
module Applicative_infix
-
-
module Make3
-
argument 1-X
-
module Applicative_infix
-
-
module Make3_using_map2
-
argument 1-X
-
module Applicative_infix
-
-
module Make_let_syntax
-
argument 1-X
-
argument 2-Intf
-
module Let_syntax
-
module Let_syntax
-
-
-
module Make_let_syntax2
-
argument 1-X
-
argument 2-Intf
-
module Let_syntax
-
module Let_syntax
-
-
-
module Make_let_syntax3
-
argument 1-X
-
argument 2-Intf
-
module Let_syntax
-
module Let_syntax
-
-
-
module Make_using_map2
-
argument 1-X
-
module Applicative_infix
-
-
module Of_monad
-
argument 1-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Applicative_infix
-
-
module Of_monad2
-
argument 1-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Applicative_infix
-
-
module Pair
-
argument 1-F
-
module Applicative_infix
-
-
argument 2-G
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module type S
-
module Applicative_infix
-
-
module type S2
-
module Applicative_infix
-
-
module S2_to_S
-
argument 1-X
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module S2_to_S3
-
argument 1-X
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module type S3
-
module Applicative_infix
-
-
module S3_to_S2
-
argument 1-X
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module S_to_S2
-
argument 1-X
-
module Applicative_infix
-
-
module Applicative_infix
-
-
-
module Array
-
module Avltree
-
module Binary_search
-
module Binary_searchable
-
module type Indexable
-
module type Indexable1
-
module type S
-
module type S1
-
module Which_target_by_key
-
module Which_target_by_segment
-
-
module Blit
-
module Make
-
argument 1-Sequence
-
-
module Make1
-
argument 1-Sequence
-
-
module Make1_generic
-
argument 1-Sequence
-
-
module Make_distinct
-
module Make_to_string
-
argument 1-T
-
argument 2-To_bytes
-
-
module type S
-
module type S1
-
module type S1_distinct
-
module type S_distinct
-
module type S_to_string
-
module type Sequence
-
module type Sequence1
-
-
module Bool
-
module Non_short_circuiting
-
-
module Bytes
-
module From_string
-
module To_string
-
-
module Comparable
-
module Make_using_comparator
-
argument 1-T
-
-
module Polymorphic_compare
-
argument 1-T
-
-
module type S
-
module type With_compare
-
module Comparisons
-
module Container
-
module Continue_or_stop
-
module type Generic
-
module type Generic_phantom
-
module type S0
-
module type S0_phantom
-
module type S1
-
module type S1_phantom
-
module type S1_phantom_invariant
-
module type Summable
-
-
module Continue_or_stop
-
module Either
-
module First
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type Focused
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Second
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
-
module Error
-
module Internal_repr
-
-
module Exn
-
module Export
-
module Field
-
module Fn
-
module Formatter
-
module Hash_set
-
module Hashtbl
-
module type Accessors
-
module type Equal_m
-
module type For_deriving
-
module type Equal_m
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module type Sexp_of_m
-
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module Merge_into_action
-
module type Multi
-
module Poly
-
module type S_poly
-
module type S_without_submodules
-
module type Sexp_of_m
-
-
module Identifiable
-
module type Arg
-
module type Arg_with_comparator
-
module Make_using_comparator
-
argument 1-M
-
-
module type S
-
-
module Info
-
module Internal_repr
-
module type S
-
module Internal_repr
-
-
-
module Int
-
module Hex
-
module type Int_without_module_types
-
module O
-
module type Operators
-
module type Operators_unbounded
-
module type Round
-
module type S_unbounded
-
-
module Int63
-
module Hex
-
module O
-
module Overflow_exn
-
-
module Lazy
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
module T_unforcing
-
-
module Linked_queue
-
module List
-
module Assoc
-
module Cartesian_product
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
module Or_unequal_lengths
-
-
module Map
-
module type Accessors1
-
module type Accessors2
-
module type Accessors3
-
module type Accessors3_with_comparator
-
module type Accessors_generic
-
module type Compare_m
-
module Continue_or_stop
-
module type Creators1
-
module type Creators2
-
module type Creators3_with_comparator
-
module type Creators_and_accessors1
-
module type Creators_and_accessors2
-
module type Creators_and_accessors3_with_comparator
-
module type Creators_and_accessors_generic
-
module type Creators_generic
-
module type Equal_m
-
module Finished_or_unfinished
-
module type For_deriving
-
module type Compare_m
-
module type Equal_m
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module type Sexp_of_m
-
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module Merge_element
-
module Or_duplicate
-
module Poly
-
module type S_poly
-
module type Sexp_of_m
-
module Symmetric_diff_element
-
module Using_comparator
-
module Empty_without_value_restriction
-
argument 1-K
-
-
module Tree
-
module Build_increasing
-
-
-
module With_comparator
-
module With_first_class_module
-
module Without_comparator
-
-
module Maybe_bound
-
module Monad
-
module type Basic
-
module type Basic2
-
module type Basic3
-
module type Basic_indexed
-
module Ident
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type Infix
-
module type Infix2
-
module type Infix3
-
module type Infix_indexed
-
module Make
-
argument 1-X
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Make2
-
argument 1-X
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Make3
-
argument 1-X
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Make_indexed
-
argument 1-X
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Of_monad
-
argument 1-Monad
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
argument 2-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Of_monad2
-
argument 1-Monad
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
argument 2-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Of_monad3
-
argument 1-Monad
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
argument 2-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Of_monad_indexed
-
argument 1-Monad
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
argument 2-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S2
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S3
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S_indexed
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S_without_syntax
-
module Monad_infix
-
-
module type Syntax
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
-
module type Syntax2
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
-
module type Syntax3
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
-
module type Syntax_indexed
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
-
-
module Nothing
-
module Option
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Option_array
-
module Or_error
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Ordered_collection_common
-
module Private
-
-
module Poly
-
module Popcount
-
module Pretty_printer
-
module Register_pp
-
argument 1-M
-
-
module type S
-
module Printf
-
module Result
-
module Error
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Export
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Sequence
-
module Expert
-
module Generator
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
module Step
-
-
module Set
-
module type Accessors0
-
module Named
-
-
module type Accessors1
-
module Named
-
-
module type Accessors2
-
module Named
-
-
module type Accessors2_with_comparator
-
module Named
-
-
module type Accessors_generic
-
module Named
-
-
module type Compare_m
-
module type Creators0
-
module type Creators1
-
module type Creators2
-
module type Creators2_with_comparator
-
module type Creators_and_accessors0
-
module Named
-
-
module type Creators_and_accessors1
-
module Named
-
-
module type Creators_and_accessors2
-
module Named
-
-
module type Creators_and_accessors2_with_comparator
-
module Named
-
-
module type Creators_generic
-
module type Elt_plain
-
module type Equal_m
-
module type For_deriving
-
module type Compare_m
-
module type Equal_m
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module type Sexp_of_m
-
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module Merge_to_sequence_element
-
module Named
-
module type Sexp_of_m
-
module Using_comparator
-
module Empty_without_value_restriction
-
argument 1-Elt
-
-
module Named
-
-
module With_comparator
-
module With_first_class_module
-
module Without_comparator
-
-
module Sexpable
-
module Of_sexpable
-
argument 1-Sexpable
-
argument 2-M
-
-
module Of_sexpable1
-
argument 1-Sexpable
-
argument 2-M
-
-
module Of_sexpable2
-
argument 1-Sexpable
-
argument 2-M
-
-
module Of_sexpable3
-
argument 1-Sexpable
-
argument 2-M
-
-
module Of_stringable
-
argument 1-M
-
-
module type S
-
module type S1
-
module type S2
-
module type S3
-
-
module Sign
-
module Sign_or_nan
-
module Source_code_position
-
module Staged
-
module String
-
module Caseless
-
module Escaping
-
module Search_pattern
-
-
module Stringable
-
module type S
-
-
module Sys
-
module type T1
-
module type T2
-
module type T3
-
module Type_equal
-
module type Injective
-
module type Injective2
-
module Uchar
-
module Uniform_array
-
module Unit
-
module Variant
-
module With_return
-
module Word_size
-
-
-
library base.base_internalhash_types
-
module Base_internalhash_types
-
-
library base.caml
-
module Caml
-
module In_channel
-
module Out_channel
-
-
-
library base.md5
-
module Md5_lib
-
-
library base.shadow_stdlib
-
module Shadow_stdlib
-
module In_channel
-
module Out_channel
-
-
val t_sexp_grammar : t Sexplib0.Sexp_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 Identifiable.S with type t := t
val hash_fold_t : Hash.state -> t -> Hash.state
val hash : t -> Hash.hash_value
include Sexplib0.Sexpable.S with type t := t
val t_of_sexp : Sexplib0.Sexp.t -> t
val sexp_of_t : t -> Sexplib0.Sexp.t
include Stringable.S with type t := t
val of_string : string -> t
include Comparable.S with type t := t
include Comparisons.S with type t := t
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
.
ascending
is identical to compare
. descending x y = ascending y x
. These are intended to be mnemonic when used like List.sort ~compare:ascending
and List.sort
~cmp:descending
, since they cause the list to be sorted in ascending or descending order, respectively.
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
val comparator : ( t, comparator_witness ) Comparator.comparator
include Pretty_printer.S with type t := t
val pp : Formatter.t -> t -> unit
val hashable : t Hashable.t
include Invariant.S with type t := t
val invariant : t -> unit
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: Machine epsilon.
val max_finite_value : t
min_positive_subnormal_value = 2 ** -1074
min_positive_normal_value = 2 ** -1022
val min_positive_subnormal_value : t
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.
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
Note 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 -> int
val of_int64 : int64 -> t
val to_int64 : t -> int64
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 floati
that is an integer withmin_int <= i <= max_int
.
round_* i = i
for any floati
that is an integer.
iround_*_exn (of_int i) = i
for any inti
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 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 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 int63_round_lbound : t
val int63_round_ubound : t
round_significant x ~significant_digits:n
rounds to the nearest number with n
significant digits. More precisely: it returns the representable float closest to x
rounded to n significant digits
. It is meant to be equivalent to sprintf "%.*g" n x
|> Float.of_string
but 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 x
as a float is slightly under-represented:
sprintf "%.11g" x = "4.3608320883";;
sprintf "%.30g" x = "4.36083208834999958014577714493"
More importantly, round_significant
might sometimes give a different result than sprintf ... |> Float.of_string
because 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.5
Therefore:
round_significant 0.009375 ~significant_digits:3 = 0.00938
whereas:
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_significant
is more likely to give the "desired" result than sprintf ... |>
of_string
(that is, the result of rounding the decimal fraction, rather than its float representation). But it's not guaranteed either--see the 4.36083208835
example above.
round_decimal x ~decimal_digits:n
rounds x
to the nearest 10**(-n)
. For positive n
it is meant to be equivalent to sprintf "%.*f" n x |> Float.of_string
, but faster.
All the considerations mentioned in round_significant
apply (both functions use the same code path).
val is_nan : t -> bool
val is_inf : t -> bool
A float is infinite when it is either infinity
or neg_infinity
.
val is_finite : t -> bool
A float is finite when neither is_nan
nor is_inf
is true.
val is_integer : t -> bool
is_integer x
is true
if and only if x
is an integer.
min_inan
and max_inan
return, respectively, the min and max of the two given values, except when one of the values is a nan
, in which case the other is returned. (Returns nan
if both arguments are nan
.)
In analogy to Int.( % ), ( % ):
- always produces non-negative (or NaN) result
- raises when given a negative modulus.
Like the other infix operators, NaNs in mean NaNs out.
Other cases: (a % Infinity) = a when 0 <= a < Infinity, (a % Infinity) = Infinity when -Infinity < a < 0, (+/- Infinity % a) = NaN, (a % 0) = NaN.
module Parts : sig ... end
Returns the fractional part and the whole (i.e., integer) part. For example, 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 %
.
Ordinary functions for arithmetic operations
These are for modules that inherit from t
, since the infix operators are more convenient.
module O : sig ... end
A sub-module designed to be opened to make working with floats more convenient.
module O_dot : sig ... end
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_string : t -> string
to_string x
builds a string s
representing the float x
that guarantees the round trip, that is such that 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 less) of them.
val to_string_hum :
?delimiter:char ->
?decimals:int ->
?strip_zero:bool ->
?explicit_plus:bool ->
t ->
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 : 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 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_string
is defined in terms of to_padded_compact_string_custom
below as
let 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 ->
string
Similar to to_padded_compact_string
but allows the user to provide different abbreviations. This can be useful to display currency values, e.g. $1mm3, where prefix="$", mega="mm".
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 to be faster than **
.
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
.
expm1 x
computes exp x -. 1.0
, giving numerically-accurate results even if x
is close to 0.0
.
log1p x
computes log(1.0 +. x)
(natural logarithm), giving numerically-accurate results even if x
is close to 0.0
.
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.
Arc cosine. The argument must fall within the range [-1.0, 1.0]
. Result is in radians and is between 0.0
and pi
.
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
.
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
.
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.
module Class : sig ... end
Excluding nan the floating-point "number line" looks like:
The sign of a float. Both -0.
and 0.
map to Zero
. Raises on nan. All other values map to Neg
or Pos
.
val sign_or_nan : t -> Sign_or_nan.t
The sign of a float, with support for NaN. Both -0.
and 0.
map to Zero
. All NaN values map to Nan
. All other values map to Neg
or Pos
.
val create_ieee :
negative:bool ->
exponent:int ->
mantissa:Int63.t ->
t Or_error.t
These 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 ieee_negative : t -> bool
val ieee_exponent : t -> int
module Terse : sig ... end
S-expressions contain at most 8 significant digits.