Module Float32Source

32-bit floating-point representation and utilities. This is essentially a copy of the Base.Float API.

Sourcetype t = float32
include Bin_prot.Binable.S__local with type t := t
include Bin_prot.Binable.S_only_functions__local with type t := t
include sig ... end
Sourceval bin_size_t : t Bin_prot.Size.sizer
Sourceval bin_write_t : t Bin_prot.Write.writer
Sourceval bin_read_t : t Bin_prot.Read.reader
Sourceval __bin_read_t__ : t Bin_prot.Read.vtag_reader

This function only needs implementation if t exposed to be a polymorphic variant. Despite what the type reads, this does *not* produce a function after reading; instead it takes the constructor tag (int) before reading and reads the rest of the variant t afterwards.

Sourceval bin_shape_t : Bin_prot.Shape.t
Sourceval globalize : t @ local -> t
include Sexplib0.Sexpable.S__stack with type t := t
include Sexplib0.Sexpable.Of_sexp with type t := t
include Sexplib0.Sexpable.Sexp_of__stack with type t := t
include Ppx_quickcheck_runtime.Quickcheckable.S with type t := t
Sourceval quickcheck_generator : t Base_quickcheck.Generator.t
Sourceval quickcheck_observer : t Base_quickcheck.Observer.t
Sourceval quickcheck_shrinker : t Base_quickcheck.Shrinker.t
include Base.Floatable.S with type t := 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__local with type t := t
include Sexplib0.Sexpable.S with type t := t
include Sexplib0.Sexpable.Of_sexp with type t := t
Sourceval t_of_sexp : Sexplib0.Sexp.t -> t
include Sexplib0.Sexpable.Sexp_of with type t := t
Sourceval sexp_of_t : t -> Sexplib0.Sexp.t
include Base.Stringable.S with type t := t
include Base.Stringable.Of_stringable with type t := t
include Base.Stringable.To_stringable with type t := t
include Base.Comparable.S__local with type t := t
include Base.Comparisons.S__local with type t := t
include Base.Comparisons.Infix with type t := t
Sourceval 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 ~compare:ascending and List.sort ~cmp:descending, since they cause the list to be sorted in ascending or descending order, respectively.

Sourceval descending : t -> t -> int
Sourceval between : t -> low:t -> high:t -> bool

between t ~low ~high means low <= t <= high

Sourceval 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).

Sourceval clamp : t -> min:t -> max:t -> t Base.Or_error.t
include Base.Comparator.S with type t := t
Sourcetype comparator_witness : value mod nonportable
include Base.Pretty_printer.S with type t := t
Sourceval pp : Base.Formatter.t -> t -> unit
Sourceval hashable : t Base.Hashable.t
Sourceval of_string : Base.string @ local -> t @@ portable
Sourceval of_string_opt : Base.string @ local -> t Base.option @@ portable
Sourcemodule Util : sig ... end
include Base.Comparable.With_zero with type t := t
Sourceval is_positive : t -> bool
Sourceval is_non_negative : t -> bool
Sourceval is_negative : t -> bool
Sourceval is_non_positive : t -> bool
include Base.Invariant.S with type t := t
Sourceval invariant : t -> unit
include Base.Comparisons.S_with_local_opt__local with type t := t
include Base.Comparisons.Infix_with_local_opt with type t := t
Sourceval (<) : t -> t -> bool
Sourceval (<=) : t -> t -> bool
Sourceval (<>) : t -> t -> bool
Sourceval (=) : t -> t -> bool
Sourceval (>) : t -> t -> bool
Sourceval (>=) : t -> t -> bool
Sourceval equal : t -> t -> bool
Sourceval compare : t -> t -> int
Sourceval min : t -> t -> t
Sourceval max : t -> t -> t
Sourceval nan : t @@ portable
Sourceval infinity : t @@ portable
Sourceval neg_infinity : t @@ portable
Sourceval max_value : t @@ portable

Equal to infinity.

Sourceval min_value : t @@ portable

Equal to neg_infinity.

Sourceval zero : t @@ portable
Sourceval one : t @@ portable
Sourceval minus_one : t @@ portable
Sourceval pi : t @@ portable

The constant pi.

Sourceval sqrt_pi : t @@ portable

The constant sqrt(pi).

Sourceval sqrt_2pi : t @@ portable

The constant sqrt(2 * pi).

Sourceval euler_gamma_constant : t @@ portable

Euler-Mascheroni constant (γ).

Sourceval epsilon_float : t @@ portable

The difference between 1.0s and the smallest exactly representable float32 greater than 1.0s. That is:

epsilon_float = (one_ulp `Up 1.0s) -. 1.0s

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.

Sourceval max_finite_value : t @@ portable
Sourceval min_positive_subnormal_value : t @@ portable
Sourceval min_positive_normal_value : t @@ portable
Sourceval to_int32_preserve_order : t @ local -> Base.int32 Base.option @@ portable

An order-preserving bijection between all float32s except for nans, and all int32s with absolute value smaller than or equal to 2**31 - 2**23. Note both 0.s and -0.s map to 0l.

Sourceval to_int32_preserve_order_exn : t @ local -> Base.int32 @@ portable
Sourceval of_int32_preserve_order : Base.int32 @ local -> t @@ portable

Returns nan if the absolute value of the argument is too large.

Sourceval one_ulp : [ `Up | `Down ] -> t @ local -> t @@ portable

The next or previous representable float32. 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.

Sourceval of_float : Base.float @ local -> t @@ portable

Converts a 64-bit float to the nearest representable float32.

Sourceval to_float : t @ local -> Base.float @@ portable

Converts a float32 to a 64-bit float.

Sourceval of_int : Base.int -> t @@ portable

Convert an integer to a float32. Note that this doesn't round trip in either direction. For example, Float32.to_int (Float32.of_int max_int) <> max_int.

Sourceval to_int : t @ local -> Base.int @@ portable

Truncate the given float32 to an integer. The result is unspecified if the argument is nan or falls outside the range of representable integers.

Sourceval of_int64 : Base.int64 @ local -> t @@ portable

Converts the given int64 to the nearest representable float32. The amd64 flambda-backend compiler translates this call to CVTSI2SS.

Sourceval to_int64 : t @ local -> Base.int64 @@ portable

Truncate the given float32 number to an int64. The result is unspecified if the argument is nan or falls outside the range of representable int64s. The amd64 flambda-backend compiler translates this call to CVTTSS2SI.

Sourceval of_bits : Base.int32 @ local -> t @@ portable

Converts an int32 to a float32 with the same bit pattern. The amd64 flambda-backend compiler translates this call to MOVD.

Sourceval to_bits : t @ local -> Base.int32 @@ portable

Converts a float32 to an int32 with the same bit pattern. The amd64 flambda-backend compiler translates this call to MOVD.

Sourceval round : ?dir:[ `Zero | `Nearest | `Up | `Down ] -> t @ local -> t @@ portable

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

  | `Down    | rounds toward Float32.neg_infinity                             |
  | `Up      | rounds toward Float32.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 float32 outside the range [float32 min_int, float32 max_int).

Here are some examples for round for each direction:

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

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 float32 i that is an integer with min_int <= i <= max_int.
  • round_* i = i for any float32 i that is an integer.
  • iround_*_exn (of_int i) = i for any int i with -2**23 <= i <= 2**23.
Sourceval iround : ?dir:[ `Zero | `Nearest | `Up | `Down ] -> t @ local -> Base.int Base.option @@ portable
Sourceval iround_exn : ?dir:[ `Zero | `Nearest | `Up | `Down ] -> t @ local -> Base.int @@ portable
Sourceval round_towards_zero : t @ local -> t @@ portable

Rounds a float32 to the next integer float32 toward zero. The amd64 flambda-backend compiler translates this call to ROUNDSS.

Sourceval round_down : t @ local -> t @@ portable

Rounds a float32 down to the next integer float32 toward negative infinity. The amd64 flambda-backend compiler translates this call to ROUNDSS.

Sourceval round_up : t @ local -> t @@ portable

Rounds a float32 up to the next integer float32 toward positive infinity. The amd64 flambda-backend compiler translates this call to ROUNDSS.

Sourceval round_nearest : t @ local -> t @@ portable

Rounds half integers up.

Sourceval round_nearest_half_to_even : t @ local -> t @@ portable

Rounds half integers to the even integer.

Sourceval iround_towards_zero : t @ local -> Base.int Base.option @@ portable
Sourceval iround_down : t @ local -> Base.int Base.option @@ portable
Sourceval iround_up : t @ local -> Base.int Base.option @@ portable
Sourceval iround_nearest : t @ local -> Base.int Base.option @@ portable
Sourceval iround_towards_zero_exn : t @ local -> Base.int @@ portable
Sourceval iround_down_exn : t @ local -> Base.int @@ portable
Sourceval iround_up_exn : t @ local -> Base.int @@ portable
Sourceval iround_nearest_exn : t @ local -> Base.int @@ portable
Sourceval iround_nearest_half_to_even : t @ local -> Base.int64 @@ portable

If the argument is NaN, infinite, or otherwise cannot be represented, no exception is raised and the result is an unspecified int64.

Sourceval iround_lbound : t @@ portable

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

Sourceval iround_ubound : t @@ portable
Sourceval is_nan : t @ local -> Base.bool @@ portable

A float32 is nan when it is not a rational number or an infinity.

Sourceval is_inf : t @ local -> Base.bool @@ portable

A float32 is infinite when it is either infinity or neg_infinity.

Sourceval is_finite : t @ local -> Base.bool @@ portable

A float32 is finite when neither is_nan nor is_inf is true.

Sourceval is_integer : t @ local -> Base.bool @@ portable

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

Sourceval min_inan : t -> t -> t @@ portable
Sourceval max_inan : t -> t -> t @@ portable

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

Sourceval (+) : t @ local -> t @ local -> t @@ portable
Sourceval (-) : t @ local -> t @ local -> t @@ portable
Sourceval (*) : t @ local -> t @ local -> t @@ portable
Sourceval (/) : t @ local -> t @ local -> t @@ portable
Sourceval (~-) : t @ local -> t @@ portable
Sourceval (%) : t @ local -> t @ local -> t @@ portable

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.

Sourceval (**) : t @ local -> t @ local -> t @@ portable
Sourcemodule Parts : sig ... end

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

Sourceval modf : t @ local -> Parts.t @@ portable
Sourceval mod_float : t @ local -> t @ local -> t @@ portable

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 -. (float32 (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.

Sourceval add : t @ local -> t @ local -> t @@ portable
Sourceval sub : t @ local -> t @ local -> t @@ portable
Sourceval mul : t @ local -> t @ local -> t @@ portable
Sourceval div : t @ local -> t @ local -> t @@ portable
Sourceval neg : t @ local -> t @@ portable
Sourceval abs : t @ local -> t @@ portable
Sourceval pow : t @ local -> t @ local -> t @@ portable
Sourcemodule O : sig ... end

A sub-module designed to be opened to make working with float32s more convenient.

Sourcemodule O_dot : sig ... end

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

Sourceval to_string : t @ local -> Base.string @@ portable

to_string x builds a string s representing the float32 x that guarantees the round trip, that is such that Float32.equal x (Float32.of_string s).

Sourceval to_string_hum : ?delimiter:Base.char -> ?decimals:Base.int -> ?strip_zero:Base.bool -> ?explicit_plus:Base.bool -> t @ local -> Base.string @@ portable

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

Sourceval to_padded_compact_string : t @ local -> Base.string @@ portable

Produce a lossy compact string representation of the float32. The float32 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.01s) = "-0 "
  • to_padded_compact_string 1.89s = "1.9"
  • to_padded_compact_string 999_949.99s = "999k9"
  • to_padded_compact_string 999_950.s = "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.s = "1 "
  • to_padded_compact_string 1.e6s = "1m "
  • to_padded_compact_string 1.e16s = "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.25s = "3.2"
  • to_padded_compact_string 3.75s = "3.8"
  • to_padded_compact_string 33_250.s = "33k2"
  • to_padded_compact_string 33_350.s = "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"
      ()
  ;;
Sourceval to_padded_compact_string_custom : t @ local -> ?prefix:Base.string -> kilo:Base.string -> mega:Base.string -> giga:Base.string -> tera:Base.string -> ?peta:Base.string -> Base.unit -> Base.string @@ portable

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

Sourceval int_pow : t @ local -> Base.int -> t @@ portable

int_pow x n computes x ** float32 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 **.

Sourceval square : t -> t @@ portable

square x returns x *. x.

Sourceval ldexp : t @ local -> Base.int -> t @@ portable

ldexp x n returns x *. 2 ** n

Sourceval frexp : t @ local -> t * Base.int @@ portable

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.5s <= x < 1.0s.

Sourceval log10 : t @ local -> t @@ portable

Base 10 logarithm.

Sourceval log2 : t @ local -> t @@ portable

Base 2 logarithm.

Sourceval expm1 : t @ local -> t @@ portable

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

Sourceval log1p : t @ local -> t @@ portable

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

Sourceval copysign : t @ local -> t @ local -> t @@ portable

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.

Sourceval cos : t @ local -> t @@ portable

Cosine. Argument is in radians.

Sourceval sin : t @ local -> t @@ portable

Sine. Argument is in radians.

Sourceval tan : t @ local -> t @@ portable

Tangent. Argument is in radians.

Sourceval acos : t @ local -> t @@ portable

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

Sourceval asin : t @ local -> t @@ portable

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

Sourceval atan : t @ local -> t @@ portable

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

Sourceval atan2 : t @ local -> t @ local -> t @@ portable

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.

Sourceval hypot : t @ local -> t @ local -> t @@ portable

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.

Sourceval cosh : t @ local -> t @@ portable

Hyperbolic cosine. Argument is in radians.

Sourceval sinh : t @ local -> t @@ portable

Hyperbolic sine. Argument is in radians.

Sourceval tanh : t @ local -> t @@ portable

Hyperbolic tangent. Argument is in radians.

Sourceval acosh : t @ local -> t @@ portable

Hyperbolic arc cosine. The argument must fall within the range [1.0s, inf]. Result is in radians and is between 0.0s and inf.

Sourceval asinh : t @ local -> t @@ portable

Hyperbolic arc sine. The argument and result range over the entire real line. Result is in radians.

Sourceval atanh : t @ local -> t @@ portable

Hyperbolic arc tangent. The argument must fall within the range [-1.0s, 1.0s]. Result is in radians and ranges over the entire real line.

Sourceval sqrt : t @ local -> t @@ portable

Square root.

Sourceval cbrt : t @ local -> t @@ portable

Cube root.

Sourceval exp : t @ local -> t @@ portable

Exponential.

Sourceval log : t @ local -> t @@ portable

Natural logarithm.

Sourceval classify : t @ local -> Base.Float.Class.t @@ portable
Sourceval sign : t -> Base.Sign.t @@ portable
  • deprecated [since 2016-01] Replace [sign] with [robust_sign] or [sign_exn]
Sourceval sign_exn : t @ local -> Base.Sign.t @@ portable

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

Sourceval sign_or_nan : t @ local -> Base.Sign_or_nan.t @@ portable

The sign of a float32, 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.

Sourceval create_ieee : negative:Base.bool -> exponent:Base.int -> mantissa:Base.int -> t Base.Or_error.t @@ portable

These functions construct and destruct 32-bit floating point numbers based on their IEEE representation with a sign bit, an 8-bit non-negative (biased) exponent, and a 23-bit non-negative mantissa (or significand). See Wikipedia for details of the encoding.

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

  create_ieee_exn ~negative:false ~exponent ~mantissa
  = (2 ** (exponent - 127)) * (1 + ((2 ** -23) * mantissa))
Sourceval create_ieee_exn : negative:Base.bool -> exponent:Base.int -> mantissa:Base.int -> t @@ portable
Sourceval ieee_negative : t @ local -> Base.bool @@ portable
Sourceval ieee_exponent : t @ local -> Base.int @@ portable
Sourceval ieee_mantissa : t @ local -> Base.int @@ portable
Sourcemodule Terse : sig ... end

S-expressions contain at most 8 significant digits.