Average.v 54 KB
Newer Older
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(**
This example is part of the Flocq formalization of floating-point
arithmetic in Coq: http://flocq.gforge.inria.fr/

Copyright (C) 2014-2018 Sylvie Boldo

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.
*)

18
Require Import Reals Psatz.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
19
From Flocq Require Import Core Plus_error.
20 21 22 23 24

Open Scope R_scope.

Section av1.

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

Lemma Rmin_Rmax_overflow: forall x y z M, Rabs x <= M -> Rabs y <= M ->
        Rmin x y <= z <= Rmax x y -> Rabs z <= M.
Proof.
intros x y z M Hx Hy H.
case (Rle_or_lt 0 z); intros Hz.
rewrite Rabs_right.
apply Rle_trans with (1:=proj2 H).
generalize (proj2 H).
apply Rmax_case_strong.
intros; apply Rle_trans with (2:=Hx).
apply RRle_abs.
intros; apply Rle_trans with (2:=Hy).
apply RRle_abs.
now apply Rle_ge.
rewrite Rabs_left; try assumption.
apply Rle_trans with (Rmax (-x) (-y)).
rewrite Rmax_opp.
apply Ropp_le_contravar, H.
apply Rmax_case_strong.
intros; apply Rle_trans with (2:=Hx).
rewrite <- Rabs_Ropp.
apply RRle_abs.
intros; apply Rle_trans with (2:=Hy).
rewrite <- Rabs_Ropp.
apply RRle_abs.
Qed.

53 54 55 56 57 58 59 60

Definition radix2 := Build_radix 2 (refl_equal true).
Notation bpow e := (bpow radix2 e).

Variable emin prec : Z.
Context { prec_gt_0_ : Prec_gt_0 prec }.

Notation format := (generic_format radix2 (FLT_exp emin prec)).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
61
Notation round_flt :=(round radix2 (FLT_exp emin prec) ZnearestE).
62
Notation ulp_flt :=(ulp radix2 (FLT_exp emin prec)).
63
Notation cexp := (cexp radix2 (FLT_exp emin prec)).
64
Notation pred_flt := (pred radix2 (FLT_exp emin prec)).
65 66 67 68 69 70

Lemma FLT_format_double: forall u, format u -> format (2*u).
Proof with auto with typeclass_instances.
intros u Fu.
apply generic_format_FLT.
apply FLT_format_generic in Fu...
71
destruct Fu as [uf H1 H2 H3].
72 73 74 75
exists (Float radix2 (Fnum uf) (Fexp uf+1)).
rewrite H1; unfold F2R; simpl.
rewrite bpow_plus, bpow_1.
simpl;ring.
76
easy.
77
apply Z.le_trans with (1:=H3).
78
apply Zle_succ.
79 80
Qed.

81
Lemma FLT_format_half: forall u,
82 83 84 85
   format u -> bpow (prec+emin) <= Rabs u -> format (u/2).
Proof with auto with typeclass_instances.
intros u Fu H.
apply FLT_format_generic in Fu...
86
destruct Fu as [[n e] H1 H2 H3].
87 88 89 90 91
simpl in H1, H2, H3.
apply generic_format_FLT.
exists (Float radix2 n (e-1)).
rewrite H1; unfold F2R; simpl.
unfold Zminus; rewrite bpow_plus.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
92 93
change (bpow (-(1))) with (/2).
unfold Rdiv; ring.
94 95 96
easy.
cut (prec + emin < prec +e)%Z.
  simpl ; omega.
97 98 99 100 101 102 103 104
apply lt_bpow with radix2.
apply Rle_lt_trans with (1:=H).
rewrite H1; unfold F2R; simpl.
rewrite Rabs_mult; rewrite (Rabs_right (bpow e)).
2: apply Rle_ge, bpow_ge_0.
rewrite bpow_plus.
apply Rmult_lt_compat_r.
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
105 106 107
rewrite <- abs_IZR.
rewrite <- IZR_Zpower.
now apply IZR_lt.
108
now apply Zlt_le_weak.
109 110
Qed.

111
Lemma FLT_round_half: forall z, bpow (prec+emin) <= Rabs z ->
112 113 114 115 116 117 118 119 120 121 122 123
   round_flt (z/2)= round_flt z /2.
Proof with auto with typeclass_instances.
intros z Hz.
apply Rmult_eq_reg_l with 2.
2: apply sym_not_eq; auto with real.
apply trans_eq with (round_flt z).
2: field.
assert (z <> 0)%R.
intros K; contradict Hz.
rewrite K, Rabs_R0; apply Rlt_not_le.
apply bpow_gt_0.
assert (cexp (z/2) = cexp z -1)%Z.
124
assert (prec+emin < mag radix2 z)%Z.
125
apply lt_bpow with radix2.
126
destruct mag as (e,He); simpl.
127 128
apply Rle_lt_trans with (1:=Hz).
now apply He.
129
unfold cexp, FLT_exp.
130
replace ((mag radix2 (z/2))-prec)%Z with ((mag radix2 z -1) -prec)%Z.
131
rewrite Z.max_l; lia.
132
apply Zplus_eq_compat; try reflexivity.
133 134
apply sym_eq, mag_unique.
destruct (mag radix2 z) as (e,He); simpl.
135 136 137 138 139 140 141 142 143
unfold Rdiv; rewrite Rabs_mult.
rewrite (Rabs_right (/2)).
split.
apply Rmult_le_reg_l with (bpow 1).
apply bpow_gt_0.
rewrite <- bpow_plus.
replace (1+(e-1-1))%Z with (e-1)%Z by ring.
apply Rle_trans with (Rabs z).
now apply He.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
144
change (bpow 1) with 2%R.
145 146 147 148 149 150
right; simpl; field.
apply Rmult_lt_reg_l with (bpow 1).
apply bpow_gt_0.
rewrite <- bpow_plus.
replace (1+(e-1))%Z with e by ring.
apply Rle_lt_trans with (Rabs z).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
151 152
change (bpow 1) with 2.
right; field.
153
now apply He.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
154
lra.
155 156 157 158 159 160 161
unfold round, scaled_mantissa, F2R.
rewrite H0; simpl.
rewrite Rmult_comm, Rmult_assoc.
apply f_equal2.
apply f_equal, f_equal.
replace (-(cexp z -1))%Z with (-cexp z +1)%Z by ring.
rewrite bpow_plus.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
162 163
change (bpow 1) with 2.
field.
164
unfold Zminus; rewrite bpow_plus.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
165 166
change (bpow (-(1))) with (/2).
field.
167
Qed.
168 169 170 171

Lemma FLT_ulp_le_id: forall u, bpow emin <= u -> ulp_flt u <= u.
Proof with auto with typeclass_instances.
intros u H.
172 173
rewrite ulp_neq_0.
2: apply Rgt_not_eq, Rlt_le_trans with (2:=H), bpow_gt_0.
174
case (Rle_or_lt (bpow (emin+prec-1)) u); intros Hu.
175 176
unfold ulp; rewrite cexp_FLT_FLX.
unfold cexp, FLX_exp.
177
destruct (mag radix2 u) as (e,He); simpl.
178 179 180 181 182 183 184 185 186 187 188 189
apply Rle_trans with (bpow (e-1)).
apply bpow_le.
unfold Prec_gt_0 in prec_gt_0_; omega.
rewrite <- (Rabs_right u).
apply He.
apply Rgt_not_eq, Rlt_gt.
apply Rlt_le_trans with (2:=Hu).
apply bpow_gt_0.
apply Rle_ge, Rle_trans with (2:=Hu), bpow_ge_0.
rewrite Rabs_right.
assumption.
apply Rle_ge, Rle_trans with (2:=Hu), bpow_ge_0.
190
unfold ulp; rewrite cexp_FLT_FIX.
191 192 193 194 195 196 197 198 199 200 201 202 203
apply H.
apply Rgt_not_eq, Rlt_gt.
apply Rlt_le_trans with (2:=H).
apply bpow_gt_0.
rewrite Rabs_right.
apply Rlt_le_trans with (1:=Hu).
apply bpow_le; omega.
apply Rle_ge, Rle_trans with (2:=H), bpow_ge_0.
Qed.



Lemma FLT_ulp_double: forall u, ulp_flt (2*u) <= 2*ulp_flt(u).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
204
Proof.
205
intros u.
206 207 208 209
case (Req_bool_spec u 0); intros Hu'.
rewrite Hu', Rmult_0_r.
rewrite <- (Rmult_1_l (ulp_flt 0)) at 1.
apply Rmult_le_compat_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
210
apply ulp_ge_0.
211 212
left; apply Rlt_plus_1.
rewrite 2!ulp_neq_0; trivial.
213
2: lra.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
214
change 2 at 2 with (bpow 1).
215
rewrite <- bpow_plus.
216 217 218
apply bpow_le.
case (Rle_or_lt (bpow (emin+prec-1)) (Rabs u)); intros Hu.
(* *)
219 220 221
rewrite cexp_FLT_FLX.
rewrite cexp_FLT_FLX; trivial.
unfold cexp, FLX_exp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
222
change 2 with (bpow 1).
223
rewrite Rmult_comm, mag_mult_bpow.
224 225 226 227 228 229 230 231 232
omega.
intros H; contradict Hu.
apply Rlt_not_le; rewrite H, Rabs_R0.
apply bpow_gt_0.
apply Rle_trans with (1:=Hu).
rewrite Rabs_mult.
pattern (Rabs u) at 1; rewrite <- (Rmult_1_l (Rabs u)).
apply Rmult_le_compat_r.
apply Rabs_pos.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
233
rewrite <- (abs_IZR 2).
234
now apply IZR_le.
235
(* *)
236 237 238
rewrite cexp_FLT_FIX.
rewrite cexp_FLT_FIX; trivial.
unfold FIX_exp, cexp; omega.
239 240
apply Rlt_le_trans with (1:=Hu).
apply bpow_le; omega.
241
lra.
242
rewrite Rabs_mult.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
243
rewrite Rabs_pos_eq.
244
2: now apply IZR_le.
245
apply Rlt_le_trans with (2*bpow (emin + prec - 1)).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
246
apply Rmult_lt_compat_l with (1 := Rlt_0_2).
247
assumption.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
248
change 2 with (bpow 1).
249 250 251 252 253
rewrite <- bpow_plus.
apply bpow_le; omega.
Qed.


254
Lemma round_plus_small_id_aux: forall f h, format f -> (bpow (prec+emin) <= f) -> 0 < f
255
   -> Rabs h <= /4* ulp_flt f -> round_flt (f+h) = f.
256 257 258 259 260 261
Proof with auto with typeclass_instances.
intros f h Ff H1 H2 Hh.
case (Rle_or_lt 0 h); intros H3;[destruct H3|idtac].
(* 0 < h *)
rewrite Rabs_right in Hh.
2: now apply Rle_ge, Rlt_le.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
262
apply round_N_eq_DN_pt with (f+ ulp_flt f)...
263
pattern f at 2; rewrite <- (round_DN_plus_eps_pos radix2 (FLT_exp emin prec) f) with (eps:=h); try assumption.
264
apply round_DN_pt...
265
now left.
266 267
split.
now left.
268
apply Rle_lt_trans with (1:=Hh).
269 270
rewrite <- (Rmult_1_l (ulp_flt f)) at 2.
apply Rmult_lt_compat_r.
271
rewrite ulp_neq_0; try now apply Rgt_not_eq.
272
apply bpow_gt_0.
273
lra.
274
rewrite <- (round_UP_plus_eps_pos radix2 (FLT_exp emin prec) f) with (eps:=h); try assumption.
275
apply round_UP_pt...
276
now left.
277
split; trivial.
278
apply Rle_trans with (1:=Hh).
279 280
rewrite <- (Rmult_1_l (ulp_flt f)) at 2.
apply Rmult_le_compat_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
281
apply ulp_ge_0.
282
lra.
283 284 285
apply Rplus_lt_reg_l with (-f); ring_simplify.
apply Rlt_le_trans with (/2*ulp_flt f).
2: right; field.
286
apply Rle_lt_trans with (1:=Hh).
287
apply Rmult_lt_compat_r.
288
rewrite ulp_neq_0; try now apply Rgt_not_eq.
289
apply bpow_gt_0.
290
lra.
291 292 293 294 295 296 297 298 299
(* h = 0 *)
rewrite <- H, Rplus_0_r.
apply round_generic...
(* h < 0 *)
(*  - assertions *)
rewrite Rabs_left in Hh; try assumption.
assert (0 < pred_flt f).
apply Rlt_le_trans with (bpow emin).
apply bpow_gt_0.
300
apply pred_ge_gt...
301 302 303 304 305
apply FLT_format_bpow...
omega.
apply Rlt_le_trans with (2:=H1).
apply bpow_lt.
unfold Prec_gt_0 in prec_gt_0_; omega.
306 307
assert (M:(prec + emin +1 <= mag radix2 f)%Z).
apply mag_ge_bpow.
308 309 310
replace (prec+emin+1-1)%Z with (prec+emin)%Z by ring.
rewrite Rabs_right; try assumption.
apply Rle_ge; now left.
311 312
assert (T1:(ulp_flt (pred_flt f) = ulp_flt f)
     \/ ( ulp_flt (pred_flt f) = /2* ulp_flt f
313
               /\ f = bpow (mag radix2 f -1))).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
314
generalize H; rewrite pred_eq_pos; [idtac|now left].
315
unfold pred_pos; case Req_bool_spec; intros K HH.
316 317
(**)
right; split; try assumption.
318
rewrite ulp_neq_0;[idtac|now apply Rgt_not_eq].
319
apply trans_eq with (bpow (mag radix2 f- prec -1)).
320
apply f_equal.
321
unfold cexp.
322
apply trans_eq with (FLT_exp emin prec (mag radix2 f -1)%Z).
323 324 325 326
apply f_equal.
unfold FLT_exp.
rewrite Z.max_l.
2: omega.
327
apply mag_unique.
328 329
rewrite Rabs_right.
split.
330
apply Rplus_le_reg_l with (bpow (mag radix2 f -1-prec)).
331
ring_simplify.
332
apply Rle_trans with (bpow (mag radix2 f - 1 - 1) + bpow (mag radix2 f - 1 - 1)).
333 334 335
apply Rplus_le_compat_r.
apply bpow_le.
unfold Prec_gt_0 in prec_gt_0_; omega.
336
apply Rle_trans with (bpow 1*bpow (mag radix2 f - 1 - 1)).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
337 338
change (bpow 1) with 2.
right; ring.
339
rewrite <- bpow_plus.
340
apply Rle_trans with (bpow (mag radix2 f -1)).
341 342 343
apply bpow_le; omega.
rewrite <- K; now right.
rewrite <- K.
344
apply Rplus_lt_reg_l with (-f+bpow (mag radix2 f-1-prec)); ring_simplify.
345 346 347
apply bpow_gt_0.
apply Rle_ge.
rewrite K at 1.
348
apply Rplus_le_reg_l with (bpow (mag radix2 f - 1 - prec)).
349 350 351 352 353 354
ring_simplify.
apply bpow_le.
unfold Prec_gt_0 in prec_gt_0_; omega.
unfold FLT_exp.
rewrite Z.max_l;[ring|omega].
replace (/2) with (bpow (-1)) by reflexivity.
355 356
rewrite ulp_neq_0; try now apply Rgt_not_eq.
rewrite <- bpow_plus.
357
apply f_equal.
358
unfold cexp, FLT_exp.
359 360 361
rewrite Z.max_l;[ring|omega].
(**)
left.
362 363
assert (bpow (mag radix2 f -1) < f).
destruct (mag radix2 f); simpl in *.
364
destruct a.
365
now apply Rgt_not_eq.
366 367 368 369 370
rewrite Rabs_right in H0.
destruct H0; try assumption.
contradict H0.
now apply sym_not_eq.
apply Rle_ge; now left.
371
assert (bpow (mag radix2 f -1) + ulp_flt (bpow (mag radix2 f-1)) <= f).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
372
rewrite <- succ_eq_pos;[idtac|apply bpow_ge_0].
373
apply succ_le_lt...
374 375 376 377 378 379
apply FLT_format_bpow...
unfold Prec_gt_0 in prec_gt_0_;omega.
rewrite ulp_bpow in H4.
unfold FLT_exp in H4.
rewrite Z.max_l in H4.
2: omega.
380
replace (mag radix2 f - 1 + 1 - prec)%Z with  (mag radix2 f - prec)%Z in H4 by ring.
381 382
rewrite ulp_neq_0; try now apply Rgt_not_eq.
rewrite ulp_neq_0 at 2; try now apply Rgt_not_eq.
383
unfold cexp.
384
apply f_equal; apply f_equal.
385 386
replace (ulp_flt f) with (bpow (mag radix2 f -prec)).
apply mag_unique.
387 388
rewrite Rabs_right.
split.
389
apply Rplus_le_reg_l with (bpow (mag radix2 f -prec)).
390 391 392
ring_simplify.
apply Rle_trans with (2:=H4); right; ring.
apply Rlt_trans with f.
393
apply Rplus_lt_reg_l with (-f+bpow (mag radix2 f - prec)).
394 395 396
ring_simplify.
apply bpow_gt_0.
apply Rle_lt_trans with (1:=RRle_abs _).
397
apply bpow_mag_gt.
398
apply Rle_ge.
399
apply Rplus_le_reg_l with (bpow (mag radix2 f - prec)).
400 401 402 403
ring_simplify.
left; apply Rle_lt_trans with (2:=H0).
apply bpow_le.
unfold Prec_gt_0 in prec_gt_0_;omega.
404
rewrite ulp_neq_0; try now apply Rgt_not_eq.
405
unfold cexp, FLT_exp.
406 407 408
rewrite Z.max_l.
reflexivity.
omega.
409
assert (T: (ulp_flt (pred_flt f) = ulp_flt f \/
410 411
              (ulp_flt (pred_flt f) = / 2 * ulp_flt f /\ - h < / 4 * ulp_flt f))
         \/ (ulp_flt (pred_flt f) = / 2 * ulp_flt f /\
412
              f = bpow (mag radix2 f - 1) /\
413 414 415 416 417 418 419 420 421
              - h = / 4 * ulp_flt f) ).
destruct T1.
left; now left.
case Hh; intros P.
left; right.
split; try apply H0; assumption.
right.
split; try split; try apply H0; assumption.
clear T1.
422
(*  - end of assertions *)
423 424
destruct T.
(* normal case *)
BOLDO Sylvie's avatar
BOLDO Sylvie committed
425
apply round_N_eq_UP_pt with (pred_flt f)...
426
rewrite <- (round_DN_minus_eps_pos radix2 (FLT_exp emin prec) f) with (eps:=-h); try assumption.
427 428 429 430
replace (f--h) with (f+h) by ring.
apply round_DN_pt...
split.
auto with real.
431 432 433
apply Rle_trans with (1:=Hh).
apply Rle_trans with (/2*ulp_flt f).
apply Rmult_le_compat_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
434
apply ulp_ge_0.
435
lra.
436 437 438
case H0.
intros Y; rewrite Y.
rewrite <- (Rmult_1_l (ulp_flt f)) at 2.
439
apply Rmult_le_compat_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
440
apply ulp_ge_0.
441
lra.
442
intros Y; rewrite (proj1 Y); now right.
443
replace (f+h) with (pred_flt f + (f-pred_flt f+h)) by ring.
444
pattern f at 4; rewrite <- (round_UP_pred_plus_eps_pos radix2 (FLT_exp emin prec) f) with (eps:=(f - pred_flt f + h)); try assumption.
445 446 447 448
apply round_UP_pt...
replace (f-pred_flt f) with (ulp_flt (pred_flt f)).
split.
apply Rplus_lt_reg_l with (-h); ring_simplify.
449 450 451 452
case H0; [intros Y|intros (Y1,Y2)].
apply Rle_lt_trans with (1:=Hh).
rewrite Y.
rewrite <- (Rmult_1_l (ulp_flt f)) at 2.
453
apply Rmult_lt_compat_r.
454
rewrite ulp_neq_0;[apply bpow_gt_0|now apply Rgt_not_eq].
455
lra.
456 457 458
apply Rlt_le_trans with (1:=Y2).
rewrite Y1.
apply Rmult_le_compat_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
459
apply ulp_ge_0.
460
lra.
461 462
apply Rplus_le_reg_l with (-ulp_flt (pred_flt f)); ring_simplify.
now left.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
463
rewrite pred_eq_pos; try now left.
464
pattern f at 2; rewrite <- (pred_pos_plus_ulp radix2 (FLT_exp emin prec) f)...
465 466 467 468 469 470
ring.
apply Rplus_lt_reg_l with (-f); ring_simplify.
apply Rle_lt_trans with (-(/2 * ulp_flt (pred_flt f))).
right.
apply trans_eq with ((pred_flt f - f) / 2).
field.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
471
rewrite pred_eq_pos; try now left.
472
pattern f at 2; rewrite <- (pred_pos_plus_ulp radix2 (FLT_exp emin prec) f)...
473 474 475
field.
replace h with (--h) by ring.
apply Ropp_lt_contravar.
476 477 478 479
case H0;[intros Y|intros (Y1,Y2)].
apply Rle_lt_trans with (1:=Hh).
rewrite Y.
apply Rmult_lt_compat_r.
480
rewrite ulp_neq_0; try apply bpow_gt_0; now apply Rgt_not_eq.
481
lra.
482 483 484 485 486
apply Rlt_le_trans with (1:=Y2).
rewrite Y1.
right; field.
(* complex case: even choosing *)
elim H0; intros  T1 (T2,T3); clear H0.
487
assert (pred_flt f = bpow (mag radix2 f - 1) - bpow (mag radix2 f - 1 -prec)).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
488
rewrite pred_eq_pos; try now left.
489
unfold pred_pos; case Req_bool_spec.
490 491 492 493 494 495 496 497 498
intros _; rewrite <- T2.
apply f_equal, f_equal.
unfold FLT_exp.
rewrite Z.max_l.
ring.
omega.
intros Y; now contradict T2.
assert (round radix2 (FLT_exp emin prec) Zfloor (f+h) = pred_flt f).
replace (f+h) with (f-(-h)) by ring.
499
apply round_DN_minus_eps_pos...
500 501 502 503
split.
auto with real.
rewrite T3, T1.
apply Rmult_le_compat_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
504
apply ulp_ge_0.
505
lra.
506 507
assert (round radix2 (FLT_exp emin prec) Zceil (f+h) = f).
replace (f+h) with (pred_flt f + /2*ulp_flt (pred_flt f)).
508
apply round_UP_pred_plus_eps_pos...
509 510
split.
apply Rmult_lt_0_compat.
511
lra.
512
rewrite ulp_neq_0; try now apply Rgt_not_eq.
513 514 515
apply bpow_gt_0.
rewrite <- (Rmult_1_l (ulp_flt (pred_flt f))) at 2.
apply Rmult_le_compat_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
516
apply ulp_ge_0.
517
lra.
518 519
rewrite T1, H0, <- T2.
replace h with (--h) by ring; rewrite T3.
520
replace (bpow (mag radix2 f - 1 - prec)) with (/2*ulp_flt f).
521 522 523 524 525 526 527 528
field.
replace (/2) with (bpow (-1)) by reflexivity.
rewrite T2 at 1.
rewrite ulp_bpow, <- bpow_plus.
apply f_equal; unfold FLT_exp.
rewrite Z.max_l.
ring.
omega.
529
assert ((Z.even (Zfloor (scaled_mantissa radix2 (FLT_exp emin prec) (f + h)))) = false).
530 531
replace (Zfloor (scaled_mantissa radix2 (FLT_exp emin prec) (f + h)))
   with (Zpower radix2 prec -1)%Z.
532 533 534
unfold Zminus; rewrite Z.even_add.
rewrite Z.even_opp.
rewrite Z.even_pow.
535 536
reflexivity.
unfold Prec_gt_0 in prec_gt_0_; omega.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
537
apply eq_IZR.
538 539 540 541 542
rewrite <- scaled_mantissa_DN...
2: rewrite H4; assumption.
rewrite H4.
unfold scaled_mantissa.
rewrite bpow_opp.
543
rewrite <- ulp_neq_0; try now apply Rgt_not_eq.
544 545
rewrite T1.
rewrite Rinv_mult_distr.
546
2: apply Rgt_not_eq; lra.
547 548
2: apply Rgt_not_eq; rewrite ulp_neq_0; try apply bpow_gt_0.
2: now apply Rgt_not_eq.
549
rewrite Rinv_involutive.
550
2: apply Rgt_not_eq; lra.
551 552 553 554 555 556 557 558 559 560
rewrite T2 at 2.
rewrite ulp_bpow.
rewrite <- bpow_opp.
unfold FLT_exp at 2.
rewrite Z.max_l.
2: omega.
replace 2 with (bpow 1) by reflexivity.
rewrite <- bpow_plus.
rewrite H0.
rewrite Rmult_minus_distr_r, <- 2!bpow_plus.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
561
rewrite minus_IZR.
562
apply f_equal2.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
563
rewrite IZR_Zpower.
564 565 566 567 568 569 570 571 572 573 574 575
apply f_equal.
ring.
unfold Prec_gt_0 in prec_gt_0_; omega.
apply trans_eq with (bpow 0).
reflexivity.
apply f_equal.
ring.
rewrite round_N_middle.
rewrite H5.
rewrite H6.
reflexivity.
rewrite H5, H4.
576
pattern f at 1; rewrite <- (pred_pos_plus_ulp radix2 (FLT_exp emin prec) f); try assumption.
577
ring_simplify.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
578
rewrite <- pred_eq_pos;[idtac|now left].
579 580 581 582
rewrite T1.
replace h with (--h) by ring.
rewrite T3.
field.
583 584
Qed.

585
Lemma round_plus_small_id: forall f h, format f -> (bpow (prec+emin) <= Rabs f)
586
   -> Rabs h <= /4* ulp_flt f -> round_flt (f+h) = f.
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
intros f h Ff H1 H2.
case (Rle_or_lt 0 f); intros V.
case V; clear V; intros V.
apply round_plus_small_id_aux; try assumption.
rewrite Rabs_right in H1; try assumption.
apply Rle_ge; now left.
contradict H1.
rewrite <- V, Rabs_R0.
apply Rlt_not_le, bpow_gt_0.
rewrite <- (Ropp_involutive f), <- (Ropp_involutive h).
replace (--f + --h) with (-(-f+-h)) by ring.
rewrite round_NE_opp.
apply f_equal.
apply round_plus_small_id_aux.
now apply generic_format_opp.
rewrite Rabs_left in H1; try assumption.
auto with real.
now rewrite Rabs_Ropp, ulp_opp.
Qed.


608

BOLDO Sylvie's avatar
BOLDO Sylvie committed
609
Definition avg_naive (x y : R) :=round_flt(round_flt(x+y)/2).
610 611 612 613 614 615

Variables x y:R.
Hypothesis Fx: format x.
Hypothesis Fy: format y.

Let a:=(x+y)/2.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
616
Let av:=avg_naive x y.
617

BOLDO Sylvie's avatar
BOLDO Sylvie committed
618
Lemma avg_naive_correct: av = round_flt a.
619 620 621 622
Proof with auto with typeclass_instances.
case (Rle_or_lt (bpow (prec + emin)) (Rabs (x+y))).
(* normal case: division by 2 is exact *)
intros H.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
623
unfold av,a,avg_naive.
624 625 626 627 628 629 630 631 632
rewrite round_generic...
now apply sym_eq, FLT_round_half.
apply FLT_format_half.
apply generic_format_round...
apply abs_round_ge_generic...
apply FLT_format_bpow...
unfold Prec_gt_0 in prec_gt_0_; omega.
(* subnormal case: addition is exact, but division by 2 is not *)
intros H.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
633
unfold av, avg_naive.
634 635 636 637 638 639 640 641
replace (round_flt (x + y)) with (x+y).
reflexivity.
apply sym_eq, round_generic...
apply FLT_format_plus_small...
left; assumption.
Qed.


642

BOLDO Sylvie's avatar
BOLDO Sylvie committed
643
Lemma avg_naive_symmetry: forall u v, avg_naive u v = avg_naive v u.
644
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
645
intros u v; unfold avg_naive.
646 647 648
rewrite Rplus_comm; reflexivity.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
649
Lemma avg_naive_symmetry_Ropp: forall u v, avg_naive (-u) (-v) = - avg_naive u v.
650
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
651
intros u v; unfold avg_naive.
652 653 654 655 656 657
replace (-u+-v) with (-(u+v)) by ring.
rewrite round_NE_opp.
replace (- round_flt (u + v) / 2) with (- (round_flt (u + v) / 2)) by (unfold Rdiv; ring).
now rewrite round_NE_opp.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
658
Lemma avg_naive_same_sign_1: 0 <= a -> 0 <= av.
659 660
Proof with auto with typeclass_instances.
intros H.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
661
rewrite avg_naive_correct.
662 663 664 665
apply round_ge_generic...
apply generic_format_0.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
666
Lemma avg_naive_same_sign_2: a <= 0-> av <= 0.
667 668
Proof with auto with typeclass_instances.
intros H.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
669
rewrite avg_naive_correct.
670 671 672 673
apply round_le_generic...
apply generic_format_0.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
674
Lemma avg_naive_between: Rmin x y <= av <= Rmax x y.
675
Proof with auto with typeclass_instances.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
676
rewrite avg_naive_correct.
677
split.
678
apply round_ge_generic...
679
now apply P_Rmin.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
680 681 682
apply Rmult_le_reg_l with (1 := Rlt_0_2).
replace (2 * Rmin x y) with (Rmin x y + Rmin x y) by ring.
replace (2 * a) with (x + y) by (unfold a; field).
683 684 685 686
apply Rplus_le_compat.
apply Rmin_l.
apply Rmin_r.
(* *)
687
apply round_le_generic...
688
now apply Rmax_case.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
689 690 691
apply Rmult_le_reg_l with (1 := Rlt_0_2).
replace (2 * a) with (x + y) by (unfold a; field).
replace (2 * Rmax x y) with (Rmax x y + Rmax x y) by ring.
692 693 694
apply Rplus_le_compat.
apply Rmax_l.
apply Rmax_r.
695 696 697
Qed.


BOLDO Sylvie's avatar
BOLDO Sylvie committed
698
Lemma avg_naive_zero: a = 0 -> av = 0.
699
Proof with auto with typeclass_instances.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
700
intros H1; rewrite avg_naive_correct, H1.
701 702 703 704 705
rewrite round_0...
Qed.



BOLDO Sylvie's avatar
BOLDO Sylvie committed
706
Lemma avg_naive_no_underflow: (bpow emin) <= Rabs a -> av <> 0.
707 708 709 710 711 712 713 714 715 716
Proof with auto with typeclass_instances.
intros H.
(* *)
cut (bpow emin <= Rabs av).
intros H1 H2.
rewrite H2 in H1; rewrite Rabs_R0 in H1.
contradict H1.
apply Rlt_not_le.
apply bpow_gt_0.
(* *)
BOLDO Sylvie's avatar
BOLDO Sylvie committed
717
rewrite avg_naive_correct.
718 719 720 721 722 723
apply abs_round_ge_generic...
apply FLT_format_bpow...
omega.
Qed.


BOLDO Sylvie's avatar
BOLDO Sylvie committed
724
Lemma avg_naive_correct_weak1: Rabs (av -a) <= /2*ulp_flt a.
725
Proof with auto with typeclass_instances.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
726
rewrite avg_naive_correct.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
727
apply error_le_half_ulp...
728 729
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
730
Lemma avg_naive_correct_weak2: Rabs (av -a) <= 3/2*ulp_flt a.
731
Proof with auto with typeclass_instances.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
732
apply Rle_trans with (1:=avg_naive_correct_weak1).
733
apply Rmult_le_compat_r.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
734
unfold ulp; apply ulp_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
735
lra.
736 737 738 739 740 741 742 743
Qed.



(* Hypothesis diff_sign: (0 <= x /\ y <= 0) \/ (x <= 0 /\ 0 <= y).
  is useless for properties: only useful for preventing overflow *)


744 745 746



BOLDO Sylvie's avatar
BOLDO Sylvie committed
747
Definition avg_sum_half (x y : R) :=round_flt(round_flt(x/2) + round_flt(y/2)).
748

BOLDO Sylvie's avatar
BOLDO Sylvie committed
749
Let av2:=avg_sum_half x y.
750 751


BOLDO Sylvie's avatar
BOLDO Sylvie committed
752
Lemma avg_sum_half_correct: bpow (emin +prec+prec+1) <= Rabs x -> av2 = round_flt a.
753 754 755 756
Proof with auto with typeclass_instances.
intros Hx.
assert (G:(0 < prec)%Z).
unfold Prec_gt_0 in prec_gt_0_; assumption.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
757
unfold av2, avg_sum_half.
758 759 760 761 762 763 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
replace (round_flt (x/2)) with (x/2).
2: apply sym_eq, round_generic...
2: apply FLT_format_half; try assumption.
2: apply Rle_trans with (2:=Hx).
2: apply bpow_le; omega.
case (Rle_or_lt (bpow (prec + emin)) (Rabs y)).
(* y is big enough so that y/2 is correct *)
intros Hy.
replace (round_flt (y/2)) with (y/2).
apply f_equal; unfold a; field.
apply sym_eq, round_generic...
apply FLT_format_half; assumption.
(* y is a subnormal, then it is too small to impact the result *)
intros Hy.
assert (format (x/2)).
apply FLT_format_half.
assumption.
apply Rle_trans with (2:=Hx).
apply bpow_le.
omega.
assert (bpow (prec+emin) <= Rabs (x/2)).
apply Rmult_le_reg_l with (bpow 1).
apply bpow_gt_0.
rewrite <- bpow_plus.
apply Rle_trans with (Rabs x).
apply Rle_trans with (2:=Hx).
apply bpow_le.
omega.
rewrite <- (Rabs_right (bpow 1)).
rewrite <- Rabs_mult.
right; apply f_equal.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
789 790
change (bpow 1) with 2.
field.
791
apply Rle_ge, bpow_ge_0.
792
assert (K1: Rabs (y / 2) <= bpow (prec+emin-1)).
793 794
unfold Rdiv; rewrite Rabs_mult.
unfold Zminus; rewrite bpow_plus.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
795 796
simpl; rewrite (Rabs_pos_eq (/2)).
apply (Rmult_le_compat_r (/2)).
797
lra.
798
now left.
799
lra.
800
assert (K2:bpow (prec+emin-1) <= / 4 * ulp_flt (x / 2)).
801 802 803 804 805
assert (Z: x/2 <> 0).
intros K; contradict H0.
rewrite K, Rabs_R0.
apply Rlt_not_le, bpow_gt_0.
rewrite ulp_neq_0; trivial.
806 807
replace (/4) with (bpow (-2)) by reflexivity.
rewrite <- bpow_plus.
808
apply bpow_le.
809
unfold cexp, FLT_exp.
810 811
assert (emin+prec+prec+1 -1 < mag radix2 (x/2))%Z.
destruct (mag radix2 (x/2)) as (e,He).
812 813 814 815 816 817 818
simpl.
apply lt_bpow with radix2.
apply Rle_lt_trans with (Rabs (x/2)).
unfold Rdiv; rewrite Rabs_mult.
unfold Zminus; rewrite bpow_plus.
simpl; rewrite (Rabs_right (/2)).
apply Rmult_le_compat_r.
819
lra.
820
exact Hx.
821
lra.
822
apply He; trivial.
823 824 825
rewrite Z.max_l.
omega.
omega.
826 827 828 829 830 831 832
(* . *)
apply trans_eq with (x/2).
apply round_plus_small_id; try assumption.
apply Rle_trans with (2:=K2).
apply abs_round_le_generic...
apply FLT_format_bpow...
omega.
833 834 835
unfold a; apply sym_eq.
replace ((x+y)/2) with (x/2+y/2) by field.
apply round_plus_small_id; try assumption.
836
now apply Rle_trans with (2:=K2).
837 838 839 840
Qed.



841 842 843 844 845 846 847 848 849 850
End av1.

Section av3.

Notation bpow e := (bpow radix2 e).

Variable emin prec : Z.
Context { prec_gt_0_ : Prec_gt_0 prec }.

Notation format := (generic_format radix2 (FLT_exp emin prec)).
851
Notation round_flt :=(round radix2 (FLT_exp emin prec) ZnearestE).
852
Notation ulp_flt :=(ulp radix2 (FLT_exp emin prec)).
853
Notation cexp := (cexp radix2 (FLT_exp emin prec)).
854

BOLDO Sylvie's avatar
BOLDO Sylvie committed
855
Definition avg_half_sub (x y : R) :=round_flt(x+round_flt(round_flt(y-x)/2)).
856 857 858 859 860 861

Variables x y:R.
Hypothesis Fx: format x.
Hypothesis Fy: format y.

Let a:=(x+y)/2.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
862
Let av:=avg_half_sub x y.
863 864


BOLDO Sylvie's avatar
BOLDO Sylvie committed
865 866
Lemma avg_half_sub_symmetry_Ropp: forall u v, avg_half_sub (-u) (-v) = - avg_half_sub u v.
intros u v; unfold avg_half_sub.
867 868 869 870 871 872 873 874 875 876
replace (-v--u) with (-(v-u)) by ring.
rewrite round_NE_opp.
replace (- round_flt (v-u) / 2) with (- (round_flt (v-u) / 2)) by (unfold Rdiv; ring).
rewrite round_NE_opp.
replace (- u + - round_flt (round_flt (v - u) / 2)) with
   (-(u+round_flt (round_flt (v - u) / 2))) by ring.
apply round_NE_opp.
Qed.


BOLDO Sylvie's avatar
BOLDO Sylvie committed
877
Lemma avg_half_sub_same_sign_1: 0 <= a -> 0 <= av.
878 879 880 881 882 883 884 885
Proof with auto with typeclass_instances.
intros H.
apply round_ge_generic...
apply generic_format_0.
apply Rplus_le_reg_l with (-x).
ring_simplify.
apply round_ge_generic...
now apply generic_format_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
886
apply Rmult_le_reg_l with (1 := Rlt_0_2).
887 888 889 890 891 892 893 894 895
apply Rle_trans with (-(2*x)).
right; ring.
apply Rle_trans with (round_flt (y - x)).
2: right; field.
apply round_ge_generic...
apply generic_format_opp.
now apply FLT_format_double...
apply Rplus_le_reg_l with (2*x).
apply Rmult_le_reg_r with (/2).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
896
lra.
897 898 899 900 901
apply Rle_trans with 0;[right; ring|idtac].
apply Rle_trans with (1:=H).
right; unfold a, Rdiv; ring.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
902
Lemma avg_half_sub_same_sign_2: a <= 0-> av <= 0.
903 904 905 906 907 908 909 910
Proof with auto with typeclass_instances.
intros H.
apply round_le_generic...
apply generic_format_0.
apply Rplus_le_reg_l with (-x).
ring_simplify.
apply round_le_generic...
now apply generic_format_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
911
apply Rmult_le_reg_l with (1 := Rlt_0_2).
912 913 914 915 916 917 918 919 920
apply Rle_trans with (-(2*x)).
2: right; ring.
apply Rle_trans with (round_flt (y - x)).
right; field.
apply round_le_generic...
apply generic_format_opp.
now apply FLT_format_double...
apply Rplus_le_reg_l with (2*x).
apply Rmult_le_reg_r with (/2).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
921
lra.
922 923 924 925 926 927 928 929
apply Rle_trans with 0;[idtac|right; ring].
apply Rle_trans with (2:=H).
right; unfold a, Rdiv; ring.
Qed.




BOLDO Sylvie's avatar
BOLDO Sylvie committed
930 931
Lemma avg_half_sub_between_aux: forall u v, format u -> format v -> u <= v ->
    u <= avg_half_sub u v <= v.
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
Proof with auto with typeclass_instances.
clear Fx Fy a av x y.
intros x y Fx Fy M.
split.
(* . *)
apply round_ge_generic...
apply Rplus_le_reg_l with (-x).
ring_simplify.
apply round_ge_generic...
apply generic_format_0.
unfold Rdiv; apply Rmult_le_pos.
apply round_ge_generic...
apply generic_format_0.
apply Rplus_le_reg_l with x.
now ring_simplify.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
947
lra.
948 949 950 951 952 953 954 955 956 957 958 959 960 961
(* . *)
apply round_le_generic...
assert (H:(0 <= round radix2 (FLT_exp emin prec) Zfloor (y-x))).
apply round_ge_generic...
apply generic_format_0.
apply Rplus_le_reg_l with x.
now ring_simplify.
destruct H as [H|H].
(* .. *)
pattern y at 2; replace y with (x + (y-x)) by ring.
apply Rplus_le_compat_l.
case (generic_format_EM radix2 (FLT_exp emin prec) (y-x)); intros K.
apply round_le_generic...
rewrite round_generic...
Guillaume Melquiond's avatar
Guillaume Melquiond committed
962
apply Rmult_le_reg_l with (1 := Rlt_0_2).
963 964 965 966 967 968 969 970
apply Rplus_le_reg_l with (2*x-y).
apply Rle_trans with x.
right; field.
apply Rle_trans with (1:=M).
right; field.
apply Rle_trans with (round radix2 (FLT_exp emin prec) Zfloor (y - x)).
apply round_le_generic...
apply generic_format_round...
Guillaume Melquiond's avatar
Guillaume Melquiond committed
971
apply Rmult_le_reg_l with (1 := Rlt_0_2).
972 973 974 975 976 977 978
apply Rle_trans with (round_flt (y - x)).
right; field.
case (round_DN_or_UP radix2 (FLT_exp emin prec) ZnearestE (y-x));
   intros H1; rewrite H1.
apply Rplus_le_reg_l with (-round radix2 (FLT_exp emin prec) Zfloor (y - x)).
ring_simplify.
now left.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
979
rewrite round_UP_DN_ulp.
980 981
apply Rplus_le_reg_l with (-round radix2 (FLT_exp emin prec) Zfloor (y - x)); ring_simplify.
apply round_DN_pt...
982
apply generic_format_ulp...
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
case (Rle_or_lt (bpow (emin + prec - 1))  (y-x)); intros P.
apply FLT_ulp_le_id...
apply Rle_trans with (2:=P).
apply bpow_le; unfold Prec_gt_0 in prec_gt_0_; omega.
contradict K.
apply FLT_format_plus_small...
now apply generic_format_opp.
rewrite Rabs_right.
apply Rle_trans with (bpow (emin+prec-1)).
left; exact P.
apply bpow_le; omega.
apply Rle_ge; apply Rplus_le_reg_l with x; now ring_simplify.
assumption.
apply round_DN_pt...
(* .. *)
case M; intros H1.
2: rewrite H1; replace (y-y) with 0 by ring.
2: rewrite round_0...
2: unfold Rdiv; rewrite Rmult_0_l.
2: rewrite round_0...
2: right; ring.
apply Rle_trans with (x+0).
2: rewrite Rplus_0_r; assumption.
apply Rplus_le_compat_l.
replace 0 with (round_flt (bpow emin/2)).
apply round_le...
unfold Rdiv; apply Rmult_le_compat_r.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1010
lra.
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
apply round_le_generic...
apply FLT_format_bpow...
omega.
case (Rle_or_lt (y-x) (bpow emin)); trivial.
intros H2.
contradict H.
apply Rlt_not_eq.
apply Rlt_le_trans with (bpow emin).
apply bpow_gt_0.
apply round_DN_pt...
apply FLT_format_bpow...
omega.
now left.
replace (bpow emin /2) with (bpow (emin-1)).
1025
unfold round, scaled_mantissa, cexp, FLT_exp.
1026
rewrite mag_bpow.
1027 1028 1029 1030 1031 1032 1033
replace (emin - 1 + 1 - prec)%Z with (emin-prec)%Z by ring.
rewrite Z.max_r.
2: unfold Prec_gt_0 in prec_gt_0_; omega.
rewrite <- bpow_plus.
replace (emin-1+-emin)%Z with (-1)%Z by ring.
replace (ZnearestE (bpow (-1))) with 0%Z.
unfold F2R; simpl; ring.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1034
change (bpow (-1)) with (/2).
1035 1036 1037 1038 1039 1040
simpl; unfold Znearest.
replace (Zfloor (/2)) with 0%Z.
rewrite Rcompare_Eq.
reflexivity.
simpl; ring.
apply sym_eq, Zfloor_imp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1041
simpl ; lra.
1042 1043 1044 1045
unfold Zminus; rewrite bpow_plus.
reflexivity.
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
1046
Lemma avg_half_sub_between: Rmin x y <= av <= Rmax x y.
1047 1048 1049 1050 1051
Proof with auto with typeclass_instances.
case (Rle_or_lt x y); intros M.
(* x <= y *)
rewrite Rmin_left; try exact M.
rewrite Rmax_right; try exact M.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1052
now apply avg_half_sub_between_aux.
1053 1054 1055 1056
(* y < x *)
rewrite Rmin_right; try now left.
rewrite Rmax_left; try now left.
unfold av; rewrite <- (Ropp_involutive x); rewrite <- (Ropp_involutive y).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1057
rewrite avg_half_sub_symmetry_Ropp.
1058
split; apply Ropp_le_contravar.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1059
apply avg_half_sub_between_aux.
1060 1061 1062
now apply generic_format_opp.
now apply generic_format_opp.
apply Ropp_le_contravar; now left.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1063
apply avg_half_sub_between_aux.
1064 1065 1066 1067 1068 1069
now apply generic_format_opp.
now apply generic_format_opp.
apply Ropp_le_contravar; now left.
Qed.


BOLDO Sylvie's avatar
BOLDO Sylvie committed
1070
Lemma avg_half_sub_zero: a = 0 -> av = 0.
1071 1072 1073 1074 1075 1076 1077 1078
Proof with auto with typeclass_instances.
intros H.
assert (y=-x).
apply Rplus_eq_reg_l with x.
apply Rmult_eq_reg_r with (/2).
apply trans_eq with a.
reflexivity.
rewrite H; ring.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1079
lra.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1080
unfold av, avg_half_sub.
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
rewrite H0.
replace (-x-x) with (-(2*x)) by ring.
rewrite round_generic with (x:=(-(2*x)))...
replace (-(2*x)/2) with (-x) by field.
rewrite round_generic with (x:=-x)...
replace (x+-x) with 0 by ring.
apply round_0...
now apply generic_format_opp.
apply generic_format_opp.
now apply FLT_format_double.
Qed.


1094
Lemma avg_half_sub_no_underflow_aux_aux: forall z:Z, (0 < z)%Z ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1095
    (ZnearestE (IZR z / 2) < z)%Z.
1096 1097 1098
Proof.
intros z H1.
case (Zle_lt_or_eq 1 z); [omega|intros H2|intros H2].
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1099 1100 1101
apply lt_IZR.
apply Rplus_lt_reg_r with (- ((IZR z)/2)).
apply Rle_lt_trans with (-(((IZR z) /2) - IZR (ZnearestE (IZR z / 2)))).
1102 1103 1104
right; ring.
apply Rle_lt_trans with (1:= RRle_abs _).
rewrite Rabs_Ropp.
1105
apply Rle_lt_trans with (1:=Znearest_half (fun x => negb (Z.even x)) _).
1106
apply Rle_lt_trans with (1*/2);[right; ring|idtac].
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1107
apply Rlt_le_trans with ((IZR z)*/2);[idtac|right; field].
1108
apply Rmult_lt_compat_r.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1109
lra.
1110
now apply IZR_lt.
1111 1112 1113 1114 1115 1116 1117 1118
rewrite <- H2.
unfold Znearest; simpl.
replace (Zfloor (1 / 2)) with 0%Z.
rewrite Rcompare_Eq.
simpl; omega.
simpl; field.
unfold Rdiv; rewrite Rmult_1_l.
apply sym_eq, Zfloor_imp.