Frama-C API - Typed_float
Objectives and limitations
The goal of this module is to provide a representation of floating-point numbers that statically encode the format of the represented number. As for now, the numbers are represented using the OCaml float
type (which are in the binary64
format) and thus, the Long
format (or binary80
) is not yet supported.
The main feature of this module currently used by Frama-C is the parse
function, which returns a witness that specifies the format one would like to use and the one actually used to encode the parsed floating point number.
Floating point formats
Supported floating point formats are declared at the type level to enforce a strict manipulation of floating point numbers.
val fkind_of_format : 'f format -> Cil_types.fkind
Returns the fkind
corresponding to the given format.
Type used to return a proof that two formats are in fact the same.
val same_format : 'l format -> 'r format -> ('l, 'r) same_format
Check if two formats are in fact the same.
Typed floating point numbers
Type of statically formatted floating point numbers. The parameter specifies the number's format.
Build a typed float of the given format given an OCaml 64-bits floating point number.
val to_float : 'f t -> float
Returns the OCaml float represented by a given typed float.
val is_finite : 'f t -> bool
True if the given typed float is finite (not infinite or a NaN).
val is_infinite : 'f t -> bool
True if the given typed float is infinite (but not a NaN).
val is_nan : 'f t -> bool
True if the given typed float is a NaN.
Cast a typed floating point number to a new given format. Rounding operations are performed when needed.
Pretty printers
The function pretty_normal
implements a custom printer. The function pretty
relies on kernel's options to decide between using the OCaml printer or the pretty_normal
one.
val pretty_normal : use_hex:bool -> Stdlib.Format.formatter -> 'f t -> unit
val pretty : Stdlib.Format.formatter -> 'f t -> unit
Parser
Parses a typed float from a string. As the represented float may not be exact, the parser actually returns three typed floats. The lower
one corresponds to the floating point number computed using the Downward
rounding mode. The upper
one is based on the Upward
rounding mode. Finally, the nearest
corresponds to the Nearest_even
rounding mode. As each one of them is typed, the parser must actually returns an existantially quantified record, using the parsed
wrapper type.
type ('format, 'encoded_as) parsed_format =
| Single_supported : (single, single) parsed_format
| Double_supported : (double, double) parsed_format
| Long_unsupported : (long, double) parsed_format
A witness encoding the floating-point format represented by the string, and the format actually used to parse it. For now, Long format is not supported by this module, and is thus approximated using double precision.
val parse : string -> (parsed_result, string) Stdlib.result
Parses the given string and returns Ok Parsed (kind, float)
with the parsed float
and its kind
(single, double or long) according to its suffix, if any. Strings with no suffix are parsed as double. Returns Error msg
if the parsing fails, where msg
is the error message.
val parse_exn : string -> parsed_result
Calls parse
and evaluates the result type.
val parsed_fkind : ('k, 'f) parsed_format -> Cil_types.fkind
Returns the floating-point kind parsed by parse
.
Format based constants
See documentation in Floating_point
.
Comparisons
Correctly rounded arithmetic operations
Correctly rounded arithmetic operations. As stated by the IEEE-754 norm, computing a correctly rounded operation is exactly equivalent to first compute the operation in real arithmetic over the operands and then perform a rounding into the floating point format.
Mathematic functions over floating point numbers
None of those functions are correctly rounded. They behave exactly as their libc equivalents. For each format, the corresponding libc function is actually called. For example, computing the exponential of a floating point number in single precision uses the libc expf
function.
Floating point numbers as integers
Truncate a given typed floating point number, i.e returns the closest integer rounded toward zero, regardless of the current rounding mode. The trunc
function is based on the libc equivalent and never fails.
Rounds a given typed floating point number to the closest integer. Ties are rounded away from zero, regardless of the current rounding mode.