Fcore_generic_fmt.v 24.9 KB
Newer Older
1 2 3 4
Require Import Fcore_Raux.
Require Import Fcore_defs.
Require Import Fcore_rnd.
Require Import Fcore_float_prop.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
5 6 7 8 9

Section RND_generic.

Variable beta : radix.

10
Notation bpow e := (bpow beta e).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
11 12 13

Variable fexp : Z -> Z.

14 15 16 17 18 19 20 21
Definition valid_exp :=
  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 ).

Variable prop_exp : valid_exp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
22

23 24 25 26 27
Definition canonic_exponent x :=
  fexp (projT1 (ln_beta beta x)).

Definition canonic (f : float beta) :=
  Fexp f = canonic_exponent (F2R f).
28

29 30 31
Definition scaled_mantissa x :=
  (x * bpow (- canonic_exponent x))%R.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
32
Definition generic_format (x : R) :=
33
  x = F2R (Float beta (Ztrunc (scaled_mantissa x)) (canonic_exponent x)).
34 35 36 37 38 39 40

(*
Theorem canonic_mantissa_0 :
  canonic_mantissa 0 = Z0.
Proof.
unfold canonic_mantissa.
rewrite Rmult_0_l.
41
exact (Zfloor_Z2R 0).
42 43
Qed.
*)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
44

Guillaume Melquiond's avatar
Guillaume Melquiond committed
45 46 47
Theorem generic_format_0 :
  generic_format 0.
Proof.
48
unfold generic_format, scaled_mantissa.
49 50 51 52 53 54 55 56 57 58 59 60
rewrite Rmult_0_l.
change (Ztrunc 0) with (Ztrunc (Z2R 0)).
now rewrite Ztrunc_Z2R, F2R_0.
Qed.

Theorem canonic_exponent_opp :
  forall x,
  canonic_exponent (-x) = canonic_exponent x.
Proof.
intros x.
unfold canonic_exponent.
now rewrite ln_beta_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
61 62
Qed.

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
(*
Theorem canonic_mantissa_opp :
  forall x,
  generic_format x ->
  canonic_mantissa (-x) = (- canonic_mantissa x)%Z.
Proof.
unfold generic_format, canonic_mantissa.
intros x Hx.
rewrite canonic_exponent_opp.
rewrite Hx at 1 3.
generalize (canonic_exponent x).
intros e.
clear.
unfold F2R. simpl.
rewrite Ropp_mult_distr_l_reverse.
rewrite Rmult_assoc, <- bpow_add, Zplus_opp_r.
rewrite Rmult_1_r.
rewrite <- opp_Z2R.
81
now rewrite 2!Zfloor_Z2R.
82 83 84
Qed.
*)

85 86 87 88 89
Theorem generic_format_bpow :
  forall e, (fexp (e + 1) <= e)%Z ->
  generic_format (bpow e).
Proof.
intros e H.
90
unfold generic_format, scaled_mantissa, canonic_exponent.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
rewrite ln_beta_bpow.
rewrite <- bpow_add.
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.
omega.
Qed.

Theorem generic_format_canonic_exponent :
  forall m e,
  (canonic_exponent (F2R (Float beta m e)) <= e)%Z ->
  generic_format (F2R (Float beta m e)).
Proof.
intros m e.
107
unfold generic_format, scaled_mantissa.
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
set (e' := canonic_exponent (F2R (Float beta m e))).
intros He.
unfold F2R at 3. simpl.
assert (H: (Z2R m * bpow e * bpow (- e') = Z2R (m * Zpower (radix_val beta) (e + -e')))%R).
rewrite Rmult_assoc, <- bpow_add, mult_Z2R.
rewrite Z2R_Zpower.
apply refl_equal.
now apply Zle_left.
rewrite H, Ztrunc_Z2R.
unfold F2R. simpl.
rewrite <- H.
rewrite Rmult_assoc, <- bpow_add, Zplus_opp_l.
now rewrite Rmult_1_r.
Qed.

Theorem canonic_opp :
  forall m e,
  canonic (Float beta m e) ->
  canonic (Float beta (-m) e).
Proof.
intros m e H.
unfold canonic.
now rewrite <- opp_F2R, canonic_exponent_opp.
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.

150
Theorem scaled_mantissa_generic :
151 152
  forall x,
  generic_format x ->
153
  scaled_mantissa x = Z2R (Ztrunc (scaled_mantissa x)).
154 155
Proof.
intros x Hx.
156
unfold scaled_mantissa.
157 158 159 160 161 162
pattern x at 1 3 ; rewrite Hx.
unfold F2R. simpl.
rewrite Rmult_assoc, <- bpow_add, Zplus_opp_r, Rmult_1_r.
now rewrite Ztrunc_Z2R.
Qed.

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
Theorem scaled_mantissa_bpow :
  forall x,
  (scaled_mantissa x * bpow (canonic_exponent x))%R = x.
Proof.
intros x.
unfold scaled_mantissa.
rewrite Rmult_assoc, <- bpow_add, Zplus_opp_l.
apply Rmult_1_r.
Qed.

Theorem scaled_mantissa_opp :
  forall x,
  scaled_mantissa (-x) = (-scaled_mantissa x)%R.
Proof.
intros x.
unfold scaled_mantissa.
rewrite canonic_exponent_opp.
now rewrite Ropp_mult_distr_l_reverse.
Qed.

183 184 185 186 187
Theorem generic_format_opp :
  forall x, generic_format x -> generic_format (-x).
Proof.
intros x Hx.
unfold generic_format.
188 189 190 191
rewrite scaled_mantissa_opp, canonic_exponent_opp.
rewrite Ztrunc_opp.
rewrite <- opp_F2R.
now apply f_equal.
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
Qed.

Theorem canonic_exponent_fexp_pos :
  forall x ex,
  (bpow (ex - 1) <= x < bpow ex)%R ->
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
unfold canonic_exponent.
rewrite <- (Rabs_pos_eq x) in Hx.
now rewrite ln_beta_unique with (1 := Hx).
apply Rle_trans with (2 := proj1 Hx).
apply bpow_ge_0.
Qed.

Theorem canonic_exponent_fexp_neg :
  forall x ex,
  (bpow (ex - 1) <= -x < bpow ex)%R ->
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
unfold canonic_exponent.
rewrite <- (Rabs_left1 x) in Hx.
now rewrite ln_beta_unique with (1 := Hx).
apply Ropp_le_cancel.
rewrite Ropp_0.
apply Rle_trans with (2 := proj1 Hx).
apply bpow_ge_0.
Qed.

Theorem canonic_exponent_fexp :
  forall x ex,
  (bpow (ex - 1) <= Rabs x < bpow ex)%R ->
  canonic_exponent x = fexp ex.
Proof.
intros x ex Hx.
unfold canonic_exponent.
now rewrite ln_beta_unique with (1 := Hx).
Qed.

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.
239
split.
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
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.
rewrite Rmult_assoc, <- bpow_add, Zplus_opp_l.
rewrite Rmult_1_r, Rmult_1_l.
apply Rlt_le_trans with (1 := proj2 Hx).
now apply -> bpow_le.
Qed.

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.
274 275
Qed.

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
Theorem generic_format_discrete :
  forall x m,
  let e := canonic_exponent x in
  (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.
now rewrite scaled_mantissa_bpow.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
295
Theorem generic_DN_pt_large_pos_ge_pow_aux :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
296 297
  forall x ex,
  (fexp ex < ex)%Z ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
298 299
  (bpow (ex - 1) <= x)%R ->
  (bpow (ex - 1) <= F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)))%R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
300
Proof.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
301
intros x ex He1 Hx1.
302
unfold F2R. simpl.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
303
replace (ex - 1)%Z with ((ex - 1 - fexp ex) + fexp ex)%Z by ring.
304
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
305
apply Rmult_le_compat_r.
306
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
307
assert (Hx2 : bpow (ex - 1 - fexp ex) = Z2R (Zpower (radix_val beta) (ex - 1 - fexp ex))).
308 309 310 311 312 313 314 315
apply sym_eq.
apply Z2R_Zpower.
omega.
rewrite Hx2.
apply Z2R_le.
apply Zfloor_lub.
rewrite <- Hx2.
unfold Zminus at 1.
316
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
317
apply Rmult_le_compat_r.
318
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
319
exact Hx1.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
320 321
Qed.

322 323 324 325 326 327
Theorem generic_format_canonic :
  forall f, canonic f ->
  generic_format (F2R f).
Proof.
intros (m, e) Hf.
unfold canonic in Hf. simpl in Hf.
328
unfold generic_format, scaled_mantissa.
329 330 331 332 333 334 335
rewrite <- Hf.
apply (f_equal (fun m => F2R (Float beta m e))).
unfold F2R. simpl.
rewrite Rmult_assoc, <- bpow_add, Zplus_opp_r, Rmult_1_r.
now rewrite Ztrunc_Z2R.
Qed.

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
Section Fcore_generic_rounding.

Variable Zrnd : R -> Z.
Hypothesis Zrnd_monotone : forall x y, (x <= y)%R -> (Zrnd x <= Zrnd y)%Z.
Hypothesis Zrnd_Z2R : forall n, Zrnd (Z2R n) = n.

Definition rounding x :=
  F2R (Float beta (Zrnd (scaled_mantissa x)) (canonic_exponent x)).

Theorem rounding_monotone :
  forall x y, (x <= y)%R -> (rounding x <= rounding y)%R.
Proof.
intros x y Hxy.
unfold rounding, scaled_mantissa, canonic_exponent.
destruct (total_order_T x 0) as [[Hx|Hx]|Hx].
(* x < 0 *)
destruct (Rlt_or_le y 0) as [Hy|Hy].
(* . y < 0 *)
destruct (ln_beta beta x) as (ex, Hex). simpl.
destruct (ln_beta beta y) as (ey, Hey). simpl.
specialize (Hex (Rlt_not_eq _ _ Hx)).
specialize (Hey (Rlt_not_eq _ _ Hy)).
rewrite Rabs_left in Hex. 2: exact Hx.
rewrite Rabs_left in Hey. 2: exact Hy.
assert (He: (ey <= ex)%Z).
cut (ey - 1 < ex)%Z. omega.
apply <- bpow_lt.
apply Rle_lt_trans with (1 := proj1 Hey).
apply Rle_lt_trans with (2 := proj2 Hex).
now apply Ropp_le_contravar.
destruct (Zle_or_lt ex (fexp ex)) as [Hx1|Hx1].
rewrite (proj2 (proj2 (prop_exp ex) Hx1) ey).
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
now apply Zle_trans with ex.
destruct (Zle_lt_or_eq _ _ He) as [He'|He'].
destruct (Zle_or_lt ex (fexp ey)) as [Hy2|Hy2].
rewrite (proj2 (proj2 (prop_exp ey) (Zle_trans _ _ _ He Hy2)) ex Hy2).
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
apply Rle_trans with (F2R (Float beta (Zrnd (- bpow (ex - 1) * bpow (- fexp ex))%R) (fexp ex))).
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
rewrite <- (Ropp_involutive x).
apply Ropp_le_contravar.
apply Hex.
rewrite Ropp_mult_distr_l_reverse.
rewrite <- bpow_add.
rewrite <- (Z2R_Zpower beta (ex - 1 + -fexp ex)). 2: omega.
rewrite <- opp_Z2R, Zrnd_Z2R.
destruct (Zle_or_lt ey (fexp ey)) as [Hy1|Hy1].
apply Rle_trans with (F2R (Float beta (-1) (fexp ey))).
unfold F2R. simpl.
rewrite opp_Z2R.
rewrite Z2R_Zpower. 2: omega.
rewrite 2!Ropp_mult_distr_l_reverse.
rewrite <- bpow_add, Rmult_1_l.
apply Ropp_le_contravar.
apply -> bpow_le.
omega.
apply F2R_le_compat.
rewrite <- (Zrnd_Z2R (-1)).
apply Zrnd_monotone.
apply Rlt_le.
apply Ropp_lt_cancel.
rewrite <- Ropp_mult_distr_l_reverse.
simpl. rewrite Ropp_involutive.
exact (proj2 (mantissa_small_pos _ _ Hey Hy1)).
apply Rle_trans with (F2R (Float beta (Zrnd (- bpow ey * bpow (- fexp ey))%R) (fexp ey))).
rewrite Ropp_mult_distr_l_reverse.
rewrite <- bpow_add.
rewrite <- Z2R_Zpower. 2: omega.
rewrite <- opp_Z2R.
rewrite Zrnd_Z2R.
unfold F2R. simpl.
rewrite 2!opp_Z2R.
rewrite 2!Z2R_Zpower ; try omega.
rewrite 2!Ropp_mult_distr_l_reverse.
rewrite <- 2!bpow_add.
apply Ropp_le_contravar.
apply -> bpow_le.
omega.
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
rewrite <- (Ropp_involutive y).
apply Ropp_lt_contravar.
apply Hey.
rewrite He'.
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
(* . 0 <= y *)
apply Rle_trans with R0.
apply F2R_le_0_compat. simpl.
rewrite <- (Zrnd_Z2R 0).
apply Zrnd_monotone.
rewrite <- (Rmult_0_l (bpow (- fexp (projT1 (ln_beta beta x))))).
apply Rmult_le_compat_r.
apply bpow_ge_0.
now apply Rlt_le.
apply F2R_ge_0_compat. simpl.
rewrite <- (Zrnd_Z2R Z0).
apply Zrnd_monotone.
apply Rmult_le_pos.
exact Hy.
apply bpow_ge_0.
(* x = 0 *)
rewrite Hx.
rewrite Rmult_0_l.
replace (Zrnd R0) with (Zrnd (Z2R Z0)) by reflexivity.
rewrite Zrnd_Z2R.
rewrite F2R_0.
apply F2R_ge_0_compat.
simpl.
rewrite <- (Zrnd_Z2R 0).
apply Zrnd_monotone.
apply Rmult_le_pos.
now rewrite <- Hx.
apply bpow_ge_0.
(* 0 < x *)
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.
apply <- bpow_lt.
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].
rewrite (proj2 (proj2 (prop_exp ey) Hy1) ex).
apply F2R_le_compat.
apply Zrnd_monotone.
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].
rewrite (proj2 (proj2 (prop_exp ex) (Zle_trans _ _ _ He Hx2)) ey Hx2).
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
apply Rle_trans with (F2R (Float beta (Zrnd (bpow (ey - 1) * bpow (- fexp ey))%R) (fexp ey))).
rewrite <- bpow_add.
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.
rewrite <- (Zrnd_Z2R 1).
apply Zrnd_monotone.
apply Rlt_le.
exact (proj2 (mantissa_small_pos _ _ Hex Hx1)).
unfold F2R. simpl.
rewrite Z2R_Zpower. 2: omega.
rewrite <- bpow_add, Rmult_1_l.
apply -> bpow_le.
omega.
apply Rle_trans with (F2R (Float beta (Zrnd (bpow ex * bpow (- fexp ex))%R) (fexp ex))).
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Rlt_le.
apply Hex.
rewrite <- bpow_add.
rewrite <- Z2R_Zpower. 2: omega.
rewrite Zrnd_Z2R.
unfold F2R. simpl.
rewrite 2!Z2R_Zpower ; try omega.
rewrite <- 2!bpow_add.
apply -> bpow_le.
omega.
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
apply Hey.
rewrite He'.
apply F2R_le_compat.
apply Zrnd_monotone.
apply Rmult_le_compat_r.
apply bpow_ge_0.
exact Hxy.
Qed.

Theorem rounding_generic :
  forall x,
  generic_format x ->
  rounding x = x.
Proof.
intros x Hx.
unfold rounding.
rewrite scaled_mantissa_generic with (1 := Hx).
rewrite Zrnd_Z2R.
now apply sym_eq.
Qed.

End Fcore_generic_rounding.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
557
Theorem generic_DN_pt_pos :
558
  forall x, (0 < x)%R ->
559
  Rnd_DN_pt generic_format x (F2R (Float beta (Zfloor (scaled_mantissa x)) (canonic_exponent x))).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
560
Proof.
561
intros x H0x.
562
unfold scaled_mantissa, canonic_exponent.
563 564 565 566
destruct (ln_beta beta x) as (ex, He).
simpl.
specialize (He (Rgt_not_eq _ _ H0x)).
rewrite (Rabs_pos_eq _ (Rlt_le _ _ H0x)) in He.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
567 568
destruct (Z_lt_le_dec (fexp ex) ex) as [He1|He1].
(* - positive big enough *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
569
assert (Hbl : (bpow (ex - 1) <= F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)))%R).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
570
now apply generic_DN_pt_large_pos_ge_pow_aux.
571
(* - . smaller *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
572
assert (Hrx : (F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)) <= x)%R).
573
unfold F2R. simpl.
574 575 576
apply Rmult_le_reg_r with (bpow (- fexp ex)).
apply bpow_gt_0.
rewrite Rmult_assoc, <- bpow_add, Zplus_opp_r, Rmult_1_r.
577
apply Zfloor_lb.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
578 579
split.
(* - . rounded *)
580
apply generic_format_canonic.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
581
apply sym_eq.
582
apply canonic_exponent_fexp_pos.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
583 584
split.
exact Hbl.
585
now apply Rle_lt_trans with (2 := proj2 He).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
586
split.
587
exact Hrx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
588
(* - . biggest *)
589
intros g Hg Hgx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
590 591 592
destruct (Rle_or_lt g R0) as [Hg3|Hg3].
apply Rle_trans with (2 := Hbl).
apply Rle_trans with (1 := Hg3).
593
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
594 595
apply Rnot_lt_le.
intros Hrg.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
596
assert (bpow (ex - 1) <= g < bpow ex)%R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
597 598 599 600
split.
apply Rle_trans with (1 := Hbl).
now apply Rlt_le.
now apply Rle_lt_trans with (1 := Hgx).
601 602
assert (Hcg: canonic_exponent g = fexp ex).
unfold canonic_exponent.
603
rewrite <- (Rabs_pos_eq g (Rlt_le _ _ Hg3)) in H.
604
now rewrite ln_beta_unique with (1 := H).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
605
apply Rlt_not_le with (1 := Hrg).
606 607
rewrite Hg.
rewrite Hcg.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
608
apply F2R_le_compat.
609
apply Zfloor_lub.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
610
apply Rmult_le_reg_r with (bpow (fexp ex)).
611
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
612
rewrite Rmult_assoc.
613
rewrite <- bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
614 615
rewrite Zplus_opp_l.
rewrite Rmult_1_r.
616 617
rewrite <- Hcg.
now rewrite Hg in Hgx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
618
(* - positive too small *)
619
rewrite mantissa_DN_small_pos with (1 := He) (2 := He1).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
620
rewrite F2R_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
621
split.
622
(* - . rounded *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
623
exact generic_format_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
624
split.
625
now apply Rlt_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
626
(* - . biggest *)
627
intros g Hg Hgx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
628 629
apply Rnot_lt_le.
intros Hg3.
630 631
destruct (ln_beta beta g) as (ge, Hg4).
simpl in Hg.
632
specialize (Hg4 (Rgt_not_eq _ _ Hg3)).
633 634 635
assert (Hcg: canonic_exponent g = fexp ge).
unfold canonic_exponent.
now rewrite ln_beta_unique with (1 := Hg4).
636
rewrite Rabs_pos_eq in Hg4.
637 638
apply (Rlt_not_le _ _ (Rle_lt_trans _ _ _ Hgx (proj2 He))).
apply Rle_trans with (bpow (fexp ge)).
639
apply -> bpow_le.
640
rewrite (proj2 (proj2 (prop_exp ex) He1) ge).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
641 642
exact He1.
apply Zle_trans with (2 := He1).
643
cut (ge - 1 < ex)%Z. omega.
644
apply <- bpow_lt.
645
apply Rle_lt_trans with (2 := proj2 He).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
646 647
apply Rle_trans with (2 := Hgx).
exact (proj1 Hg4).
648
rewrite Hg.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
649
rewrite <- F2R_bpow.
650
rewrite Hcg.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
651
apply F2R_le_compat.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
652
apply (Zlt_le_succ 0).
653 654 655
apply F2R_lt_reg with beta (fexp ge).
rewrite F2R_0, <- Hcg.
now rewrite Hg in Hg3.
656
now apply Rlt_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
657 658 659
Qed.

Theorem generic_DN_pt_neg :
660
  forall x, (x < 0)%R ->
661
  Rnd_DN_pt generic_format x (F2R (Float beta (Zfloor (scaled_mantissa x)) (canonic_exponent x))).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
662
Proof.
663
intros x Hx0.
664
unfold scaled_mantissa, canonic_exponent.
665 666 667 668
destruct (ln_beta beta x) as (ex, He).
simpl.
specialize (He (Rlt_not_eq _ _ Hx0)).
rewrite (Rabs_left _ Hx0) in He.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
669
assert (Hbr : (F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)) <= x)%R).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
670 671
(* - bounded right *)
unfold F2R. simpl.
672 673 674
apply Rmult_le_reg_r with (bpow (-fexp ex)).
apply bpow_gt_0.
rewrite Rmult_assoc, <- bpow_add, Zplus_opp_r, Rmult_1_r.
675
apply Zfloor_lb.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
676 677
destruct (Z_lt_le_dec (fexp ex) ex) as [He1|He1].
(* - negative big enough *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
678
assert (Hbl : (- bpow ex <= F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)))%R).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
679 680
(* - . bounded left *)
unfold F2R. simpl.
681 682 683 684 685 686
apply Rmult_le_reg_r with (bpow (-fexp ex)).
apply bpow_gt_0.
rewrite Rmult_assoc, <- bpow_add, Zplus_opp_r, Rmult_1_r.
assert (Hp : (- bpow ex * bpow (-fexp ex) = Z2R (- Zpower (radix_val beta) (ex - fexp ex)))%R).
rewrite Ropp_mult_distr_l_reverse.
rewrite <- bpow_add, <- Z2R_Zpower.
687
now rewrite opp_Z2R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
688
omega.
689 690 691
rewrite Hp.
apply Z2R_le.
apply Zfloor_lub.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
692
rewrite <- Hp.
693
apply Rmult_le_compat_r.
694
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
695 696
apply Ropp_le_cancel.
rewrite Ropp_involutive.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
697 698 699 700 701
now apply Rlt_le.
split.
(* - . rounded *)
destruct (Rle_lt_or_eq_dec _ _ Hbl) as [Hbl2|Hbl2].
(* - . . not a radix power *)
702 703
apply generic_format_canonic.
assert (Hb: (bpow (ex - 1) <= - F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)) < bpow ex)%R).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
704
split.
705 706
apply Rle_trans with (1 := proj1 He).
now apply Ropp_le_contravar.
707 708
apply Ropp_lt_cancel.
now rewrite Ropp_involutive.
709 710
apply sym_eq.
apply canonic_exponent_fexp_neg with (1 := Hb).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
711 712
(* - . . a radix power *)
rewrite <- Hbl2.
713 714 715
apply generic_format_opp.
apply generic_format_bpow.
exact (proj1 (prop_exp _) He1).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
716 717 718
split.
exact Hbr.
(* - . biggest *)
719
intros g Hg Hgx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
720 721 722 723
apply Rnot_lt_le.
intros Hg3.
assert (Hg4 : (g < 0)%R).
now apply Rle_lt_trans with (1 := Hgx).
724
destruct (ln_beta beta g) as (ge, Hge).
725
specialize (Hge (Rlt_not_eq _ _ Hg4)).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
726
apply Rlt_not_le with (1 := Hg3).
727 728
rewrite Hg.
assert (Hge' : ge = ex).
729
apply bpow_unique with (1 := Hge).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
730
split.
731
apply Rle_trans with (1 := proj1 He).
732
rewrite Rabs_left with (1 := Hg4).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
733 734
now apply Ropp_le_contravar.
apply Ropp_lt_cancel.
735 736 737
rewrite Rabs_left with (1 := Hg4).
rewrite Ropp_involutive.
now apply Rle_lt_trans with (1 := Hbl).
738 739 740 741 742
assert (Hcg: canonic_exponent g = fexp ex).
rewrite <- Hge'.
now apply canonic_exponent_fexp.
rewrite Hcg.
apply F2R_le_compat.
743
apply Zfloor_lub.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
744
apply Rmult_le_reg_r with (bpow (fexp ex)).
745
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
746
rewrite Rmult_assoc.
747 748 749
rewrite <- bpow_add, Zplus_opp_l, Rmult_1_r.
rewrite <- Hcg.
now rewrite Hg in Hgx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
750
(* - negative too small *)
751 752 753 754 755
rewrite <- (Zopp_involutive (Zfloor (x * bpow (- fexp ex)))).
rewrite <- (Ropp_involutive x) at 2.
rewrite Ropp_mult_distr_l_reverse.
change (- Zfloor (- (- x * bpow (- fexp ex))))%Z with (Zceil (- x * bpow (- fexp ex)))%Z.
rewrite mantissa_UP_small_pos ; try assumption.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
756 757 758 759 760
unfold F2R. simpl.
rewrite Ropp_mult_distr_l_reverse.
rewrite Rmult_1_l.
(* - . rounded *)
split.
761 762 763
apply generic_format_opp.
apply generic_format_bpow.
exact (proj1 (proj2 (prop_exp _) He1)).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
764 765
split.
(* - . smaller *)
766 767
apply Ropp_le_cancel.
rewrite Ropp_involutive.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
768
apply Rlt_le.
769
apply Rlt_le_trans with (1 := proj2 He).
770
now apply -> bpow_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
771
(* - . biggest *)
772
intros g Hg Hgx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
773 774 775 776
apply Rnot_lt_le.
intros Hg3.
assert (Hg4 : (g < 0)%R).
now apply Rle_lt_trans with (1 := Hgx).
777 778
destruct (ln_beta beta g) as (ge, Hge).
simpl in Hg.
779
specialize (Hge (Rlt_not_eq g 0 Hg4)).
780
rewrite (Rabs_left _ Hg4) in Hge.
781 782
assert (Hge' : (ge <= fexp ex)%Z).
cut (ge - 1 < fexp ex)%Z. omega.
783
apply <- bpow_lt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
784 785 786
apply Rle_lt_trans with (1 := proj1 Hge).
apply Ropp_lt_cancel.
now rewrite Ropp_involutive.
787 788 789 790 791
assert (Hcg: canonic_exponent g = fexp ex).
unfold canonic_exponent.
rewrite <- Rabs_left with (1 := Hg4) in Hge.
rewrite ln_beta_unique with (1 := Hge).
exact (proj2 (proj2 (prop_exp _) He1) _ Hge').
Guillaume Melquiond's avatar
Guillaume Melquiond committed
792
apply Rlt_not_le with (1 := proj2 Hge).
793
rewrite Hg.
794
unfold scaled_mantissa, F2R. simpl.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
795
rewrite <- Ropp_mult_distr_l_reverse.
796 797
rewrite Hcg.
pattern (fexp ex) at 2 ; replace (fexp ex) with (fexp ex - ge + ge)%Z by ring.
798
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
799
rewrite <- Rmult_assoc.
800
pattern (bpow ge) at 1 ; rewrite <- Rmult_1_l.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
801
apply Rmult_le_compat_r.
802
apply bpow_ge_0.
803 804 805 806 807 808
assert (- Z2R (Ztrunc (g * bpow (- fexp ex))) * bpow (fexp ex - ge) = Z2R (- Ztrunc (g * bpow (-fexp ex)) * Zpower (radix_val beta) (fexp ex - ge)))%R.
rewrite mult_Z2R.
rewrite Z2R_Zpower. 2: omega.
now rewrite opp_Z2R.
rewrite H.
apply (Z2R_le 1).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
809 810
apply (Zlt_le_succ 0).
apply lt_Z2R.
811 812 813 814
rewrite <- H.
unfold Zminus.
rewrite bpow_add, <- Rmult_assoc.
apply Rmult_lt_0_compat.
815
rewrite <- Ropp_0.
816
rewrite Ropp_mult_distr_l_reverse.
817 818 819
apply Ropp_lt_contravar.
rewrite <- Hcg.
now rewrite Hg in Hg4.
820
apply bpow_gt_0.
821 822 823 824 825
Qed.

Theorem generic_format_satisfies_any :
  satisfies_any generic_format.
Proof.
826
split.
827
(* symmetric set *)
828
exact generic_format_0.
829
exact generic_format_opp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
830
(* rounding down *)
831 832
intros x.
exists (match Req_EM_T x 0 with
833
  | left Hx => R0
834
  | right Hx => F2R (Float beta (Zfloor (x * bpow (- canonic_exponent x))) (canonic_exponent x))
Guillaume Melquiond's avatar
Guillaume Melquiond committed
835
  end).
836 837
destruct (Req_EM_T x 0) as [Hx|Hx].
(* . *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
838
split.
839 840
apply generic_format_0.
rewrite Hx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
841 842
split.
apply Rle_refl.
843 844 845
now intros g _ H.
(* . *)
destruct (ln_beta beta x) as (ex, H1).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
846
simpl.
847 848
specialize (H1 Hx).
destruct (Rdichotomy _ _ Hx) as [H2|H2].
849 850 851 852 853 854 855 856 857 858 859 860 861
now apply generic_DN_pt_neg.
now apply generic_DN_pt_pos.
Qed.

Theorem generic_DN_pt :
  forall x,
  Rnd_DN_pt generic_format x (F2R (Float beta (Zfloor (x * bpow (- canonic_exponent x))) (canonic_exponent x))).
Proof.
intros x.
destruct (total_order_T 0 x) as [[Hx|Hx]|Hx].
now apply generic_DN_pt_pos.
rewrite <- Hx, Rmult_0_l.
fold (Z2R 0).
862
rewrite Zfloor_Z2R, F2R_0.
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
apply Rnd_DN_pt_refl.
apply generic_format_0.
now apply generic_DN_pt_neg.
Qed.

Theorem generic_UP_pt :
  forall x,
  Rnd_UP_pt generic_format x (F2R (Float beta (Zceil (x * bpow (- canonic_exponent x))) (canonic_exponent x))).
Proof.
intros x.
apply Rnd_DN_UP_pt_sym.
apply generic_format_satisfies_any.
unfold Zceil.
rewrite <- Ropp_mult_distr_l_reverse.
rewrite opp_F2R, Zopp_involutive.
rewrite <- canonic_exponent_opp.
apply generic_DN_pt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
880 881 882
Qed.

Theorem generic_DN_pt_small_pos :
883
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
884
  (bpow (ex - 1) <= x < bpow ex)%R ->
885 886 887 888
  (ex <= fexp ex)%Z ->
  Rnd_DN_pt generic_format x R0.
Proof.
intros x ex Hx He.
889 890 891 892
rewrite <- (F2R_0 beta (canonic_exponent x)).
rewrite <- mantissa_DN_small_pos with (1 := Hx) (2 := He).
rewrite <- canonic_exponent_fexp_pos with (1 := Hx).
apply generic_DN_pt.
893 894
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
895
Theorem generic_UP_pt_small_pos :
896
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
897
  (bpow (ex - 1) <= x < bpow ex)%R ->
898 899 900 901
  (ex <= fexp ex)%Z ->
  Rnd_UP_pt generic_format x (bpow (fexp ex)).
Proof.
intros x ex Hx He.
902 903 904 905
rewrite <- F2R_bpow.
rewrite <- mantissa_UP_small_pos with (1 := Hx) (2 := He).
rewrite <- canonic_exponent_fexp_pos with (1 := Hx).
apply generic_UP_pt.
906 907
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
908
Theorem generic_UP_pt_large_pos_le_pow :
909
  forall x xu ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
910
  (bpow (ex - 1) <= x < bpow ex)%R ->
911 912 913 914
  (fexp ex < ex)%Z ->
  Rnd_UP_pt generic_format x xu ->
  (xu <= bpow ex)%R.
Proof.
915 916 917 918
intros x xu ex Hx He (_, (_, Hu4)).
apply Hu4 with (2 := Rlt_le _ _ (proj2 Hx)).
apply generic_format_bpow.
exact (proj1 (prop_exp _) He).
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
Qed.

Theorem generic_format_EM :
  forall x,
  generic_format x \/ ~generic_format x.
Proof.
intros x.
destruct (proj1 (satisfies_any_imp_DN _ generic_format_satisfies_any) x) as (d, Hd).
destruct (Rle_lt_or_eq_dec d x) as [Hxd|Hxd].
apply Hd.
right.
intros Fx.
apply Rlt_not_le with (1 := Hxd).
apply Req_le.
apply sym_eq.
now apply Rnd_DN_pt_idempotent with (1 := Hd).
left.
rewrite <- Hxd.
apply Hd.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
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
Theorem generic_DN_pt_large_pos_ge_pow :
  forall x d e,
  (0 < d)%R ->
  Rnd_DN_pt generic_format x d ->
  (bpow e <= x)%R ->
  (bpow e <= d)%R.
Proof.
intros x d e Hd Hxd Hex.
destruct (ln_beta beta x) as (ex, He).
assert (Hx: (0 < x)%R).
apply Rlt_le_trans with (1 := Hd).
apply Hxd.
specialize (He (Rgt_not_eq _ _ Hx)).
rewrite Rabs_pos_eq in He. 2: now apply Rlt_le.
apply Rle_trans with (bpow (ex - 1)).
apply -> bpow_le.
cut (e < ex)%Z. omega.
apply <- bpow_lt.
now apply Rle_lt_trans with (2 := proj2 He).
apply Hxd with (2 := proj1 He).
apply generic_format_bpow.
destruct (Zle_or_lt ex (fexp ex)).
elim Rgt_not_eq with (1 := Hd).
apply Rnd_DN_pt_unicity with (1 := Hxd).
now apply generic_DN_pt_small_pos with (1 := He).
ring_simplify (ex - 1 + 1)%Z.
omega.
Qed.

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
Theorem canonic_exponent_DN_pt :
  forall x d : R,
  (0 < d)%R ->
  Rnd_DN_pt generic_format x d ->
  canonic_exponent d = canonic_exponent x.
Proof.
intros x d Hd Hxd.
unfold canonic_exponent.
apply f_equal.
apply ln_beta_unique.
rewrite (Rabs_pos_eq d). 2: now apply Rlt_le.
destruct (ln_beta beta x) as (ex, He).
simpl.
assert (Hx: (0 < x)%R).
apply Rlt_le_trans with (1 := Hd).
apply Hxd.
specialize (He (Rgt_not_eq _ _ Hx)).
rewrite Rabs_pos_eq in He. 2: now apply Rlt_le.
split.
now apply generic_DN_pt_large_pos_ge_pow with (2 := Hxd).
apply Rle_lt_trans with (2 := proj2 He).
apply Hxd.
Qed.

993
End RND_generic.