Average.v 43.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 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 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
Require Import Fcore.
Require Import Fprop_plus_error.

Open Scope R_scope.

Section av1.


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)).
Notation round_flt :=(round radix2 (FLT_exp emin prec) ZnearestE). 
Notation ulp_flt :=(ulp radix2 (FLT_exp emin prec)).
Notation cexp := (canonic_exp radix2 (FLT_exp emin prec)).

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...
destruct Fu as (uf, (H1,(H2,H3))).
exists (Float radix2 (Fnum uf) (Fexp uf+1)).
split.
rewrite H1; unfold F2R; simpl.
rewrite bpow_plus, bpow_1.
simpl;ring.
split.
now simpl.
simpl; apply Zle_trans with (1:=H3).
omega.
Qed.


Lemma FLT_format_half: forall u, 
   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...
destruct Fu as ((n,e),(H1,(H2,H3))).
simpl in H1, H2, H3.
apply generic_format_FLT.
exists (Float radix2 n (e-1)).
split; simpl.
rewrite H1; unfold F2R; simpl.
unfold Zminus; rewrite bpow_plus.
simpl; unfold Rdiv; ring.
split;[assumption|idtac].
assert (prec + emin < prec +e)%Z;[idtac|omega].
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.
rewrite <- Z2R_abs.
rewrite <- Z2R_Zpower.
now apply Z2R_lt.
unfold Prec_gt_0 in prec_gt_0_; omega.
Qed.



Lemma FLT_ulp_le_id: forall u, bpow emin <= u -> ulp_flt u <= u.
Proof with auto with typeclass_instances.
intros u H.
case (Rle_or_lt (bpow (emin+prec-1)) u); intros Hu.
unfold ulp; rewrite canonic_exp_FLT_FLX.
unfold canonic_exp, FLX_exp.
destruct (ln_beta radix2 u) as (e,He); simpl.
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.
unfold ulp; rewrite canonic_exp_FLT_FIX.
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).
intros u.
pattern 2 at 2; replace 2 with (bpow 1) by reflexivity.
unfold ulp; rewrite <- bpow_plus.
apply bpow_le.
case (Rle_or_lt (bpow (emin+prec-1)) (Rabs u)); intros Hu.
(* *)
rewrite canonic_exp_FLT_FLX.
rewrite canonic_exp_FLT_FLX; trivial.
unfold canonic_exp, FLX_exp.
replace 2 with (bpow 1) by reflexivity.
rewrite Rmult_comm, ln_beta_mult_bpow.
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.
rewrite Rabs_right.
now auto with real.
apply Rle_ge; now auto with real.
(* *)
case (Req_dec u 0); intros K.
rewrite K, Rmult_0_r.
omega.
rewrite canonic_exp_FLT_FIX.
rewrite canonic_exp_FLT_FIX; trivial.
unfold FIX_exp, canonic_exp; omega.
apply Rlt_le_trans with (1:=Hu).
apply bpow_le; omega.
apply Rmult_integral_contrapositive_currified; trivial.
apply Rgt_not_eq, Rlt_gt; now auto with real.
rewrite Rabs_mult.
rewrite Rabs_right.
2: apply Rle_ge; now auto with real.
apply Rlt_le_trans with (2*bpow (emin + prec - 1)).
apply Rmult_lt_compat_l.
now auto with real.
assumption.
replace 2 with (bpow 1) by reflexivity.
rewrite <- bpow_plus.
apply bpow_le; omega.
Qed.



Definition average1 (x y : R) :=round_flt(round_flt(x+y)/2).

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

Let a:=(x+y)/2.
Let av:=average1 x y.


Lemma average1_symmetry: forall u v, average1 u v = average1 v u.
Proof.
intros u v; unfold average1.
rewrite Rplus_comm; reflexivity.
Qed.

Lemma average1_symmetry_Ropp: forall u v, average1 (-u) (-v) = - average1 u v.
Proof.
intros u v; unfold average1.
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.

Lemma average1_same_sign_1: 0 <= a -> 0 <= av.
Proof with auto with typeclass_instances.
intros H.
apply round_ge_generic...
apply generic_format_0.
apply Rmult_le_pos.
apply round_ge_generic...
apply generic_format_0.
apply Rmult_le_reg_r with (/2).
auto with real.
rewrite Rmult_0_l; exact H.
auto with real.
Qed.

Lemma average1_same_sign_2: a <= 0-> av <= 0.
Proof with auto with typeclass_instances.
intros H.
apply round_le_generic...
apply generic_format_0.
replace 0 with (0*/2) by ring.
apply Rmult_le_compat_r.
auto with real.
apply round_le_generic...
apply generic_format_0.
apply Rmult_le_reg_r with (/2).
auto with real.
rewrite Rmult_0_l; exact H.
Qed.

Lemma average1_between: Rmin x y <= av <= Rmax x y.
Proof with auto with typeclass_instances.
assert (forall u v, format u -> format v -> u <= v -> u <= average1 u v <= v).
(* *)
intros u v Fu Fv H; split.
apply round_ge_generic...
apply Rmult_le_reg_l with 2.
auto with real.
apply Rle_trans with (round_flt (u + v)).
2: right; field.
apply round_ge_generic...
now apply FLT_format_double.
replace (2*u) with (u+u) by ring.
now apply Rplus_le_compat_l.
apply round_le_generic...
apply Rmult_le_reg_l with 2.
auto with real.
apply Rle_trans with (round_flt (u + v)).
right; field.
apply round_le_generic...
now apply FLT_format_double.
replace (2*v) with (v+v) by ring.
now apply Rplus_le_compat_r.
(* *)
case (Rle_or_lt x y); intros H1.
rewrite Rmin_left; try exact H1.
rewrite Rmax_right; try exact H1.
now apply H.
rewrite Rmin_right; try (left;exact H1).
rewrite Rmax_left; try (left;exact H1).
unfold av; rewrite average1_symmetry.
apply H; trivial; left; exact H1.
Qed.


Lemma average1_zero: a = 0 -> av = 0.
Proof with auto with typeclass_instances.
intros H1; unfold av, average1.
replace (x+y) with 0.
rewrite round_0...
unfold Rdiv; rewrite Rmult_0_l.
rewrite round_0...
apply Rmult_eq_reg_r with (/2).
rewrite Rmult_0_l, <- H1; reflexivity.
apply Rgt_not_eq, Rlt_gt.
auto with real.
Qed.



Lemma average1_no_underflow: (bpow emin) <= Rabs a -> av <> 0.
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.
(* *)
apply abs_round_ge_generic...
apply FLT_format_bpow...
omega.
apply Rmult_le_reg_l with 2.
auto with real.
apply Rle_trans with (Rabs (round_flt (x + y))).
apply abs_round_ge_generic...
apply FLT_format_double.
apply FLT_format_bpow...
omega.
apply Rmult_le_reg_l with (/2).
auto with real.
apply Rle_trans with (bpow emin).
right; field.
apply Rle_trans with (1:=H).
right; unfold a.
unfold Rdiv; rewrite Rabs_mult.
rewrite (Rabs_right (/2)).
ring.
apply Rle_ge; auto with real.
right; unfold Rdiv; rewrite Rabs_mult.
rewrite (Rabs_right (/2)).
field.
apply Rle_ge; auto with real.
Qed.


Lemma average1_correct: Rabs (av -a) <= /2*ulp_flt a.
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.
replace av with (round_flt (x + y) / 2).
apply Rmult_le_reg_l with 2.
auto with real.
apply Rle_trans with (Rabs (round_flt (x + y) - (x+y))).
rewrite <- (Rabs_right 2) at 1.
rewrite <- Rabs_mult.
right; apply f_equal.
unfold a; field.
apply Rle_ge; auto with real.
apply Rle_trans with (/2*ulp_flt (x+y)).
apply ulp_half_error...
right; apply trans_eq with (/2*(2*ulp_flt a)).
2: ring.
apply f_equal.
unfold ulp, a.
pattern 2 at 1; replace 2 with (bpow 1) by reflexivity.
rewrite <- bpow_plus.
apply f_equal.
unfold Rdiv; replace (/2) with (bpow (-1)) by reflexivity.
rewrite canonic_exp_FLT_FLX.
rewrite canonic_exp_FLT_FLX.
unfold canonic_exp, FLX_exp.
rewrite ln_beta_mult_bpow.
ring.
intros H1; rewrite H1, Rabs_R0 in H.
contradict H; apply Rlt_not_le, bpow_gt_0.
rewrite Rabs_mult.
rewrite (Rabs_right (bpow (-1))).
unfold Zminus; rewrite bpow_plus.
apply Rmult_le_compat_r.
apply bpow_ge_0.
rewrite Zplus_comm; exact H.
apply Rle_ge, bpow_ge_0.
apply Rle_trans with (2:=H).
apply bpow_le; omega.
apply sym_eq, round_generic...
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.
unfold av, average1.
replace (round_flt (x + y)) with (x+y).
unfold a; apply ulp_half_error...
apply sym_eq, round_generic...
apply FLT_format_plus_small...
left; assumption.
Qed.

Lemma average1_correct_weak: Rabs (av -a) <= 3/2*ulp_flt a.
Proof with auto with typeclass_instances.
apply Rle_trans with (1:=average1_correct).
apply Rmult_le_compat_r.
unfold ulp; apply bpow_ge_0.
apply Rle_trans with (1/2); unfold Rdiv.
right; ring.
apply Rmult_le_compat_r.
now auto with real.
apply Rplus_le_reg_l with (-1); ring_simplify.
now auto with real.
Qed.



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


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)).
Notation round_flt :=(round radix2 (FLT_exp emin prec) ZnearestE). 
Notation ulp_flt :=(ulp radix2 (FLT_exp emin prec)).
Notation cexp := (canonic_exp radix2 (FLT_exp emin prec)).

Definition average3 (x y : R) :=round_flt(x+round_flt(round_flt(y-x)/2)).

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

Let a:=(x+y)/2.
Let av:=average3 x y.


Lemma average3_symmetry_Ropp: forall u v, average3 (-u) (-v) = - average3 u v.
intros u v; unfold average3.
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.


Lemma average3_same_sign_1: 0 <= a -> 0 <= av.
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.
apply Rmult_le_reg_l with 2.
auto with real.
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).
auto with real.
apply Rle_trans with 0;[right; ring|idtac].
apply Rle_trans with (1:=H).
right; unfold a, Rdiv; ring.
Qed.

Lemma average3_same_sign_2: a <= 0-> av <= 0.
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.
apply Rmult_le_reg_l with 2.
auto with real.
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).
auto with real.
apply Rle_trans with 0;[idtac|right; ring].
apply Rle_trans with (2:=H).
right; unfold a, Rdiv; ring.
Qed.




Lemma average3_between_aux: forall u v, format u -> format v -> u <= v ->
    u <= average3 u v <= v.
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.
auto with real.
(* . *)
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...
apply Rmult_le_reg_l with 2.
auto with real.
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...
apply Rmult_le_reg_l with 2.
auto with real.
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.
rewrite ulp_DN_UP.
apply Rplus_le_reg_l with (-round radix2 (FLT_exp emin prec) Zfloor (y - x)); ring_simplify.
apply round_DN_pt...
unfold ulp.
apply FLT_format_bpow...
apply Z.le_max_r.
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.
auto with real.
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)).
unfold round, scaled_mantissa, canonic_exp, FLT_exp.
rewrite ln_beta_bpow.
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.
simpl; unfold Znearest.
replace (Zfloor (/2)) with 0%Z.
rewrite Rcompare_Eq.
reflexivity.
simpl; ring.
apply sym_eq, Zfloor_imp.
simpl; split.
auto with real.
apply Rmult_lt_reg_l with 2.
auto with real.
apply Rle_lt_trans with 1.
right; field.
rewrite Rmult_1_r.
auto with real.
unfold Zminus; rewrite bpow_plus.
reflexivity.
Qed.

Lemma average3_between: Rmin x y <= av <= Rmax x y.
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.
now apply average3_between_aux.
(* y < x *)
rewrite Rmin_right; try now left.
rewrite Rmax_left; try now left.
unfold av; rewrite <- (Ropp_involutive x); rewrite <- (Ropp_involutive y).
rewrite average3_symmetry_Ropp.
split; apply Ropp_le_contravar.
apply average3_between_aux.
now apply generic_format_opp.
now apply generic_format_opp.
apply Ropp_le_contravar; now left.
apply average3_between_aux.
now apply generic_format_opp.
now apply generic_format_opp.
apply Ropp_le_contravar; now left.
Qed.


Lemma average3_zero: a = 0 -> av = 0.
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.
apply Rgt_not_eq, Rlt_gt.
auto with real.
unfold av, average3.
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.


Lemma average3_no_underflow_aux_aux: forall z:Z, (0 < z)%Z -> 
    (ZnearestE (Z2R z / 2) < z)%Z.
Proof.
intros z H1.
case (Zle_lt_or_eq 1 z); [omega|intros H2|intros H2].
apply lt_Z2R.
apply Rplus_lt_reg_r with (- ((Z2R z)/2)).
apply Rle_lt_trans with (-(((Z2R z) /2) - Z2R (ZnearestE (Z2R z / 2)))).
right; ring.
apply Rle_lt_trans with (1:= RRle_abs _).
rewrite Rabs_Ropp.
apply Rle_lt_trans with (1:=Znearest_N (fun x => negb (Zeven x)) _).
apply Rle_lt_trans with (1*/2);[right; ring|idtac].
apply Rlt_le_trans with ((Z2R z)*/2);[idtac|right; field].
apply Rmult_lt_compat_r.
auto with real.
replace 1 with (Z2R 1) by reflexivity.
now apply Z2R_lt.
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.
simpl; split.
auto with real.
apply Rmult_lt_reg_l with 2.
auto with real.
apply Rle_lt_trans with 1.
right; field.
rewrite Rmult_1_r.
auto with real.
Qed.


Lemma average3_no_underflow_aux1: forall f, format f -> 0 < f ->
  f <= round_flt (f/2) -> False.
Proof with auto with typeclass_instances.
intros f Ff Hf1 Hf2.
apply FLT_format_generic in Ff...
destruct Ff as (g, (H1,(H2,H3))).
case (Zle_lt_or_eq emin (Fexp g)); try exact H3; intros H4.
contradict Hf2.
apply Rlt_not_le.
rewrite round_generic...
apply Rplus_lt_reg_l with (-(f/2)).
apply Rle_lt_trans with 0;[right; ring|idtac].
apply Rlt_le_trans with (f*/2);[idtac|right;field].
apply Rmult_lt_0_compat; try assumption.
auto with real.
apply generic_format_FLT.
exists (Float radix2 (Fnum g) (Fexp g-1)).
split.
rewrite H1; unfold F2R; simpl.
unfold Zminus; rewrite bpow_plus.
simpl; field.
split.
now simpl.
simpl; omega.
contradict Hf2; apply Rlt_not_le.
unfold round, scaled_mantissa.
replace (cexp (f/2)) with emin.
rewrite H1; unfold F2R; simpl.
rewrite <- H4.
apply Rmult_lt_compat_r.
apply bpow_gt_0.
apply Z2R_lt.
replace (Z2R (Fnum g) * bpow emin / 2 * bpow (- emin)) with (Z2R (Fnum g) /2).
apply average3_no_underflow_aux_aux.
apply lt_Z2R.
apply Rmult_lt_reg_r with (bpow (Fexp g)).
apply bpow_gt_0.
rewrite Rmult_0_l.
apply Rlt_le_trans with (1:=Hf1).
right; rewrite H1; reflexivity.
unfold Rdiv; apply trans_eq with (Z2R (Fnum g) * / 2 * (bpow (- emin)*bpow emin)).
rewrite <- bpow_plus.
ring_simplify (-emin+emin)%Z.
simpl; ring.
ring.
apply sym_eq, canonic_exp_FLT_FIX.
apply Rgt_not_eq, Rlt_gt.
unfold Rdiv; apply Rmult_lt_0_compat; try assumption.
auto with real.
rewrite H1; unfold F2R, Rdiv; simpl.
replace (/2) with (bpow (-1)) by reflexivity.
rewrite Rmult_assoc, <- bpow_plus.
rewrite Rabs_mult.
rewrite (Rabs_right (bpow _)).
2: apply Rle_ge, bpow_ge_0.
rewrite (Zplus_comm emin _).
rewrite (bpow_plus _ prec _).
apply Rmult_lt_compat.
apply Rabs_pos.
apply bpow_ge_0.
rewrite <- Z2R_Zpower, <- Z2R_abs.
now apply Z2R_lt.
unfold Prec_gt_0 in prec_gt_0_; omega.
rewrite <- H4; apply bpow_lt.
omega.
Qed.


Lemma average3_no_underflow_aux2: forall u v, format u -> format v -> 
    (0 <= u /\ 0 <= v) \/ (u <= 0 /\ v <= 0) ->
    u <= v ->
   (bpow emin) <= Rabs ((u+v)/2) -> average3 u v <> 0.
Proof with auto with typeclass_instances.
clear Fx Fy a av x y; intros x y Fx Fy same_sign xLey H; unfold average3.
intros J.
apply round_plus_eq_zero in J...
2: apply generic_format_round...
assert (H1:x <= 0).
apply Rplus_le_reg_r with (round_flt (round_flt (y - x) / 2)).
rewrite J, Rplus_0_l.
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.
auto with real.
destruct H1 as [H1|H1].
(* *)
destruct same_sign as [(H2,H3)|(_,H2)].
contradict H2; now apply Rlt_not_le.
apply average3_no_underflow_aux1 with (-x).
now apply generic_format_opp.
rewrite <- Ropp_0; now apply Ropp_lt_contravar.
apply Rle_trans with (round_flt (round_flt (y - x) / 2)).
apply Rplus_le_reg_l with x.
rewrite J; right; ring.
apply round_le...
unfold Rdiv; apply Rmult_le_compat_r.
auto with real.
apply round_le_generic...
now apply generic_format_opp.
apply Rplus_le_reg_l with x.
now ring_simplify.
(* *)
rewrite H1 in J, H.
rewrite Rplus_0_l in H.
contradict J; apply Rgt_not_eq, Rlt_gt.
rewrite Rplus_0_l.
unfold Rminus; rewrite Ropp_0, Rplus_0_r.
rewrite round_generic with (x:=y)...
apply Rlt_le_trans with (bpow emin).
apply bpow_gt_0.
apply round_ge_generic...
apply FLT_format_bpow...
omega.
apply Rle_trans with (1:=H).
right; apply Rabs_right.
apply Rle_ge; unfold Rdiv; apply Rmult_le_pos.
rewrite <- H1; assumption.
auto with real.
Qed.

Lemma average3_no_underflow_aux3: forall u v, format u -> format v -> 
    (0 <= u /\ 0 <= v) \/ (u <= 0 /\ v <= 0) ->
   (bpow emin) <= Rabs ((u+v)/2) -> average3 u v <> 0.
Proof with auto with typeclass_instances.
clear Fx Fy a av x y; intros x y Fx Fy.
intros same_sign H.
case (Rle_or_lt x y); intros H1.
now apply average3_no_underflow_aux2.
rewrite <- (Ropp_involutive x); rewrite <- (Ropp_involutive y).
rewrite average3_symmetry_Ropp.
apply Ropp_neq_0_compat.
apply average3_no_underflow_aux2.
now apply generic_format_opp.
now apply generic_format_opp.
rewrite <- Ropp_0; case same_sign; intros (T1,T2).
right; split; now apply Ropp_le_contravar.
left; split; now apply Ropp_le_contravar.
apply Ropp_le_contravar; now left.
apply Rle_trans with (1:=H).
rewrite <- Rabs_Ropp.
right; apply f_equal.
unfold Rdiv; ring.
Qed.


Lemma average3_no_underflow: 
  (0 <= x /\ 0 <= y) \/ (x <= 0 /\ y <= 0) ->
  (bpow emin) <= Rabs a -> av <> 0.
Proof with auto with typeclass_instances.
intros; now apply average3_no_underflow_aux3.
Qed.



Lemma average3_correct_aux: forall u v, format u -> format v -> u <= v ->
     (0 <= u /\ 0 <= v) \/ (u <= 0 /\ v <= 0) ->
     0 < Rabs ((u+v)/2) < bpow emin ->
     Rabs (average3 u v -((u+v)/2)) <= 3/2 * ulp_flt ((u+v)/2).
Proof with auto with typeclass_instances.
clear Fx Fy a av x y.
intros x y Fx Fy xLey same_sign.
pose (a:=(x+y)/2); fold a.
(* mostly forward proof *)
intros (H1,H2).
apply generic_format_FIX_FLT,FIX_format_generic in Fx.
apply generic_format_FIX_FLT,FIX_format_generic in Fy.
destruct Fx as ((nx,ex),(J1,J2)).
destruct Fy as ((ny,ey),(J3,J4)); simpl in J2, J4.
(* a is bpow emin /2 *)
assert (a = Z2R (nx+ny) * bpow (emin-1)).
unfold a; rewrite J1, J3; unfold F2R; rewrite J2,J4; simpl.
unfold Zminus; rewrite bpow_plus, Z2R_plus; simpl; field.
assert (Z.abs (nx+ny) = 1)%Z.
assert (0 < Z.abs (nx+ny) < 2)%Z;[idtac|omega].
split; apply lt_Z2R; simpl; rewrite Z2R_abs; 
 apply Rmult_lt_reg_l with (bpow (emin-1)); try apply bpow_gt_0.
rewrite Rmult_0_r.
apply Rlt_le_trans with (1:=H1).
right; rewrite H, Rabs_mult.
rewrite (Rabs_right (bpow (emin -1))).
ring.
apply Rle_ge, bpow_ge_0.
apply Rle_lt_trans with (Rabs a).
right; rewrite H, Rabs_mult.
rewrite (Rabs_right (bpow (emin -1))).
ring.
apply Rle_ge, bpow_ge_0.
apply Rlt_le_trans with (1:=H2).
right; unfold Zminus; rewrite bpow_plus.
simpl; field.
(* only 2 possible values for x and y *)
assert (((nx=0)/\ (ny=1)) \/ ((nx=-1)/\(ny=0)))%Z.
assert (nx <= ny)%Z.
apply le_Z2R.
apply Rmult_le_reg_r with (bpow emin).
apply bpow_gt_0.
apply Rle_trans with x.
right; rewrite J1,J2; reflexivity.
apply Rle_trans with (1:=xLey).
right; rewrite J3,J4; reflexivity.
case same_sign; intros (L1,L2).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
874 875
rewrite J1 in L1; apply F2R_ge_0_reg in L1; simpl in L1.
rewrite J3 in L2; apply F2R_ge_0_reg in L2; simpl in L2.
876 877 878 879
left.
rewrite Z.abs_eq in H0.
omega.
omega.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
880 881
rewrite J1 in L1; apply F2R_le_0_reg in L1; simpl in L1.
rewrite J3 in L2; apply F2R_le_0_reg in L2; simpl in L2.
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 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 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
right.
rewrite Z.abs_neq in H0.
omega.
omega.
(* look into the 2 possible cases *)
assert (G1:(round_flt (bpow emin/2) = 0)).
replace (bpow emin /2) with (bpow (emin-1)).
unfold round, scaled_mantissa.
rewrite canonic_exp_FLT_FIX.
unfold canonic_exp, FIX_exp; simpl.
rewrite <- bpow_plus.
replace (bpow (emin - 1 + - emin)) with (/2).
replace (ZnearestE (/ 2)) with 0%Z.
unfold F2R; simpl; ring.
unfold Znearest.
replace (Zfloor (/2)) with 0%Z.
rewrite Rcompare_Eq.
reflexivity.
simpl; ring.
apply sym_eq, Zfloor_imp.
simpl; split.
auto with real.
apply Rmult_lt_reg_l with 2.
auto with real.
apply Rle_lt_trans with 1.
right; field.
rewrite Rmult_1_r.
auto with real.
ring_simplify (emin-1+-emin)%Z; reflexivity.
apply Rgt_not_eq, Rlt_gt, bpow_gt_0.
rewrite Rabs_right.
apply bpow_lt.
unfold Prec_gt_0 in prec_gt_0_; omega.
apply Rle_ge, bpow_ge_0.
unfold Zminus; rewrite bpow_plus.
reflexivity.
case H3; intros (T1,T2).
unfold a, average3.
rewrite J1,J3,J2,J4,T1,T2; unfold F2R; simpl.
rewrite Rmult_0_l, Rmult_1_l, 2!Rplus_0_l.
unfold Rminus; rewrite Ropp_0, Rplus_0_r.
rewrite (round_generic _ _ _ (bpow (emin)))...
2: apply FLT_format_bpow...
2: omega.
rewrite G1.
rewrite round_0...
rewrite Rplus_0_l, Rabs_Ropp.
rewrite Rabs_right.
2: apply Rle_ge, Rmult_le_pos.
2: apply bpow_ge_0.
2: now auto with real.
apply Rle_trans with ((3*ulp_flt (bpow emin / 2))/2);[idtac|right; unfold Rdiv; ring].
unfold Rdiv; apply Rmult_le_compat_r.
now auto with real.
apply Rle_trans with (3*bpow emin).
apply Rle_trans with (1*bpow emin).
right; ring.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rplus_le_reg_l with (-1); ring_simplify.
now auto with real.
apply Rmult_le_compat_l.
apply Fourier_util.Rle_zero_pos_plus1.
now auto with real.
unfold ulp; apply bpow_le.
unfold canonic_exp, FLT_exp.
apply Z.le_max_r.
unfold a, average3.
rewrite J1,J3,J2,J4,T1,T2; unfold F2R; simpl.
rewrite Rmult_0_l, Rplus_0_r.
replace (0 - -1 * bpow emin) with (bpow emin) by ring.
rewrite (round_generic _ _ _ (bpow (emin)))...
2: apply FLT_format_bpow...
2: omega.
rewrite G1.
replace (-1 * bpow emin + 0) with (-bpow emin) by ring.
rewrite round_generic...
2: apply generic_format_opp.
2: apply FLT_format_bpow...
2: omega.
replace (- bpow emin - -1 * bpow emin / 2) with (-((bpow emin)/2)) by field.
rewrite Rabs_Ropp.
rewrite Rabs_right.
replace (-1 * bpow emin / 2) with (-((bpow emin/2))) by field.
rewrite ulp_opp.
apply Rle_trans with ((3*ulp_flt (bpow emin / 2))/2);[idtac|right; unfold Rdiv; ring].
unfold Rdiv; apply Rmult_le_compat_r.
now auto with real.
apply Rle_trans with (3*bpow emin).
apply Rle_trans with (1*bpow emin).
right; ring.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rplus_le_reg_l with (-1); ring_simplify.
now auto with real.
apply Rmult_le_compat_l.
apply Fourier_util.Rle_zero_pos_plus1.
now auto with real.
unfold ulp; apply bpow_le.
unfold canonic_exp, FLT_exp.
apply Z.le_max_r.
apply Rle_ge, Rmult_le_pos.
apply bpow_ge_0.
now auto with real.
Qed.



Lemma average3_correct_aux2: forall u v, format u -> format v -> u <= v ->
     (0 <= u /\ 0 <= v) \/ (u <= 0 /\ v <= 0) ->
     Rabs (average3 u v -((u+v)/2)) <= 3/2 * ulp_flt ((u+v)/2).
Proof with auto with typeclass_instances.
clear Fx Fy a av x y.
intros x y Fx Fy xLey same_sign.
pose (a:=(x+y)/2); fold a.
assert (T: forall z, Rabs (2*z) = 2* Rabs z).
intros z; rewrite Rabs_mult.
rewrite Rabs_right; try reflexivity.
apply Rle_ge; now auto with real.
destruct xLey as [xLty|xEqy].
(* when x < y *)
assert (B: x <= y) by now left.
assert (K1: a <> 0).
apply Rmult_integral_contrapositive_currified.
2: apply Rgt_not_eq, Rlt_gt; now auto with real.
intros L; case same_sign; intros (L1,L2).
absurd (0 <= x); try assumption.
apply Rlt_not_le.
apply Rlt_le_trans with y; try assumption.
apply Rplus_le_reg_l with x.
rewrite L, Rplus_0_r; assumption.
absurd (y <= 0); try assumption.
apply Rlt_not_le.
apply Rle_lt_trans with x; try assumption.
apply Rplus_le_reg_r with y.
rewrite L, Rplus_0_l; assumption.
(* . initial lemma *)
assert (Y:(Rabs (round_flt (y - x) - (y-x)) <= ulp_flt a)).
apply Rle_trans with (/2*ulp_flt (y-x)).
apply ulp_half_error...
apply Rmult_le_reg_l with 2.
now auto with real.
rewrite <- Rmult_assoc, Rinv_r, Rmult_1_l.
2: apply Rgt_not_eq, Rlt_gt; now auto with real.
apply Rle_trans with (ulp_flt (2*a)).
case same_sign; intros (T1,T2).
apply ulp_le...
apply Rplus_lt_reg_l with x; ring_simplify; assumption.
apply Rle_trans with (2*(a-x)).
right; unfold a; field.
apply Rmult_le_compat_l.
now auto with real.
apply Rplus_le_reg_l with (-a+x); ring_simplify; assumption.
rewrite <- (ulp_opp _ _ (2*a)).
apply ulp_le...
apply Rplus_lt_reg_l with x; ring_simplify; assumption.
apply Rle_trans with (2*(y-a)).
right; unfold a; field.
apply Rle_trans with (2*(-a));[idtac|right; ring].
apply Rmult_le_compat_l.
now auto with real.
apply Rplus_le_reg_l with a; ring_simplify; assumption.
unfold ulp.
replace 2 with (bpow 1) by reflexivity.
rewrite <- bpow_plus.
apply bpow_le.
unfold canonic_exp, FLT_exp.
rewrite Rmult_comm, ln_beta_mult_bpow; trivial.
rewrite <- Z.add_max_distr_l.
replace (ln_beta radix2 a + 1 - prec)%Z with (1 + (ln_beta radix2 a - prec))%Z by ring.
apply Z.max_le_compat_l.
omega.
(* . splitting case of av=0 *)
case (Rle_or_lt (bpow emin) (Rabs a)); intros D.
(* . main proof *)
unfold average3.
case (Rle_or_lt (bpow (prec+emin)) (y-x)); intros H1.
(* .. y-x is big enough: division by 2 is exact *)
cut (round_flt (round_flt (y - x) / 2) = round_flt (y - x) / 2).
intros Z; rewrite Z.
replace (round_flt (x + round_flt (y - x) / 2) - a) with
   ((round_flt (x + round_flt (y - x) / 2) - (x + round_flt (y - x) / 2)) +/2*(round_flt (y - x)-(y-x))).
2: unfold a; field.
apply Rle_trans with (1:=Rabs_triang _ _).
apply Rle_trans with (ulp_flt a+/2*ulp_flt a);[idtac|right; field].
apply Rplus_le_compat.
apply Rle_trans with (/2*ulp_flt (x + round_flt (y - x) / 2)).
apply ulp_half_error...
apply Rmult_le_reg_l with 2.
auto with real.
rewrite <- Rmult_assoc, Rinv_r, Rmult_1_l.
2: apply Rgt_not_eq, Rlt_gt; now auto with real.
apply Rle_trans with (2:=FLT_ulp_double _ _ _).
rewrite <- ulp_abs, <- (ulp_abs _ _ (2*a)).
apply ulp_le...
apply Rabs_pos_lt.
intros K2; apply average3_no_underflow_aux3 with x y; trivial.
unfold average3.
rewrite Z, K2, round_0...
replace (x + round_flt (y - x) / 2) with (a+/2*(round_flt (y - x) - (y - x))).
2: unfold a; field.
rewrite (T a).
rewrite Rmult_plus_distr_r, Rmult_1_l.
apply Rle_trans with (1:=Rabs_triang _ _).
apply Rplus_le_compat_l.
rewrite Rabs_mult.
rewrite Rabs_right.
2: apply Rle_ge; now auto with real.
apply Rmult_le_reg_l with 2.
now auto with real.
rewrite <- Rmult_assoc, Rinv_r, Rmult_1_l.
2: apply Rgt_not_eq, Rlt_gt; now auto with real.
apply Rle_trans with (1:=Y).
apply Rle_trans with (ulp_flt (2*a)).
rewrite <- ulp_abs, <- (ulp_abs _ _ (2*a)).
apply ulp_le...
now apply Rabs_pos_lt.
rewrite <- (Rmult_1_l (Rabs a)).
rewrite (T a).
apply Rmult_le_compat_r.
apply Rabs_pos.
now auto with real.
rewrite <- (T a).
rewrite <- ulp_abs.
apply FLT_ulp_le_id...
assert (H:generic_format radix2 (FIX_exp emin) (2*a)).
replace (2*a) with (x+y).
2: unfold a; field.
apply generic_format_FIX_FLT,FIX_format_generic in Fx.
apply generic_format_FIX_FLT,FIX_format_generic in Fy.
destruct Fx as (fx,(J1,J2)).
destruct Fy as (fy,(J3,J4)).
apply generic_format_FIX.
exists (Float radix2 (Fnum fx+Fnum fy) emin).
split;[idtac|reflexivity].
rewrite J1,J3; unfold F2R; simpl.
rewrite J2,J4, Z2R_plus; ring.
apply FIX_format_generic in H.
destruct H as ((n,e),(J1,J2)).
rewrite J1; unfold F2R; rewrite J2.
simpl; rewrite Rabs_mult.
pattern (bpow emin) at 1; rewrite <- (Rmult_1_l (bpow emin)).
rewrite (Rabs_right (bpow emin)).
2: apply Rle_ge, bpow_ge_0.
apply Rmult_le_compat_r.
apply bpow_ge_0.
rewrite <- Z2R_abs.
replace 1 with (Z2R 1) by reflexivity.
apply Z2R_le.
assert (0 < Z.abs n)%Z;[idtac|omega].
apply Z.abs_pos.
intros M; apply K1.
apply Rmult_eq_reg_l with 2.
2: apply Rgt_not_eq, Rlt_gt; now auto with real.
rewrite Rmult_0_r, J1,M; unfold F2R; simpl; ring.
rewrite Rabs_mult.
rewrite Rabs_right.
2: apply Rle_ge; auto with real.
apply Rmult_le_compat_l.
now auto with real.
exact Y.
apply round_generic...
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.
rewrite Rabs_right; try assumption.
apply Rle_ge; left; apply Rplus_lt_reg_l with x; now ring_simplify.
(* .. y-x is small: subtraction is exact *)
cut ((round_flt (y - x)= (y-x))).
intros Z; rewrite Z.
replace (x + round_flt ((y-x) / 2)) with (a+((round_flt ((y-x) / 2) - (y-x)/2))).
2: unfold a; field.
pose (eps:=(round_flt ((y - x) / 2) - (y - x) / 2)%R); fold eps.
assert (Rabs eps <= /2*bpow emin).
unfold eps.
apply Rle_trans with (1:=ulp_half_error _ _ _ _)...
right; apply f_equal.
unfold ulp; apply f_equal.
apply canonic_exp_FLT_FIX.
apply Rmult_integral_contrapositive_currified.
apply Rgt_not_eq, Rlt_gt.
apply Rplus_lt_reg_l with x; now ring_simplify.
apply Rgt_not_eq, Rlt_gt; now auto with real.
rewrite Zplus_comm; apply Rle_lt_trans with (2:=H1).
rewrite Rabs_right.
apply Rmult_le_reg_l with 2.
now auto with real.
apply Rplus_le_reg_l with (-y+2*x).
apply Rle_trans with x.
right; field.
left; now ring_simplify.
apply Rle_ge, Rmult_le_pos.
apply Rplus_le_reg_l with x; now ring_simplify.
now auto with real.
replace (round_flt (a + eps) - a) with ((round_flt (a+eps) -(a+eps)) + eps) by ring.
apply Rle_trans with (1:=Rabs_triang _ _).
apply Rle_trans with (/2*ulp_flt (a+eps) + /2*bpow emin).
apply Rplus_le_compat.
apply ulp_half_error...
assumption.
apply Rmult_le_reg_l with 2.
now auto with real.
apply Rle_trans with (ulp_flt (a + eps)+bpow emin).
right; field.
apply Rle_trans with (2*ulp_flt a + ulp_flt a).
2: right; field.
apply Rplus_le_compat.
apply Rle_trans with (2:=FLT_ulp_double _ _ _).
rewrite <- ulp_abs, <- (ulp_abs _ _ (2*a)).
apply ulp_le...
apply Rabs_pos_lt.
intros K2; apply average3_no_underflow_aux3 with x y; trivial.
unfold average3.
rewrite Z.
replace (x + round_flt ((y - x) / 2)) with (a+eps).
rewrite K2, round_0...
unfold a, eps; field.
replace (x + round_flt (y - x) / 2) with (a+/2*(round_flt (y - x) - (y - x))).
2: unfold a; field.
rewrite (T a).
rewrite Rmult_plus_distr_r, Rmult_1_l.
apply Rle_trans with (1:=Rabs_triang _ _).
apply Rplus_le_compat_l.
apply Rle_trans with (2:=D).
rewrite <- (Rmult_1_l (bpow emin)).
apply Rle_trans with (1:=H).
apply Rmult_le_compat_r.
apply bpow_ge_0.
pattern 1 at 3; rewrite <- Rinv_1.
apply Rinv_le; now auto with real.
unfold ulp; apply bpow_le.
unfold canonic_exp, FLT_exp.
apply Z.le_max_r.
apply round_generic...
apply FLT_format_plus_small...
now apply generic_format_opp.
rewrite Rabs_right.
now left.
apply Rle_ge, Rplus_le_reg_l with x; now ring_simplify.
(* . when a = bpow emin /2 *)
apply average3_correct_aux; trivial.
split; trivial.
now apply Rabs_pos_lt.
(* . x = y *)
unfold average3,a.
rewrite xEqy.
replace (y-y) with 0 by ring.
rewrite round_0...
unfold Rdiv; rewrite Rmult_0_l.
rewrite round_0...
rewrite Rplus_0_r.
rewrite round_generic...
replace ((y+y)*/2) with y by field.
replace (y-y) with 0 by ring.
rewrite Rabs_R0.
apply Rmult_le_pos.
apply Rmult_le_pos.
apply Fourier_util.Rle_zero_pos_plus1; now auto with real.
now auto with real.
apply bpow_ge_0.
Qed.




(* tight example x=1/2 and y=2^p-1: error is 5/4 ulp *) 

Lemma average3_correct: (0 <= x /\ 0 <= y) \/ (x <= 0 /\ y <= 0) ->
     Rabs (av-a) <= 3/2 * ulp_flt a.
Proof with auto with typeclass_instances.
intros same_sign; case (Rle_or_lt x y); intros H.
now apply average3_correct_aux2.
unfold av, a.
rewrite <- (Ropp_involutive x), <- (Ropp_involutive y).
rewrite average3_symmetry_Ropp.
rewrite <- Rabs_Ropp.
replace (- (- average3 (- x) (- y) - (- - x + - - y) / 2)) with
   (average3 (-x) (-y) - ((-x+-y)/2)).
2: unfold Rdiv; ring.
apply Rle_trans with (3 / 2 * ulp_flt ((- x + - y) / 2)).
apply average3_correct_aux2.
now apply generic_format_opp.
now apply generic_format_opp.
apply Ropp_le_contravar; now left.
rewrite <- Ropp_0; case same_sign; intros (T1,T2).
right; split; now apply Ropp_le_contravar.
left; split; now apply Ropp_le_contravar.
right; apply f_equal.
rewrite <- ulp_opp.
apply f_equal.
unfold Rdiv; ring.
Qed.


(* Lemma average3_symmetry: forall u v, average3 u v = average3 v u.
   is false *)


End av3.

Section average.

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)).
Notation round_flt :=(round radix2 (FLT_exp emin prec) ZnearestE). 
Notation ulp_flt :=(ulp radix2 (FLT_exp emin prec)).
Notation cexp := (canonic_exp radix2 (FLT_exp emin prec)).

Definition average (x y : R) := 
   let samesign :=  match (Rle_bool 0 x), (Rle_bool 0 y) with
        true  , true   => true
      | false , false => true
      | _,_ => false
   end in
     if samesign then 
       match (Rle_bool (Rabs x) (Rabs y)) with
            true => average3 emin prec x y
          | false => average3 emin prec y x
        end
      else average1 emin prec x y.

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

Let a:=(x+y)/2.
Let av:=average x y.

Lemma average_symmetry: forall u v, average u v = average v u.
Proof.
intros u v; unfold average.
case (Rle_bool_spec 0 u); case (Rle_bool_spec 0 v); intros.
rewrite 2!Rabs_right; try apply Rle_ge; try assumption.
case (Rle_bool_spec u v); case (Rle_bool_spec v u); trivial.
intros; replace u with v; trivial; auto with real.
intros H1 H2; contradict H1; auto with real.
apply average1_symmetry.
apply average1_symmetry.
rewrite 2!Rabs_left; try assumption.
case (Rle_bool_spec (-u) (-v)); case (Rle_bool_spec (-v) (-u)); trivial.
intros; replace u with v; trivial; auto with real.
intros H1 H2; contradict H1; auto with real.
Qed.

Lemma average_symmetry_Ropp: forall u v, format u -> format v -> 
  average (-u) (-v) = - average u v.
Proof with auto with typeclass_instances.
(* first: nonnegative u *)
assert (forall u v, 0 <= u -> format u -> format v -> 
  average (-u) (-v) = - average u v).
intros u v Hu Fu Fv; unfold average.
rewrite 2!Rabs_Ropp.
destruct Hu as [Hu|Hu].
 (* 0 < u *)
 rewrite Rle_bool_false.
 2: apply Ropp_lt_cancel.
 2: now rewrite Ropp_involutive, Ropp_0.
 rewrite (Rle_bool_true 0 u); [idtac|now left].
 rewrite Rabs_right.
 2: apply Rle_ge; now left.
 destruct (total_order_T 0 v) as [Hv|Hv];[destruct Hv as [Hv|Hv] |idtac].
 (* . 0 < u and 0 < v *)
   rewrite Rle_bool_false.
   2: apply Ropp_lt_cancel.
   2: now rewrite Ropp_involutive, Ropp_0.
   rewrite (Rle_bool_true 0 v); [idtac|now left].
   rewrite Rabs_right.
   2: apply Rle_ge; now left.
   case (Rle_bool_spec u v);intros.
   apply average3_symmetry_Ropp.
   apply average3_symmetry_Ropp.
 (* . 0 < u and v = 0 *)
   rewrite <- Hv, Ropp_0, Rabs_R0.
   rewrite Rle_bool_true ;[idtac|now right].
   rewrite Rle_bool_false; try exact Hu.
   unfold average1, average3.
   unfold Rminus; rewrite Ropp_0, Rplus_0_l, 2!Rplus_0_r.
   rewrite (round_generic _ _ _ u); trivial.
   rewrite (round_generic _ _ _ (-u)).
   2: now apply generic_format_opp.
   rewrite <- round_NE_opp.
   rewrite <- round_NE_opp.
   rewrite (round_generic _ _ _ (round_flt (-(u/2)))).
   apply f_equal; field.
   apply generic_format_round...
 (* . 0 < u and v < 0 *)
   rewrite Rabs_left; trivial.
   rewrite Rle_bool_true.
   rewrite Rle_bool_false; trivial.
   apply average1_symmetry_Ropp.
   rewrite <- Ropp_0; apply Ropp_le_contravar.
   now left.
 (* u = 0 *)
   rewrite <- Hu, Ropp_0, Rabs_R0.
   rewrite Rle_bool_true.
   2: now right.
   rewrite (Rle_bool_true 0 (Rabs v)).
   2: apply Rabs_pos.
   destruct (total_order_T 0 v) as [Hv|Hv];[destruct Hv as [Hv|Hv] |idtac].
   (* . u=0 and 0 < v *)
   rewrite Rle_bool_false.
   rewrite Rle_bool_true.
   unfold average1, average3.
   unfold Rminus; rewrite Ropp_0, 2!Rplus_0_l, Rplus_0_r.
   rewrite (round_generic _ _ _ v); trivial.
   rewrite (round_generic _ _ _ (-v)).
   2: now apply generic_format_opp.
   rewrite <- round_NE_opp.
   rewrite <- round_NE_opp.
   rewrite (round_generic _ _ _ (round_flt (-(v/2)))).
   apply f_equal; field.
   apply generic_format_round...
   now left.
   rewrite <- Ropp_0; now apply Ropp_lt_contravar.
  (* . u=0 and v=0 *)
   rewrite <- Hv, Ropp_0.
   rewrite Rle_bool_true.
   2: now right.
   unfold average3.
   replace (0-0) with 0 by ring; rewrite round_0...
   unfold Rdiv; rewrite Rmult_0_l, round_0, Rplus_0_l...
  rewrite round_0...
  ring.
  (* . u=0 and v < 0 *)
  rewrite Rle_bool_true.
  rewrite Rle_bool_false.
   unfold average1, average3.
   unfold Rminus; rewrite Ropp_0, 2!Rplus_0_l, Rplus_0_r.
   rewrite (round_generic _ _ _ v); trivial.
   rewrite (round_generic _ _ _ (-v)).
   2: now apply generic_format_opp.
   rewrite <- round_NE_opp.
   rewrite (round_generic _ _ _ (round_flt (-v/2))).
   apply f_equal; field.
   apply generic_format_round...
   exact Hv.
   rewrite <- Ropp_0; apply Ropp_le_contravar; now left.
(* any u *)
intros u v Fu Fv.
case (Rle_or_lt 0 u).
intros Hu; now apply H.
intros Hu.
apply trans_eq with (- average (--u) (--v)).
rewrite (H (-u) (-v)).
ring.
rewrite <- Ropp_0; apply Ropp_le_contravar; now left.
apply generic_format_opp...
apply generic_format_opp...
apply f_equal, f_equal2; ring.
Qed.


Lemma average_same_sign_1: 0 <= a -> 0 <= av.
Proof with auto with typeclass_instances.
intros H; unfold av, average.
case (Rle_bool_spec 0 x); case (Rle_bool_spec 0 y); intros.
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_same_sign_1...
apply average3_same_sign_1...
now rewrite Rplus_comm.
apply average1_same_sign_1...
apply average1_same_sign_1...
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_same_sign_1...
apply average3_same_sign_1...
now rewrite Rplus_comm.
Qed.

Lemma average_same_sign_2: a <= 0-> av <= 0.
Proof with auto with typeclass_instances.
intros H; unfold av, average.
case (Rle_bool_spec 0 x); case (Rle_bool_spec 0 y); intros.
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_same_sign_2...
apply average3_same_sign_2...
now rewrite Rplus_comm.
apply average1_same_sign_2...
apply average1_same_sign_2...
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_same_sign_2...
apply average3_same_sign_2...
now rewrite Rplus_comm.
Qed.

Lemma average_correct: Rabs (av -a) <= 3/2 * ulp_flt a.
Proof with auto with typeclass_instances.
unfold av,a,average.
case (Rle_bool_spec 0 x); case (Rle_bool_spec 0 y); intros.
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_correct...
rewrite Rplus_comm.
apply average3_correct...
apply average1_correct_weak...
apply average1_correct_weak...
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_correct...
right; split; now left.
rewrite Rplus_comm.
apply average3_correct...
right; split; now left.
Qed.

Lemma average_between: Rmin x y <= av <= Rmax x y.
Proof with auto with typeclass_instances.
unfold av,a,average.
case (Rle_bool_spec 0 x); case (Rle_bool_spec 0 y); intros.
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_between...
rewrite Rmin_comm, Rmax_comm.
apply average3_between...
apply average1_between...
apply average1_between...
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_between...
rewrite Rmin_comm, Rmax_comm.
apply average3_between...
Qed.


Lemma average_zero: a = 0 -> av = 0.
Proof with auto with typeclass_instances.
unfold av,a,average.
case (Rle_bool_spec 0 x); case (Rle_bool_spec 0 y); intros.
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_zero...
apply average3_zero...
now rewrite Rplus_comm.
apply average1_zero...
apply average1_zero...
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_zero...
apply average3_zero...
now rewrite Rplus_comm.
Qed.


Lemma average_no_underflow: (bpow emin) <= Rabs a -> av <> 0.
Proof with auto with typeclass_instances.
unfold av,a,average.
case (Rle_bool_spec 0 x); case (Rle_bool_spec 0 y); intros.
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_no_underflow...
apply average3_no_underflow...
now rewrite Rplus_comm.
apply average1_no_underflow...
apply average1_no_underflow...
case (Rle_bool_spec (Rabs x) (Rabs y)); intros.
apply average3_no_underflow...
right; split; now left.
apply average3_no_underflow...
right; split; now left.
now rewrite Rplus_comm.
Qed.



End average.