github ↗
docs menu

Module Munin_float_text.Float_print

Floats as decimal text, the same on every system.

OCaml's Printf writes a float with the C library's printf, whose text differs between systems: under mingw-w64, msvcrt writes three exponent digits, keeps 17 digits and rounds from those, and rounds an exact tie away from zero. These functions lay out the digits Float_text computes from a float's exact decimal value, rounded once, to nearest, ties to even, as C's printf does where it is correctly rounded: 135.5625 to six digits is 135.562. A NaN is nan whatever its sign, and the infinities inf and -inf; a negative zero keeps its sign.

#val g : ?precision:int -> float -> string

g ~precision x is x as C's %.*g writes it, with precision significant digits, 6 by default and at least 1: positional for a decimal exponent from -4 to below precision, otherwise with an exponent of at least two digits, without trailing zeros, as 135.562, 1e+16 and 1.5e-05.

#val f : int -> float -> string

f decimals x is x as C's %.*f writes it, with decimals digits after the point, at least 0: 0.12 for 0.125 at two.

#val exact : float -> string

exact x is the decimal of fewest digits that reads back as x, to nearest, ties to even, the nearest to x of those (Float_text.shortest), written as g writes it at the larger of 15 and its number of digits: 0.1, 0.30000000000000004, 1e+15 and 5e-324.

#val exact_float : float -> string

exact_float x is exact x with .0 added when it has neither a fraction nor an exponent, as 1.0 and -0.0, so that a reader that tells integers from floats by their text reads a float.