library colibrilib.fp — module Intfmodule Intf243use int.Int244use int.Power245use real.RealInfix246use real.FromInt247use colibrilib.pow2r.Pow2r248use colibrilib.dyadic.Dyadic as D249use int.EuclideanDivision as ED250use Ereal251use export Widen252use export Dir253use option.Option254
The format: eb bits of exponent, sb bits of significand (the hidden bit included), as in ieee_float. It is data, as in Farith: each float carries its format.
type format259function eb format : int260function sb format : int261axiom eb_gt_1: forall fm. 1 < eb fm262axiom sb_gt_1: forall fm. 1 < sb fm263264type t265function fmt t : format266
The format of a float, as data
val format_of (x: t) : format269ensures { result = fmt x }270
Equality of formats, as data
val eq_format (a b: format) : bool273ensures { result <-> a = b }274
The format of e bits of exponent and s of significand, when it has two bits of each
val mk_format_o (e s: int) : option format278ensures { match result with279| Some fm -> eb fm = e /\ sb fm = s280| None -> not (1 < e /\ 1 < s) end }281
The format fm' with the exponent of fm and one more bit of significand contains the floats of fm and the midpoints between two neighbours (ext_widen): the float of index i of fm is at windex i in fm', and windex i + 1 is the midpoint between i and i + 1.
predicate widens (fm fm': format) = eb fm' = eb fm /\ sb fm' = sb fm + 1290
emax of ieee_float: the formats have sb < emax
function emax (fm: format) : int = power 2 (eb fm - 1)293
The format with one more bit, when it is a format
val widen (fm: format) : option format296ensures { match result with297| None -> emax fm <= sb fm + 1298| Some fm' -> widens fm fm' end }299300301val predicate is_nan t302predicate is_not_nan (x: t) = not (is_nan x)303predicate is_plus_infinity t304predicate is_minus_infinity t305306function not_a_number format : t307axiom not_a_number_spec: forall fm [not_a_number fm].308is_nan (not_a_number fm) /\ fmt (not_a_number fm) = fm309
One NaN per format, as in SMT–LIB
axiom nan_unique: forall x [is_nan x]. is_nan x -> x = not_a_number (fmt x)311function plus_infinity format : t312axiom plus_infinity_spec: forall fm [plus_infinity fm].313is_plus_infinity (plus_infinity fm) /\ fmt (plus_infinity fm) = fm314function minus_infinity format : t315axiom minus_infinity_spec: forall fm [minus_infinity fm].316is_minus_infinity (minus_infinity fm) /\ fmt (minus_infinity fm) = fm317
The order where –0 < +0, and its strict version, on the floats of one format
predicate le_dz t t321predicate lt_dz t t322323axiom le_not_nan: forall x y [le_dz x y]. le_dz x y ->324is_not_nan x /\ is_not_nan y /\ fmt x = fmt y325axiom lt_not_nan: forall x y [lt_dz x y]. lt_dz x y ->326is_not_nan x /\ is_not_nan y /\ fmt x = fmt y327328axiom lt_plus_infinity: forall x [lt_dz x (plus_infinity (fmt x))].329is_not_nan x -> not (is_plus_infinity x) -> lt_dz x (plus_infinity (fmt x))330axiom minus_infinity_lt: forall x [lt_dz (minus_infinity (fmt x)) x].331is_not_nan x -> not (is_minus_infinity x) -> lt_dz (minus_infinity (fmt x)) x332
One +inf and one –inf per format, not NaN and different
axiom plus_infinity_unique: forall x [is_plus_infinity x].335is_plus_infinity x -> x = plus_infinity (fmt x)336axiom minus_infinity_unique: forall x [is_minus_infinity x].337is_minus_infinity x -> x = minus_infinity (fmt x)338axiom infinities_not_nan: forall fm.339is_not_nan (plus_infinity fm) /\ is_not_nan (minus_infinity fm)340axiom infinities_differ: forall fm.341not is_minus_infinity (plus_infinity fm) /\342not is_plus_infinity (minus_infinity fm)343
Nothing is above +inf, nothing below –inf
axiom lt_not_plus_infinity: forall x y [lt_dz x y].346lt_dz x y -> not (is_plus_infinity x)347axiom lt_not_minus_infinity: forall x y [lt_dz x y].348lt_dz x y -> not (is_minus_infinity y)349350val le_diff_zero (x y: t) : bool351ensures { result <-> le_dz x y }352353val lt_diff_zero (x y: t) : bool354ensures { result <-> lt_dz x y }355
The floats that are not NaN, ordered by le_dz, are in bijection with [-nb_sign, nb_sign), as in the semantics of SMT–LIB (Brain, Tinelli, Rümmer, Wahl, "An Automatable Formal Semantics for IEEE–754 Floating–Point Arithmetic", 2015, §V.A), with +0 at 0 and –0 at –1. nb_sign is the number of floats of each sign: the zero, the subnormals, the normals and the infinity. On the floats that are not negative, the index is the IEEE binary encoding.
function nb_sign (fm: format) : int =367(power 2 (eb fm) - 1) * power 2 (sb fm - 1) + 1368function index (x: t) : int369function of_index (fm: format) (i: int) : t370371axiom index_le: forall x y [le_dz x y | index x, index y].372is_not_nan x -> is_not_nan y -> fmt x = fmt y ->373(le_dz x y <-> index x <= index y)374375axiom index_lt: forall x y [lt_dz x y | index x, index y].376is_not_nan x -> is_not_nan y -> fmt x = fmt y ->377(lt_dz x y <-> index x < index y)378
Leibniz equality for all the floats that are not NaN, zeros and infinities included
axiom index_inj: forall x [index x].382is_not_nan x -> of_index (fmt x) (index x) = x383384val ghost index_range (x: t) : unit385requires { is_not_nan x }386ensures { - nb_sign (fmt x) <= index x < nb_sign (fmt x) }387388val ghost of_index_range (fm: format) (i: int) : unit389requires { - nb_sign fm <= i < nb_sign fm }390ensures { is_not_nan (of_index fm i) /\ index (of_index fm i) = i }391ensures { fmt (of_index fm i) = fm }392
The index and the float of an index, as data: nb_sign, the number of significands in a binade, and of_index
val nb_sign_v (fm: format) : int396ensures { result = nb_sign fm }397val binade_v (fm: format) : int398ensures { result = power 2 (sb fm - 1) }399val of_index_v (fm: format) (i: int) : t400ensures { result = of_index fm i }401
The index of a float and the widths of a format, as data
val index_v (x: t) : int404requires { is_not_nan x }405ensures { result = index x }406val eb_v (fm: format) : int407ensures { result = eb fm }408val sb_v (fm: format) : int409ensures { result = sb fm }410
The successor and the predecessor, and the float of index the middle of the indices of x and y (rounded down); the properties are the let ghost facts of fp_facts.Facts
function succ_dz (x: t) : t = of_index (fmt x) (index x + 1)415function pred_dz (x: t) : t = of_index (fmt x) (index x - 1)416417val succ_diff_zero (x: t) : t418requires { is_not_nan x /\ not is_plus_infinity x }419ensures { result = succ_dz x }420421val pred_diff_zero (x: t) : t422requires { is_not_nan x /\ not is_minus_infinity x }423ensures { result = pred_dz x }424425val mid_index (x y: t) : t426requires { is_not_nan x /\ is_not_nan y /\ fmt x = fmt y }427ensures { result = of_index (fmt x) (ED.div (index x + index y) 2) }428
The zeros and the signs
predicate is_zero t431predicate is_positive t432433axiom index_zero: forall x [is_zero x].434is_zero x <-> is_not_nan x /\ (index x = 0 \/ index x = -1)435axiom index_positive: forall x [is_positive x].436is_not_nan x -> (is_positive x <-> 0 <= index x)437
The classes by the magnitude: up to the sign bit, the IEEE binary encoding. Subnormal: exponent 0 and significand not 0; normal: exponent neither 0 nor all ones; infinite: the last one.
function magnitude (x: t) : int = if 0 <= index x then index x else -1 - index x442443predicate is_subnormal t444predicate is_normal t445446axiom index_subnormal: forall x [is_subnormal x].447is_subnormal x <-> is_not_nan x /\ 0 < magnitude x < power 2 (sb (fmt x) - 1)448axiom index_normal: forall x [is_normal x].449is_normal x <->450is_not_nan x /\ power 2 (sb (fmt x) - 1) <= magnitude x < nb_sign (fmt x) - 1451axiom index_plus_infinity: forall x [is_plus_infinity x].452is_plus_infinity x <-> is_not_nan x /\ index x = nb_sign (fmt x) - 1453axiom index_minus_infinity: forall x [is_minus_infinity x].454is_minus_infinity x <-> is_not_nan x /\ index x = - nb_sign (fmt x)455
Negation and absolute value: on the index, the reflection i -> -1 - i and the magnitude
val function neg t : t459val function abs t : t460461axiom neg_fmt: forall x [neg x]. fmt (neg x) = fmt x462axiom abs_fmt: forall x [abs x]. fmt (abs x) = fmt x463axiom neg_nan: forall x [neg x]. is_nan (neg x) <-> is_nan x464axiom abs_nan: forall x [abs x]. is_nan (abs x) <-> is_nan x465axiom index_neg: forall x [neg x].466is_not_nan x -> index (neg x) = -1 - index x467axiom index_abs: forall x [abs x].468is_not_nan x -> index (abs x) = magnitude x469
The comparisons of IEEE 754 on the floats of one format: –0 and +0 are equal, NaN is unordered
predicate fp_le (x y: t) = le_dz x y \/ (fmt x = fmt y /\ is_zero x /\ is_zero y)473predicate fp_lt (x y: t) = lt_dz x y /\ not (is_zero x /\ is_zero y)474predicate fp_eq (x y: t) =475(is_not_nan x /\ x = y) \/ (fmt x = fmt y /\ is_zero x /\ is_zero y)476
The minimum and the maximum of SMT–LIB: one of the arguments (the other one if one is NaN; either one for –0 and +0)
function min t t : t480function max t t : t481axiom min_choice: forall x y [min x y]. min x y = x \/ min x y = y482axiom max_choice: forall x y [max x y]. max x y = x \/ max x y = y483
As BTRW15 §V: the operations round the exact result, an extended real (ext: the value of a float that is not NaN), to a float; round_f m zneg e is –0 if zneg and +0 otherwise when e rounds to 0. The rounding modes are abstract, but for RTN (the sign of zero sums).
function to_real t : real491function ext t : ereal492axiom ext_def: forall x [ext x]. ext x =493if is_plus_infinity x then PInf494else if is_minus_infinity x then MInf495else Fin (to_real x)496axiom ext_le: forall x y [le_dz x y]. le_dz x y -> ele (ext x) (ext y)497axiom ext_zero: forall x [ext x]. is_not_nan x -> (is_zero x <-> ext x = Fin 0.0)498axiom ext_neg: forall x [ext (neg x)]. is_not_nan x -> ext (neg x) = eneg (ext x)499
Strictly increasing, but on the zeros
axiom ext_lt: forall x y [lt_dz x y]. lt_dz x y -> not (is_zero x /\ is_zero y) ->501not ele (ext y) (ext x)502
The modes of ieee_float (Widen, extracted with fbin.mlw)
predicate is_rtn (m: mode) = m = RTN505function round_f format mode bool ereal : t506507axiom round_f_not_nan: forall fm m z e [round_f fm m z e].508is_not_nan (round_f fm m z e) /\ fmt (round_f fm m z e) = fm509axiom round_f_infinities: forall fm m z.510round_f fm m z PInf = plus_infinity fm /\511round_f fm m z MInf = minus_infinity fm512axiom round_f_fin_zero: forall fm m z. is_zero (round_f fm m z (Fin 0.0))513axiom round_f_zero: forall fm m z e [round_f fm m z e].514is_zero (round_f fm m z e) -> (is_positive (round_f fm m z e) <-> not z)515
Increasing; when both results are zeros, their signs must be in order
axiom round_f_le: forall fm m z1 z2 e1 e2517[round_f fm m z1 e1, round_f fm m z2 e2].518ele e1 e2 ->519(is_zero (round_f fm m z1 e1) /\ is_zero (round_f fm m z2 e2) -> z2 -> z1) ->520le_dz (round_f fm m z1 e1) (round_f fm m z2 e2)521
Exact on the floats of the format, the zeros with their sign
axiom round_f_exact: forall m x523[round_f (fmt x) m (if is_positive x then false else true) (ext x)].524is_not_nan x ->525round_f (fmt x) m (if is_positive x then false else true) (ext x) = x526
The rounding of an integer, as data (z: the sign of a zero)
val of_int (fm: format) (m: mode) (z: bool) (i: int) : t529ensures { result = round_f fm m z (Fin (from_int i)) }530
The integers of magnitude at most 2^sb and at most the largest finite float are floats (as in ieee_float)
val ghost int_exact (fm: format) (i: int) : t534requires { - power 2 (sb fm) <= i <= power 2 (sb fm) }535requires { -. to_real (of_index fm (nb_sign fm - 2)) <=. from_int i <=.536to_real (of_index fm (nb_sign fm - 2)) }537ensures { fmt result = fm /\ is_not_nan result /\ ext result = Fin (from_int i) }538
The finite floats are floats of the formats with at least as many bits of exponent and of significand
val ghost widen_exact (fm': format) (x: t) : t542requires { eb (fmt x) <= eb fm' /\ sb (fmt x) <= sb fm' }543requires { is_not_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }544ensures { fmt result = fm' /\ is_not_nan result /\ ext result = ext x }545
colibrilib/dyadic.mlw)ulp_min fm: the exponent of the least ulp (of the subnormals); ulp_max fm: the exponent of the ulp of the largest finite float.
function ulp_min (fm: format) : int = 3 - emax fm - sb fm551function ulp_max (fm: format) : int = ulp_min fm + power 2 (eb fm) - 3552
A finite float is a multiple of 2^ulp_min with sb bits (FIX and FLT of Gappa)
val ghost float_dyadic (x: t) : unit556requires { is_not_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }557ensures { D.fix (to_real x) (ulp_min (fmt x)) /\ D.flt (to_real x) (sb (fmt x)) }558
The rounding keeps the multiples of 2^e, e <= ulp_max
val ghost round_fix (fm: format) (m: mode) (z: bool) (r: real) (e: int) : unit561requires { D.fix r e /\ e <= ulp_max fm }562requires { not is_plus_infinity (round_f fm m z (Fin r)) }563requires { not is_minus_infinity (round_f fm m z (Fin r)) }564ensures { D.fix (to_real (round_f fm m z (Fin r))) e }565
A nonzero real that rounds to zero is below 2^ulp_min
val ghost round_zero_small (fm: format) (m: mode) (z: bool) (r: real) : unit568requires { r <> 0.0 /\ is_zero (round_f fm m z (Fin r)) }569ensures { -. pow2r (ulp_min fm) <. r <. pow2r (ulp_min fm) }570
A multiple of 2^ulp_min with sb bits, at most the largest finite float, is a float (Flocq's generic_format_FLT, src/Core/FLT.v)
val ghost dyadic_exact (fm: format) (r: real) : t574requires { D.fix r (ulp_min fm) /\ D.flt r (sb fm) }575requires { -. to_real (of_index fm (nb_sign fm - 2)) <=. r <=.576to_real (of_index fm (nb_sign fm - 2)) }577ensures { fmt result = fm /\ is_not_nan result /\ ext result = Fin r }578
The largest finite float is at least 2^(ulp_max + sb - 1) (its significand has sb bits)
val ghost max_float_ge (fm: format) : unit582ensures { pow2r (ulp_max fm + sb fm - 1) <=. to_real (of_index fm (nb_sign fm - 2)) }583
In round to nearest, a real at least 2 x that rounds below the infinity bounds 2 x by the largest finite float (the gap between it and the overflow threshold), and the same on the negative side (average_2 of QF_FP/schanda/spark)
val ghost near_double (m: mode) (z: bool) (x: t) (r: real) : unit589requires { m = RNE \/ m = RNA }590requires { is_not_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }591ensures { 2.0 *. to_real x <=. r ->592not is_plus_infinity (round_f (fmt x) m z (Fin r)) ->5932.0 *. to_real x <=. to_real (of_index (fmt x) (nb_sign (fmt x) - 2)) }594ensures { r <=. 2.0 *. to_real x ->595not is_minus_infinity (round_f (fmt x) m z (Fin r)) ->596-. to_real (of_index (fmt x) (nb_sign (fmt x) - 2)) <=. 2.0 *. to_real x }597
The significand and the exponent of a finite float: n 2^e (the others give any pair)
val to_dyadic (x: t) : (int, int)601ensures { let (n, e) = result in602is_not_nan x -> not is_plus_infinity x -> not is_minus_infinity x ->603to_real x = from_int n *. pow2r e }604
The linear bounds a r + b <= f <= c r + d of a rounding f to nearest of a real r, when f is in [lo, hi] (fbin.Nearest.nearest_bounds): `b = ((am, ae), ad, (bm, be), (cm, ce), cd, (dm, de)) gives a = am 2^ae / ad, b = bm 2^be`, c = cm 2^ce / cd, d = dm 2^de
val nearest_bounds (lo hi: t) : option ((int, int), int, (int, int), (int, int), int, (int, int))611ensures { match result with612| Some b ->613let ((am, ae), ad, (bm, be), (cm, ce), cd, (dm, de)) = b in6140 < ad /\ 0 < cd /\615forall m z r. (m = RNE \/ m = RNA) ->616le_dz lo (round_f (fmt lo) m z (Fin r)) ->617le_dz (round_f (fmt lo) m z (Fin r)) hi ->618from_int am *. pow2r ae *. r +. from_int ad *. (from_int bm *. pow2r be)619<=. from_int ad *. to_real (round_f (fmt lo) m z (Fin r)) /\620from_int cd *. to_real (round_f (fmt lo) m z (Fin r))621<=. from_int cm *. pow2r ce *. r +. from_int cd *. (from_int dm *. pow2r de)622| None -> true end }623624axiom round_f_down: forall fm z e [round_f fm rtn z e].625ele (ext (round_f fm rtn z e)) e626axiom round_f_up: forall fm z e [round_f fm rtp z e].627ele e (ext (round_f fm rtp z e))628
In the format fm' with one more bit, the floats of fm are at windex (ext_widen). The reals that round to a or above are at or above a (RTN, RTZ above 0), its predecessor (RTP, RTZ below 0), or the midpoint between them (nearest); a is not NaN, not –inf and not +0 (–0 instead). Whether the bound itself rounds to a or above is found by rounding it.
axiom ext_widen: forall fm fm' i. widens fm fm' -> - nb_sign fm <= i < nb_sign fm ->638ext (of_index fm' (windex i)) = ext (of_index fm i)639640641axiom round_f_ge_thr: forall fm fm' m z e a [widens fm fm', le_dz a (round_f fm m z e)].642widens fm fm' -> fmt a = fm -> is_not_nan a -> not is_minus_infinity a ->643index a <> 0 -> le_dz a (round_f fm m z e) ->644ele (ext (of_index fm' (windex (index a) + lo_off m (index a)))) e645
The reals that round to b or below, b not NaN, not +inf, not –0
axiom round_f_le_thr: forall fm fm' m z e b [widens fm fm', le_dz (round_f fm m z e) b].648widens fm fm' -> fmt b = fm -> is_not_nan b -> not is_plus_infinity b ->649index b <> -1 -> le_dz (round_f fm m z e) b ->650ele e (ext (of_index fm' (windex (index b) + hi_off m (index b))))651
The sign of a zero sum (IEEE 754 §6.3): the sign of the arguments when they have the same sign, else –0 in RTN only
function add_zneg (m: mode) (x y: t) : bool =657if is_positive x <-> is_positive y then (if is_positive x then false else true)658else (if is_rtn m then true else false)659
The addition of BTRW15 §V: NaN, or the rounding of the sum of the extended reals, in the format of x
function fp_add (m: mode) (x y: t) : t =663if is_nan x \/ is_nan y \/ opposite (ext x) (ext y) then not_a_number (fmt x)664else round_f (fmt x) m (add_zneg m x y) (eadd (ext x) (ext y))665
Implemented by F.add of Farith, for two floats of the same format
val add (m: mode) (x y: t) : t668requires { fmt x = fmt y }669ensures { result = fp_add m x y }670
predicate is_infinity (x: t) = is_plus_infinity x \/ is_minus_infinity x674
The least (resp. greatest) finite float above (resp. below) a real r, strictly when strict, from the rounding f of r up (resp. down) and exact, whether f is finite of value r
val float_lo (f: t) (strict exact: bool) : t679ensures { fmt result = fmt f }680ensures { forall r. f = round_f (fmt f) RTP (r <. 0.0) (Fin r) ->681(exact <-> is_not_nan f /\ not is_infinity f /\ to_real f = r) ->682forall v. fmt v = fmt f -> is_not_nan v -> not is_infinity v ->683(if strict then r <. to_real v else r <=. to_real v) -> le_dz result v }684val float_hi (f: t) (strict exact: bool) : t685ensures { fmt result = fmt f }686ensures { forall r. f = round_f (fmt f) RTN (r <. 0.0) (Fin r) ->687(exact <-> is_not_nan f /\ not is_infinity f /\ to_real f = r) ->688forall v. fmt v = fmt f -> is_not_nan v -> not is_infinity v ->689(if strict then to_real v <. r else to_real v <=. r) -> le_dz v result }690
The bounds of x when x + y rounds into [lo, hi], in any mode (None: no bound)
fp_add, as a symbol that the implementation instantiates: the refinement of add_bounds then matches its specification syntactically (the axiom is a goal of the refinement)
function fp_add_i (m: mode) (x y: t) : t697axiom fp_add_i_def: forall m x y [fp_add_i m x y]. fp_add_i m x y = fp_add m x y698699val add_bounds (lo hi: t) : option (t, t)700ensures { match result with701| Some (a, b) -> fmt a = fmt lo /\ fmt b = fmt lo /\702forall m x y. fmt x = fmt lo -> fmt y = fmt lo ->703le_dz lo (fp_add_i m x y) -> le_dz (fp_add_i m x y) hi ->704le_dz a x /\ le_dz x b705| None -> true706end }707
The sign of a product (IEEE 754 §6.3), zeros included: negative when the signs differ
function mul_zneg (x y: t) : bool =711if is_positive x <-> is_positive y then false else true712
The multiplication of BTRW15 §V
function fp_mul (m: mode) (x y: t) : t =715if is_nan x \/ is_nan y \/ (is_zero x /\ is_infinity y)716\/ (is_infinity x /\ is_zero y) then not_a_number (fmt x)717else round_f (fmt x) m (mul_zneg x y) (emul (ext x) (ext y))718
Implemented by F.mul of Farith, for two floats of the same format
val mul (m: mode) (x y: t) : t721requires { fmt x = fmt y }722ensures { result = fp_mul m x y }723
The division of BTRW15 §V; the sign of the quotient is the sign of the product, also for a zero divisor
function fp_div (m: mode) (x y: t) : t =729if is_nan x \/ is_nan y \/ (is_zero x /\ is_zero y)730\/ (is_infinity x /\ is_infinity y) then not_a_number (fmt x)731else if is_zero y then732(if mul_zneg x y then minus_infinity (fmt x) else plus_infinity (fmt x))733else round_f (fmt x) m (mul_zneg x y) (ediv (ext x) (ext y))734
Implemented by F.div of Farith, for two floats of the same format
val div (m: mode) (x y: t) : t737requires { fmt x = fmt y }738ensures { result = fp_div m x y }739
The square root of BTRW15 §V: NaN below –0, the zeros keep their sign
function fp_sqrt (m: mode) (x: t) : t =745if is_nan x \/ (not is_positive x /\ not is_zero x) then not_a_number (fmt x)746else round_f (fmt x) m (if is_positive x then false else true) (esqrt (ext x))747
Implemented by F.sqrt of Farith
val sqrt (m: mode) (x: t) : t750ensures { result = fp_sqrt m x }751
The conversion to the format fm (to_fp of SMT–LIB from a float): NaN to NaN, a zero result has the sign of the argument
function fp_convert (fm: format) (m: mode) (x: t) : t =757if is_nan x then not_a_number fm758else round_f fm m (if is_positive x then false else true) (ext x)759
Implemented by F.round of Farith
val convert (fm: format) (m: mode) (x: t) : t762ensures { result = fp_convert fm m x }763
The rounding of a real to an integer in the mode
function round_int mode real : int768axiom round_int_monotonic: forall m x y. x <=. y -> round_int m x <= round_int m y769770function eround_int (m: mode) (e: ereal) : ereal =771match e with Fin r -> Fin (from_int (round_int m r)) | _ -> e end772
roundToIntegral: NaN to NaN, a zero result has the sign of the argument
function fp_rti (m: mode) (x: t) : t =776if is_nan x then not_a_number (fmt x)777else round_f (fmt x) m (if is_positive x then false else true) (eround_int m (ext x))778
Implemented by F.round_to_integral of colibri2
val round_to_integral (m: mode) (x: t) : t781ensures { result = fp_rti m x }782
The reals r with round_int m r >= n are at or above int_thr_lo m n, the ones with round_int m r <= n at or below int_thr_hi m n (the ties and the strict bounds of the directed modes are included)
function int_thr_lo (m: mode) (n: int) : real =789match m with790| RTN -> from_int n791| RTP -> from_int n -. 1.0792| RTZ -> if 0 < n then from_int n else from_int n -. 1.0793| RNE | RNA -> from_int n -. 0.5794end795796function int_thr_hi (m: mode) (n: int) : real =797match m with798| RTN -> from_int n +. 1.0799| RTP -> from_int n800| RTZ -> if n < 0 then from_int n else from_int n +. 1.0801| RNE | RNA -> from_int n +. 0.5802end803804val ghost round_int_ge (m: mode) (r: real) (n: int) : unit805requires { n <= round_int m r }806ensures { int_thr_lo m n <=. r }807808val ghost round_int_le (m: mode) (r: real) (n: int) : unit809requires { round_int m r <= n }810ensures { r <=. int_thr_hi m n }811
round_int RTP r is the least integer at or above r, round_int RTN r the greatest at or below
val ghost round_int_ceil (r: real) (k: int) : unit815requires { r <=. from_int k }816ensures { round_int RTP r <= k }817818val ghost round_int_floor (r: real) (k: int) : unit819requires { from_int k <=. r }820ensures { k <= round_int RTN r }821
round_int of a finite float, as data
val round_int_v (m: mode) (x: t) : int824requires { is_not_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }825ensures { result = round_int m (to_real x) }826
The rounding of the rational n / d
val of_q (fm: format) (m: mode) (z: bool) (n d: int) : t829requires { 0 < d }830ensures { result = round_f fm m z (Fin (from_int n /. from_int d)) }831
The float to which every real of [n1/d1, n2/d2] rounds, when the two ends round to the same float (a zero result is –0 when the real is negative)
val round_bracket (fm: format) (m: mode) (n1 d1 n2 d2: int) : option t836ensures { match result with837| None -> d1 <= 0 \/ d2 <= 0 \/838round_f fm m (n1 < 0) (Fin (from_int n1 /. from_int d1))839<> round_f fm m (n2 < 0) (Fin (from_int n2 /. from_int d2))840| Some x -> forall r. from_int n1 /. from_int d1 <=. r <=. from_int n2 /. from_int d2 ->841x = round_f fm m (r <. 0.0) (Fin r)842end }843
The float to which every real strictly between n/2^k and (n+1)/2^k rounds, when 2^-k is at most half the least positive float (emax fm + sb fm - 2 = 1 - qmin): no float nor midpoint of two floats is strictly between them
val round_between (fm: format) (m: mode) (n k: int) : t849requires { emax fm + sb fm - 2 <= k }850ensures { forall r. from_int n /. from_int (power 2 k) <. r <. from_int (n + 1) /. from_int (power 2 k) ->851result = round_f fm m (r <. 0.0) (Fin r) }852end853