Common — library colibrilib.fbin — module Float

The floats and the rounding

182
module Float
183
use int.Int
184
use int.Power
185
use int.EuclideanDivision
186
use real.RealInfix
187
use real.FromInt
188
use colibrilib.fp.Widen
189
use colibrilib.fp.Ereal as E
190
use Pow2r
191
use Format

Finite neg k: the sign (true for negative) and the magnitude

194
type
value
=
NaN
|
PlusInf
|
MinusInf
|
Finite
bool int
195
196
predicate
valid
(fm: format) (v: value) =
197
match v with Finite _ k -> 0 <= k < nb_sign fm - 1 | _ -> true end
198
Valid (13ms) (colibri2)
199
type
t
= {
fmt
: format;
value
: value }
200
invariant { valid fmt value }
201
by { fmt = { eb = 2; sb = 2 }; value = NaN }
202
Valid (13ms) (colibri2)
203
let function
mk
(fm: format) (v: value) : t
204
requires { valid fm v }
205
ensures { result.fmt = fm /\ result.value = v }
206
= { fmt = fm; value = v }

The mode on the magnitudes of the negative reals

209
let function
mirror
(m: mode) : mode =
210
match m with RTP -> RTN | RTN -> RTP | _ -> m end

The magnitude of the rounding of r >= 0, without overflow

213
function
round_mag
(fm: format) (m: mode) (r: real) : int =
214
let k = kdn fm r in
215
if vom fm k = r then k else
216
let mid = (vom fm k +. vom fm (k + 1)) /. 2.0 in
217
match m with
218
| RTN | RTZ -> k
219
| RTP -> k + 1
220
| RNE -> if r <. mid then k else if mid <. r then k + 1
221
else if mod k 2 = 0 then k else k + 1
222
| RNA -> if r <. mid then k else k + 1
223
end

The float of the magnitude k >= 0, rounded in the mode m of the magnitudes: on overflow, the largest finite float toward zero, else the infinity (IEEE 754 §7.4)

228
function
of_mag
(fm: format) (m: mode) (neg: bool) (k: int) : t =
229
if k < nb_sign fm - 1 then mk fm (Finite neg k)
230
else match m with
231
| RTN | RTZ -> mk fm (Finite neg (nb_sign fm - 2))
232
| _ -> mk fm (if neg then MinusInf else PlusInf)
233
end

The rounding of a real; a zero result is –0 if zneg

236
function
round_fin
(fm: format) (m: mode) (zneg: bool) (r: real) : t =
237
if 0.0 <=. r then
238
let k = round_mag fm m r in
239
if k = 0 then mk fm (Finite zneg 0) else of_mag fm m false k
240
else
241
let k = round_mag fm (mirror m) (-. r) in
242
if k = 0 then mk fm (Finite zneg 0) else of_mag fm (mirror m) true k

The real of a finite float (0 for the others)

245
function
to_real
(x: t) : real =
246
match x.value with
247
| Finite neg k -> if neg then -. vom x.fmt k else vom x.fmt k
248
| _ -> 0.0
249
end
250
251
function
round_f
(fm: format) (m: mode) (zneg: bool) (e: E.ereal) : t =
252
match e with
253
| E.Fin r -> round_fin fm m zneg r
254
| E.PInf -> mk fm PlusInf
255
| E.MInf -> mk fm MinusInf
256
end
257
end