Fcore_generic_fmt.v 42.7 KB
Newer Older
1
(**
2 3 4
This file is part of the Flocq formalization of floating-point
arithmetic in Coq: http://flocq.gforge.inria.fr/

BOLDO Sylvie's avatar
BOLDO Sylvie committed
5
Copyright (C) 2010-2011 Sylvie Boldo
6
#<br />#
BOLDO Sylvie's avatar
BOLDO Sylvie committed
7
Copyright (C) 2010-2011 Guillaume Melquiond
8 9 10 11 12 13 14 15 16 17 18 19

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
COPYING file for more details.
*)

20
(** * What is a real number belonging to a format, and many properties. *)
21 22 23 24
Require Import Fcore_Raux.
Require Import Fcore_defs.
Require Import Fcore_rnd.
Require Import Fcore_float_prop.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
25

26
Section Generic.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
27 28 29

Variable beta : radix.

30
Notation bpow e := (bpow beta e).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
31

32 33
Section Format.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
34 35
Variable fexp : Z -> Z.

36
(** To be a good fexp *)
37 38 39

Class Valid_exp :=
  valid_exp :
40 41 42 43 44 45
  forall k : Z,
  ( (fexp k < k)%Z -> (fexp (k + 1) <= k)%Z ) /\
  ( (k <= fexp k)%Z ->
    (fexp (fexp k + 1) <= fexp k)%Z /\
    forall l : Z, (l <= fexp k)%Z -> fexp l = fexp k ).

46
Context { valid_exp_ : Valid_exp }.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
47

BOLDO Sylvie's avatar
BOLDO Sylvie committed
48
Definition canonic_exp x :=
49
  fexp (ln_beta beta x).
50 51

Definition canonic (f : float beta) :=
BOLDO Sylvie's avatar
BOLDO Sylvie committed
52
  Fexp f = canonic_exp (F2R f).
53

54
Definition scaled_mantissa x :=
BOLDO Sylvie's avatar
BOLDO Sylvie committed
55
  (x * bpow (- canonic_exp x))%R.
56

Guillaume Melquiond's avatar
Guillaume Melquiond committed
57
Definition generic_format (x : R) :=
BOLDO Sylvie's avatar
BOLDO Sylvie committed
58
  x = F2R (Float beta (Ztrunc (scaled_mantissa x)) (canonic_exp x)).
59

60
(** Basic facts *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
61 62 63
Theorem generic_format_0 :
  generic_format 0.
Proof.
64
unfold generic_format, scaled_mantissa.
65 66 67 68 69
rewrite Rmult_0_l.
change (Ztrunc 0) with (Ztrunc (Z2R 0)).
now rewrite Ztrunc_Z2R, F2R_0.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
70
Theorem canonic_exp_opp :
71
  forall x,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
72
  canonic_exp (-x) = canonic_exp x.
73 74
Proof.
intros x.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
75
unfold canonic_exp.
76
now rewrite ln_beta_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
77 78
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
79
Theorem canonic_exp_abs :
80
  forall x,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
81
  canonic_exp (Rabs x) = canonic_exp x.
82 83
Proof.
intros x.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
84
unfold canonic_exp.
85 86 87
now rewrite ln_beta_abs.
Qed.

88 89 90 91 92
Theorem generic_format_bpow :
  forall e, (fexp (e + 1) <= e)%Z ->
  generic_format (bpow e).
Proof.
intros e H.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
93
unfold generic_format, scaled_mantissa, canonic_exp.
94
rewrite ln_beta_bpow.
95
rewrite <- bpow_plus.
96 97 98 99 100
rewrite <- (Z2R_Zpower beta (e + - fexp (e + 1))).
rewrite Ztrunc_Z2R.
rewrite <- F2R_bpow.
rewrite F2R_change_exp with (1 := H).
now rewrite Zmult_1_l.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
now apply Zle_minus_le_0.
Qed.

Theorem generic_format_bpow' :
  forall e, (fexp e <= e)%Z ->
  generic_format (bpow e).
Proof.
intros e He.
apply generic_format_bpow.
destruct (Zle_lt_or_eq _ _ He).
now apply valid_exp.
rewrite <- H.
apply valid_exp_.
rewrite H.
apply Zle_refl.
116 117
Qed.

118
Theorem generic_format_F2R :
119
  forall m e,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
120
  ( m <> 0 -> canonic_exp (F2R (Float beta m e)) <= e )%Z ->
121 122 123
  generic_format (F2R (Float beta m e)).
Proof.
intros m e.
124 125 126 127
destruct (Z_eq_dec m 0) as [Zm|Zm].
intros _.
rewrite Zm, F2R_0.
apply generic_format_0.
128
unfold generic_format, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
129
set (e' := canonic_exp (F2R (Float beta m e))).
130
intros He.
131
specialize (He Zm).
132
unfold F2R at 3. simpl.
133 134 135 136
rewrite  F2R_change_exp with (1 := He).
apply F2R_eq_compat.
rewrite Rmult_assoc, <- bpow_plus, <- Z2R_Zpower, <- Z2R_mult.
now rewrite Ztrunc_Z2R.
137 138 139 140 141 142 143 144 145 146
now apply Zle_left.
Qed.

Theorem canonic_opp :
  forall m e,
  canonic (Float beta m e) ->
  canonic (Float beta (-m) e).
Proof.
intros m e H.
unfold canonic.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
147
now rewrite F2R_Zopp, canonic_exp_opp.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
Qed.

Theorem canonic_unicity :
  forall f1 f2,
  canonic f1 ->
  canonic f2 ->
  F2R f1 = F2R f2 ->
  f1 = f2.
Proof.
intros (m1, e1) (m2, e2).
unfold canonic. simpl.
intros H1 H2 H.
rewrite H in H1.
rewrite <- H2 in H1. clear H2.
rewrite H1 in H |- *.
apply (f_equal (fun m => Float beta m e2)).
apply F2R_eq_reg with (1 := H).
Qed.

167
Theorem scaled_mantissa_generic :
168 169
  forall x,
  generic_format x ->
170
  scaled_mantissa x = Z2R (Ztrunc (scaled_mantissa x)).
171 172
Proof.
intros x Hx.
173
unfold scaled_mantissa.
174 175
pattern x at 1 3 ; rewrite Hx.
unfold F2R. simpl.
176
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
177 178 179
now rewrite Ztrunc_Z2R.
Qed.

180
Theorem scaled_mantissa_mult_bpow :
181
  forall x,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
182
  (scaled_mantissa x * bpow (canonic_exp x))%R = x.
183 184 185
Proof.
intros x.
unfold scaled_mantissa.
186
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
187 188 189
apply Rmult_1_r.
Qed.

190 191 192 193 194 195
Theorem scaled_mantissa_0 :
  scaled_mantissa 0 = R0.
Proof.
apply Rmult_0_l.
Qed.

196 197 198 199 200 201
Theorem scaled_mantissa_opp :
  forall x,
  scaled_mantissa (-x) = (-scaled_mantissa x)%R.
Proof.
intros x.
unfold scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
202
rewrite canonic_exp_opp.
203 204 205
now rewrite Ropp_mult_distr_l_reverse.
Qed.

206 207 208 209 210 211
Theorem scaled_mantissa_abs :
  forall x,
  scaled_mantissa (Rabs x) = Rabs (scaled_mantissa x).
Proof.
intros x.
unfold scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
212
rewrite canonic_exp_abs, Rabs_mult.
213 214 215 216 217 218
apply f_equal.
apply sym_eq.
apply Rabs_pos_eq.
apply bpow_ge_0.
Qed.

219 220 221 222 223
Theorem generic_format_opp :
  forall x, generic_format x -> generic_format (-x).
Proof.
intros x Hx.
unfold generic_format.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
224
rewrite scaled_mantissa_opp, canonic_exp_opp.
225
rewrite Ztrunc_opp.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
226
rewrite F2R_Zopp.
227
now apply f_equal.
228 229
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
230 231 232 233 234
Theorem generic_format_abs :
  forall x, generic_format x -> generic_format (Rabs x).
Proof.
intros x Hx.
unfold generic_format.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
235
rewrite scaled_mantissa_abs, canonic_exp_abs.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
236
rewrite Ztrunc_abs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
237
rewrite F2R_Zabs.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
238 239 240
now apply f_equal.
Qed.

241 242 243 244 245 246
Theorem generic_format_abs_inv :
  forall x, generic_format (Rabs x) -> generic_format x.
Proof.
intros x.
unfold generic_format, Rabs.
case Rcase_abs ; intros _.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
247
rewrite scaled_mantissa_opp, canonic_exp_opp, Ztrunc_opp.
248 249
intros H.
rewrite <- (Ropp_involutive x) at 1.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
250
rewrite H, F2R_Zopp.
251 252 253 254
apply Ropp_involutive.
easy.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
255
Theorem canonic_exp_fexp :
256
  forall x ex,
257
  (bpow (ex - 1) <= Rabs x < bpow ex)%R ->
BOLDO Sylvie's avatar
BOLDO Sylvie committed
258
  canonic_exp x = fexp ex.
259 260
Proof.
intros x ex Hx.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
261
unfold canonic_exp.
262 263 264
now rewrite ln_beta_unique with (1 := Hx).
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
265
Theorem canonic_exp_fexp_pos :
266
  forall x ex,
267
  (bpow (ex - 1) <= x < bpow ex)%R ->
BOLDO Sylvie's avatar
BOLDO Sylvie committed
268
  canonic_exp x = fexp ex.
269 270
Proof.
intros x ex Hx.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
271
apply canonic_exp_fexp.
272 273
rewrite Rabs_pos_eq.
exact Hx.
274 275 276 277
apply Rle_trans with (2 := proj1 Hx).
apply bpow_ge_0.
Qed.

278
(** Properties when the real number is "small" (kind of subnormal) *)
279 280 281 282 283 284 285
Theorem mantissa_small_pos :
  forall x ex,
  (bpow (ex - 1) <= x < bpow ex)%R ->
  (ex <= fexp ex)%Z ->
  (0 < x * bpow (- fexp ex) < 1)%R.
Proof.
intros x ex Hx He.
286
split.
287 288 289 290 291 292
apply Rmult_lt_0_compat.
apply Rlt_le_trans with (2 := proj1 Hx).
apply bpow_gt_0.
apply bpow_gt_0.
apply Rmult_lt_reg_r with (bpow (fexp ex)).
apply bpow_gt_0.
293
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_l.
294 295
rewrite Rmult_1_r, Rmult_1_l.
apply Rlt_le_trans with (1 := proj2 Hx).
296
now apply bpow_le.
297 298
Qed.

299 300 301 302 303 304 305 306 307 308 309 310
Theorem scaled_mantissa_small :
  forall x ex,
  (Rabs x < bpow ex)%R ->
  (ex <= fexp ex)%Z ->
  (Rabs (scaled_mantissa x) < 1)%R.
Proof.
intros x ex Ex He.
destruct (Req_dec x 0) as [Zx|Zx].
rewrite Zx, scaled_mantissa_0, Rabs_R0.
now apply (Z2R_lt 0 1).
rewrite <- scaled_mantissa_abs.
unfold scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
311 312
rewrite canonic_exp_abs.
unfold canonic_exp.
313 314 315 316 317 318 319 320
destruct (ln_beta beta x) as (ex', Ex').
simpl.
specialize (Ex' Zx).
apply (mantissa_small_pos _ _ Ex').
assert (ex' <= fexp ex)%Z.
apply Zle_trans with (2 := He).
apply bpow_lt_bpow with beta.
now apply Rle_lt_trans with (2 := Ex).
321
now rewrite (proj2 (proj2 (valid_exp _) He)).
322 323
Qed.

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
Theorem mantissa_DN_small_pos :
  forall x ex,
  (bpow (ex - 1) <= x < bpow ex)%R ->
  (ex <= fexp ex)%Z ->
  Zfloor (x * bpow (- fexp ex)) = Z0.
Proof.
intros x ex Hx He.
apply Zfloor_imp. simpl.
assert (H := mantissa_small_pos x ex Hx He).
split ; try apply Rlt_le ; apply H.
Qed.

Theorem mantissa_UP_small_pos :
  forall x ex,
  (bpow (ex - 1) <= x < bpow ex)%R ->
  (ex <= fexp ex)%Z ->
  Zceil (x * bpow (- fexp ex)) = 1%Z.
Proof.
intros x ex Hx He.
apply Zceil_imp. simpl.
assert (H := mantissa_small_pos x ex Hx He).
split ; try apply Rlt_le ; apply H.
346 347
Qed.

348
(** Generic facts about any format *)
349 350
Theorem generic_format_discrete :
  forall x m,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
351
  let e := canonic_exp x in
352 353 354 355 356 357 358 359 360 361 362 363 364
  (F2R (Float beta m e) < x < F2R (Float beta (m + 1) e))%R ->
  ~ generic_format x.
Proof.
intros x m e (Hx,Hx2) Hf.
apply Rlt_not_le with (1 := Hx2). clear Hx2.
rewrite Hf.
fold e.
apply F2R_le_compat.
apply Zlt_le_succ.
apply lt_Z2R.
rewrite <- scaled_mantissa_generic with (1 := Hf).
apply Rmult_lt_reg_r with (bpow e).
apply bpow_gt_0.
365
now rewrite scaled_mantissa_mult_bpow.
366 367
Qed.

368 369 370 371 372 373
Theorem generic_format_canonic :
  forall f, canonic f ->
  generic_format (F2R f).
Proof.
intros (m, e) Hf.
unfold canonic in Hf. simpl in Hf.
374
unfold generic_format, scaled_mantissa.
375
rewrite <- Hf.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
376
apply F2R_eq_compat.
377
unfold F2R. simpl.
378
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
379 380 381
now rewrite Ztrunc_Z2R.
Qed.

382 383 384 385 386 387 388 389 390 391 392 393 394
Theorem generic_format_ge_bpow :
  forall emin,
  ( forall e, (emin <= fexp e)%Z ) ->
  forall x,
  (0 < x)%R ->
  generic_format x ->
  (bpow emin <= x)%R.
Proof.
intros emin Emin x Hx Fx.
rewrite Fx.
apply Rle_trans with (bpow (fexp (ln_beta beta x))).
now apply bpow_le.
apply bpow_le_F2R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
395
apply F2R_gt_0_reg with beta (canonic_exp x).
396 397 398
now rewrite <- Fx.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
399
Theorem abs_lt_bpow_prec:
400
  forall prec,
BOLDO Sylvie's avatar
BOLDO Sylvie committed
401
  (forall e, (e - prec <= fexp e)%Z) ->
402
  (* OK with FLX, FLT and FTZ *)
BOLDO Sylvie's avatar
BOLDO Sylvie committed
403 404 405
  forall x, 
  (Rabs x < bpow (prec + canonic_exp x))%R.
intros prec Hp x.
406 407 408
case (Req_dec x 0); intros Hxz.
rewrite Hxz, Rabs_R0.
apply bpow_gt_0.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
409
unfold canonic_exp.
410 411 412
destruct (ln_beta beta x) as (ex,Ex) ; simpl.
specialize (Ex Hxz).
apply Rlt_le_trans with (1 := proj2 Ex).
413
apply bpow_le.
414
specialize (Hp ex).
415 416 417
omega.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
418 419 420 421 422 423 424 425 426 427
Theorem generic_format_bpow_inv :
  forall e,
    generic_format (bpow e) ->
   (fexp e <= e)%Z.
Proof.
intros e He.
apply Znot_gt_le; intros He2.
assert (e+1 <= fexp (e+1))%Z.
replace (fexp (e+1)) with (fexp e).
omega.
428
destruct (valid_exp e) as (Y1,Y2).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
429 430 431 432 433 434 435 436 437
apply sym_eq; apply Y2; omega.
absurd (bpow e=0)%R.
apply sym_not_eq; apply Rlt_not_eq.
apply bpow_gt_0.
rewrite He.
replace (Ztrunc (scaled_mantissa (bpow e))) with 0%Z.
apply F2R_0.
apply sym_eq.
rewrite Ztrunc_floor.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
438
unfold scaled_mantissa, canonic_exp.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
439 440 441 442 443 444
apply mantissa_DN_small_pos; trivial.
rewrite ln_beta_bpow.
split.
apply Req_le.
apply f_equal.
ring.
445
apply bpow_lt.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
446 447 448 449 450 451
omega.
now rewrite ln_beta_bpow.
unfold scaled_mantissa.
apply Rmult_le_pos; apply bpow_ge_0.
Qed.

452
Section Fcore_generic_round_pos.
453

BOLDO Sylvie's avatar
BOLDO Sylvie committed
454
(** Rounding functions: R -> Z *)
455 456 457 458

Variable rnd : R -> Z.

Class Valid_rnd := {
459
  Zrnd_le : forall x y, (x <= y)%R -> (rnd x <= rnd y)%Z ;
460
  Zrnd_Z2R : forall n, rnd (Z2R n) = n
461 462
}.

463
Context { valid_rnd : Valid_rnd }.
464

465
Theorem Zrnd_DN_or_UP :
466
  forall x, rnd x = Zfloor x \/ rnd x = Zceil x.
467
Proof.
468
intros x.
469
destruct (Zle_or_lt (rnd x) (Zfloor x)) as [Hx|Hx].
470 471
left.
apply Zle_antisym with (1 := Hx).
472
rewrite <- (Zrnd_Z2R (Zfloor x)).
473
apply Zrnd_le.
474 475 476
apply Zfloor_lb.
right.
apply Zle_antisym.
477
rewrite <- (Zrnd_Z2R (Zceil x)).
478
apply Zrnd_le.
479 480 481 482 483 484 485 486 487
apply Zceil_ub.
rewrite Zceil_floor_neq.
omega.
intros H.
rewrite <- H in Hx.
rewrite Zfloor_Z2R, Zrnd_Z2R in Hx.
apply Zlt_irrefl with (1 := Hx).
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
488
(** the most useful one: R -> F *)
489
Definition round x :=
BOLDO Sylvie's avatar
BOLDO Sylvie committed
490
  F2R (Float beta (rnd (scaled_mantissa x)) (canonic_exp x)).
491

492
Theorem round_le_pos :
493
  forall x y, (0 < x)%R -> (x <= y)%R -> (round x <= round y)%R.
494
Proof.
495
intros x y Hx Hxy.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
496
unfold round, scaled_mantissa, canonic_exp.
497 498 499 500 501 502 503 504 505 506
destruct (ln_beta beta x) as (ex, Hex). simpl.
destruct (ln_beta beta y) as (ey, Hey). simpl.
specialize (Hex (Rgt_not_eq _ _ Hx)).
specialize (Hey (Rgt_not_eq _ _ (Rlt_le_trans _ _ _ Hx Hxy))).
rewrite Rabs_pos_eq in Hex.
2: now apply Rlt_le.
rewrite Rabs_pos_eq in Hey.
2: apply Rle_trans with (2:=Hxy); now apply Rlt_le.
assert (He: (ex <= ey)%Z).
cut (ex - 1 < ey)%Z. omega.
507
apply (lt_bpow beta).
508 509 510 511
apply Rle_lt_trans with (1 := proj1 Hex).
apply Rle_lt_trans with (1 := Hxy).
apply Hey.
destruct (Zle_or_lt ey (fexp ey)) as [Hy1|Hy1].
512
rewrite (proj2 (proj2 (valid_exp ey) Hy1) ex).
513
apply F2R_le_compat.
514
apply Zrnd_le.
515 516 517 518 519 520
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
now apply Zle_trans with ey.
destruct (Zle_lt_or_eq _ _ He) as [He'|He'].
destruct (Zle_or_lt ey (fexp ex)) as [Hx2|Hx2].
521
rewrite (proj2 (proj2 (valid_exp ex) (Zle_trans _ _ _ He Hx2)) ey Hx2).
522
apply F2R_le_compat.
523
apply Zrnd_le.
524 525 526
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
527
apply Rle_trans with (F2R (Float beta (rnd (bpow (ey - 1) * bpow (- fexp ey))) (fexp ey))).
528
rewrite <- bpow_plus.
529 530 531 532 533
rewrite <- (Z2R_Zpower beta (ey - 1 + -fexp ey)). 2: omega.
rewrite Zrnd_Z2R.
destruct (Zle_or_lt ex (fexp ex)) as [Hx1|Hx1].
apply Rle_trans with (F2R (Float beta 1 (fexp ex))).
apply F2R_le_compat.
534
rewrite <- (Zrnd_Z2R 1).
535
apply Zrnd_le.
536 537 538 539
apply Rlt_le.
exact (proj2 (mantissa_small_pos _ _ Hex Hx1)).
unfold F2R. simpl.
rewrite Z2R_Zpower. 2: omega.
540
rewrite <- bpow_plus, Rmult_1_l.
541
apply bpow_le.
542
omega.
543
apply Rle_trans with (F2R (Float beta (rnd (bpow ex * bpow (- fexp ex))) (fexp ex))).
544
apply F2R_le_compat.
545
apply Zrnd_le.
546 547 548 549
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hex.
550
rewrite <- bpow_plus.
551 552 553 554
rewrite <- Z2R_Zpower. 2: omega.
rewrite Zrnd_Z2R.
unfold F2R. simpl.
rewrite 2!Z2R_Zpower ; try omega.
555
rewrite <- 2!bpow_plus.
556
apply bpow_le.
557 558
omega.
apply F2R_le_compat.
559
apply Zrnd_le.
560 561 562 563 564
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Hey.
rewrite He'.
apply F2R_le_compat.
565
apply Zrnd_le.
566 567 568 569 570
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
Qed.

571
Theorem round_generic :
572 573
  forall x,
  generic_format x ->
574
  round x = x.
575 576
Proof.
intros x Hx.
577
unfold round.
578 579 580 581 582
rewrite scaled_mantissa_generic with (1 := Hx).
rewrite Zrnd_Z2R.
now apply sym_eq.
Qed.

583 584
Theorem round_0 :
  round 0 = R0.
585
Proof.
586
unfold round, scaled_mantissa.
587 588 589 590 591 592
rewrite Rmult_0_l.
fold (Z2R 0).
rewrite Zrnd_Z2R.
apply F2R_0.
Qed.

593
Theorem round_bounded_large_pos :
594 595 596
  forall x ex,
  (fexp ex < ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
597
  (bpow (ex - 1) <= round x <= bpow ex)%R.
598 599
Proof.
intros x ex He Hx.
600
unfold round, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
601
rewrite (canonic_exp_fexp_pos _ _ Hx).
602
unfold F2R. simpl.
603
destruct (Zrnd_DN_or_UP (x * bpow (- fexp ex))) as [Hr|Hr] ; rewrite Hr.
604 605 606
(* DN *)
split.
replace (ex - 1)%Z with (ex - 1 + - fexp ex + fexp ex)%Z by ring.
607
rewrite bpow_plus.
608 609
apply Rmult_le_compat_r.
apply bpow_ge_0.
610
assert (Hf: Z2R (Zpower beta (ex - 1 - fexp ex)) = bpow (ex - 1 + - fexp ex)).
611 612 613 614 615 616
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zfloor_lub.
rewrite Hf.
617
rewrite bpow_plus.
618 619 620 621 622 623
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Hx.
apply Rle_trans with (2 := Rlt_le _ _ (proj2 Hx)).
apply Rmult_le_reg_r with (bpow (- fexp ex)).
apply bpow_gt_0.
624
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
625 626 627 628 629 630
apply Zfloor_lb.
(* UP *)
split.
apply Rle_trans with (1 := proj1 Hx).
apply Rmult_le_reg_r with (bpow (- fexp ex)).
apply bpow_gt_0.
631
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
632 633
apply Zceil_ub.
pattern ex at 3 ; replace ex with (ex - fexp ex + fexp ex)%Z by ring.
634
rewrite bpow_plus.
635 636
apply Rmult_le_compat_r.
apply bpow_ge_0.
637
assert (Hf: Z2R (Zpower beta (ex - fexp ex)) = bpow (ex - fexp ex)).
638 639 640 641 642 643 644
apply Z2R_Zpower.
omega.
rewrite <- Hf.
apply Z2R_le.
apply Zceil_glb.
rewrite Hf.
unfold Zminus.
645
rewrite bpow_plus.
646 647 648 649 650 651
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hx.
Qed.

652
Theorem round_bounded_small_pos :
653 654 655
  forall x ex,
  (ex <= fexp ex)%Z ->
  (bpow (ex - 1) <= x < bpow ex)%R ->
656
  round x = R0 \/ round x = bpow (fexp ex).
657 658
Proof.
intros x ex He Hx.
659
unfold round, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
660
rewrite (canonic_exp_fexp_pos _ _ Hx).
661
unfold F2R. simpl.
662
destruct (Zrnd_DN_or_UP (x * bpow (-fexp ex))) as [Hr|Hr] ; rewrite Hr.
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
(* DN *)
left.
apply Rmult_eq_0_compat_r.
apply (@f_equal _ _ Z2R _ Z0).
apply Zfloor_imp.
refine (let H := _ in conj (Rlt_le _ _ (proj1 H)) (proj2 H)).
now apply mantissa_small_pos.
(* UP *)
right.
pattern (bpow (fexp ex)) at 2 ; rewrite <- Rmult_1_l.
apply (f_equal (fun m => (m * bpow (fexp ex))%R)).
apply (@f_equal _ _ Z2R _ 1%Z).
apply Zceil_imp.
refine (let H := _ in conj (proj1 H) (Rlt_le _ _ (proj2 H))).
now apply mantissa_small_pos.
Qed.

680
Theorem generic_format_round_pos :
681 682
  forall x,
  (0 < x)%R ->
683
  generic_format (round x).
684 685 686 687 688 689 690
Proof.
intros x Hx0.
destruct (ln_beta beta x) as (ex, Hex).
specialize (Hex (Rgt_not_eq _ _ Hx0)).
rewrite Rabs_pos_eq in Hex. 2: now apply Rlt_le.
destruct (Zle_or_lt ex (fexp ex)) as [He|He].
(* small *)
691
destruct (round_bounded_small_pos _ _ He Hex) as [Hr|Hr] ; rewrite Hr.
692 693
apply generic_format_0.
apply generic_format_bpow.
694
now apply valid_exp.
695
(* large *)
696
generalize (round_bounded_large_pos _ _ He Hex).
697
intros (Hr1, Hr2).
698
destruct (Rle_or_lt (bpow ex) (round x)) as [Hr|Hr].
699 700
rewrite <- (Rle_antisym _ _ Hr Hr2).
apply generic_format_bpow.
701
now apply valid_exp.
702 703
assert (Hr' := conj Hr1 Hr).
unfold generic_format, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
704
rewrite (canonic_exp_fexp_pos _ _ Hr').
705
unfold round, scaled_mantissa.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
706
rewrite (canonic_exp_fexp_pos _ _ Hex).
707
unfold F2R at 3. simpl.
708
rewrite Rmult_assoc, <- bpow_plus, Zplus_opp_r, Rmult_1_r.
709 710 711
now rewrite Ztrunc_Z2R.
Qed.

712
End Fcore_generic_round_pos.
713

714
Theorem round_ext :
715
  forall rnd1 rnd2,
716
  ( forall x, rnd1 x = rnd2 x ) ->
717
  forall x,
718
  round rnd1 x = round rnd2 x.
719 720
Proof.
intros rnd1 rnd2 Hext x.
721
unfold round.
722 723 724
now rewrite Hext.
Qed.

725
Section Zround_opp.
726

727 728
Variable rnd : R -> Z.
Context { valid_rnd : Valid_rnd rnd }.
729

730
Definition Zrnd_opp x := Zopp (rnd (-x)).
731

732 733 734 735
Global Instance valid_rnd_opp : Valid_rnd Zrnd_opp.
Proof with auto with typeclass_instances.
split.
(* *)
736
intros x y Hxy.
737
unfold Zrnd_opp.
738 739
apply Zopp_le_cancel.
rewrite 2!Zopp_involutive.
740
apply Zrnd_le...
741
now apply Ropp_le_contravar.
742
(* *)
743
intros n.
744
unfold Zrnd_opp.
745
rewrite <- Z2R_opp, Zrnd_Z2R...
746 747 748
apply Zopp_involutive.
Qed.

749
Theorem round_opp :
750
  forall x,
751
  round rnd (- x) = Ropp (round Zrnd_opp x).
752 753
Proof.
intros x.
754
unfold round.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
755
rewrite <- F2R_Zopp, canonic_exp_opp, scaled_mantissa_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
756
apply F2R_eq_compat.
757 758 759 760
apply sym_eq.
exact (Zopp_involutive _).
Qed.

761
End Zround_opp.
762

763
(** IEEE-754 roundings: up, down and to zero *)
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789

Global Instance valid_rnd_DN : Valid_rnd Zfloor.
Proof.
split.
apply Zfloor_le.
apply Zfloor_Z2R.
Qed.

Global Instance valid_rnd_UP : Valid_rnd Zceil.
Proof.
split.
apply Zceil_le.
apply Zceil_Z2R.
Qed.

Global Instance valid_rnd_ZR : Valid_rnd Ztrunc.
Proof.
split.
apply Ztrunc_le.
apply Ztrunc_Z2R.
Qed.

Section monotone.

Variable rnd : R -> Z.
Context { valid_rnd : Valid_rnd rnd }.
790

791
Theorem round_DN_or_UP :
792 793
  forall x,
  round rnd x = round Zfloor x \/ round rnd x = round Zceil x.
794
Proof.
795
intros x.
796
unfold round.
797
destruct (Zrnd_DN_or_UP rnd (scaled_mantissa x)) as [Hx|Hx].
798 799 800 801
left. now rewrite Hx.
right. now rewrite Hx.
Qed.

802
Theorem round_le :
803 804 805
  forall x y, (x <= y)%R -> (round rnd x <= round rnd y)%R.
Proof with auto with typeclass_instances.
intros x y Hxy.
806
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
807
3: now apply round_le_pos.
808
(* x < 0 *)
809
unfold round.
810 811 812 813
destruct (Rlt_or_le y 0) as [Hy|Hy].
(* . y < 0 *)
rewrite <- (Ropp_involutive x), <- (Ropp_involutive y).
rewrite (scaled_mantissa_opp (-x)), (scaled_mantissa_opp (-y)).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
814
rewrite (canonic_exp_opp (-x)), (canonic_exp_opp (-y)).
815
apply Ropp_le_cancel.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
816
rewrite <- 2!F2R_Zopp.
817
apply (round_le_pos (Zrnd_opp rnd) (-y) (-x)).
818 819 820 821 822 823
rewrite <- Ropp_0.
now apply Ropp_lt_contravar.
now apply Ropp_le_contravar.
(* . 0 <= y *)
apply Rle_trans with R0.
apply F2R_le_0_compat. simpl.
824
rewrite <- (Zrnd_Z2R rnd 0).
825
apply Zrnd_le...
826
simpl.
827
rewrite <- (Rmult_0_l (bpow (- fexp (ln_beta beta x)))).
828 829 830 831
apply Rmult_le_compat_r.
apply bpow_ge_0.
now apply Rlt_le.
apply F2R_ge_0_compat. simpl.
832
rewrite <- (Zrnd_Z2R rnd 0).
833
apply Zrnd_le...
834 835 836 837 838
apply Rmult_le_pos.
exact Hy.
apply bpow_ge_0.
(* x = 0 *)
rewrite Hx.
839
rewrite round_0...
840 841
apply F2R_ge_0_compat.
simpl.
842
rewrite <- (Zrnd_Z2R rnd 0).
843
apply Zrnd_le...
844 845 846 847 848
apply Rmult_le_pos.
now rewrite <- Hx.
apply bpow_ge_0.
Qed.

849
Theorem round_ge_generic :
850
  forall x y, generic_format x -> (x <= y)%R -> (x <= round rnd y)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
851
Proof.
852
intros x y Hx Hxy.
853
rewrite <- (round_generic rnd x Hx).
854
now apply round_le.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
855
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
856

857
Theorem round_le_generic :
858
  forall x y, generic_format y -> (x <= y)%R -> (round rnd x <= y)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
859
Proof.
860
intros x y Hy Hxy.
861
rewrite <- (round_generic rnd y Hy).
862
now apply round_le.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
863
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
864

865 866
End monotone.

867
Theorem round_abs_abs :
868
  forall P : R -> R -> Prop,
869 870 871 872
  ( forall rnd (Hr : Valid_rnd rnd) x, P x (round rnd x) ) ->
  forall rnd {Hr : Valid_rnd rnd} x, P (Rabs x) (Rabs (round rnd x)).
Proof with auto with typeclass_instances.
intros P HP rnd Hr x.
873 874 875
destruct (Rle_or_lt 0 x) as [Hx|Hx].
(* . *)
rewrite 2!Rabs_pos_eq.
876
now apply HP.
877
rewrite <- (round_0 rnd).
878
now apply round_le.
879 880 881 882 883
exact Hx.
(* . *)
rewrite (Rabs_left _ Hx).
rewrite Rabs_left1.
pattern x at 2 ; rewrite <- Ropp_involutive.
884
rewrite round_opp.
885
rewrite Ropp_involutive.
886
apply HP...
887
rewrite <- (round_0 rnd).
888
apply round_le...
889 890 891
now apply Rlt_le.
Qed.

892 893 894 895 896
Section monotone_abs.

Variable rnd : R -> Z.
Context { valid_rnd : Valid_rnd rnd }.

897
Theorem abs_round_ge_generic :
898 899 900 901 902 903
  forall x y, generic_format x -> (x <= Rabs y)%R -> (x <= Rabs (round rnd y))%R.
Proof with auto with typeclass_instances.
intros x y.
apply round_abs_abs...
clear rnd valid_rnd y.
intros rnd' Hrnd y Hy.
904
apply round_ge_generic...
BOLDO Sylvie's avatar
BOLDO Sylvie committed
905 906
Qed.

907
Theorem abs_round_le_generic :
908 909 910 911 912 913
  forall x y, generic_format