Flocq_rnd_generic.v 16.5 KB
Newer Older
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1 2 3
Require Import Flocq_Raux.
Require Import Flocq_defs.
Require Import Flocq_rnd_ex.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
4
Require Import Flocq_rnd_prop.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
5
Require Import Flocq_float_prop.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
6 7 8 9 10

Section RND_generic.

Variable beta : radix.

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

Variable fexp : Z -> Z.

15 16 17 18 19 20 21 22
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
23

24
Definition canonic x (f : float beta) :=
25
  x = F2R f /\ Fexp f = fexp (projT1 (ln_beta beta x)).
26

Guillaume Melquiond's avatar
Guillaume Melquiond committed
27 28
Definition generic_format (x : R) :=
  exists f : float beta,
29
  canonic x f.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
30

Guillaume Melquiond's avatar
Guillaume Melquiond committed
31 32 33 34 35 36 37
Theorem generic_format_0 :
  generic_format 0.
Proof.
exists (Float beta 0 _) ; repeat split.
now rewrite F2R_0.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
38 39 40
Theorem generic_DN_pt_large_pos_ge_pow :
  forall x ex,
  (fexp ex < ex)%Z ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
41 42
  (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
43
Proof.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
44
intros x ex He1 Hx1.
45
unfold F2R. simpl.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
46
replace (ex - 1)%Z with ((ex - 1 - fexp ex) + fexp ex)%Z by ring.
47
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
48
apply Rmult_le_compat_r.
49
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
50
assert (Hx2 : bpow (ex - 1 - fexp ex) = Z2R (Zpower (radix_val beta) (ex - 1 - fexp ex))).
51 52 53 54 55 56 57 58
apply sym_eq.
apply Z2R_Zpower.
omega.
rewrite Hx2.
apply Z2R_le.
apply Zfloor_lub.
rewrite <- Hx2.
unfold Zminus at 1.
59
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
60
apply Rmult_le_compat_r.
61
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
62
exact Hx1.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
63 64 65 66
Qed.

Theorem generic_DN_pt_pos :
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
67 68
  (bpow (ex - 1) <= x < bpow ex)%R ->
  Rnd_DN_pt generic_format x (F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex))).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
69 70 71 72
Proof.
intros x ex (Hx1, Hx2).
destruct (Z_lt_le_dec (fexp ex) ex) as [He1|He1].
(* - positive big enough *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
73
assert (Hbl : (bpow (ex - 1) <= F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)))%R).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
74
now apply generic_DN_pt_large_pos_ge_pow.
75
(* - . smaller *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
76
assert (Hrx : (F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)) <= x)%R).
77
unfold F2R. simpl.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
78
pattern x at 2 ; replace x with ((x * bpow (- fexp ex)) * bpow (fexp ex))%R.
79
apply Rmult_le_compat_r.
80
apply bpow_ge_0.
81 82
apply Zfloor_lb.
rewrite Rmult_assoc.
83
rewrite <- bpow_add.
84 85
rewrite Zplus_opp_l.
apply Rmult_1_r.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
86 87
split.
(* - . rounded *)
88
eexists ; repeat split.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
89 90 91 92
simpl.
apply f_equal.
apply sym_eq.
apply ln_beta_unique.
93
rewrite Rabs_pos_eq.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
94 95
split.
exact Hbl.
96
now apply Rle_lt_trans with (2 := Hx2).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
97
apply Rle_trans with (2 := Hbl).
98
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
99
split.
100
exact Hrx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
101 102 103 104 105
(* - . biggest *)
intros g ((gm, ge), (Hg1, Hg2)) Hgx.
destruct (Rle_or_lt g R0) as [Hg3|Hg3].
apply Rle_trans with (2 := Hbl).
apply Rle_trans with (1 := Hg3).
106
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
107 108
apply Rnot_lt_le.
intros Hrg.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
109
assert (bpow (ex - 1) <= g < bpow ex)%R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
110 111 112 113
split.
apply Rle_trans with (1 := Hbl).
now apply Rlt_le.
now apply Rle_lt_trans with (1 := Hgx).
114
rewrite <- (Rabs_pos_eq g (Rlt_le _ _ Hg3)) in H.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
115 116 117 118
rewrite ln_beta_unique with (1 := H) in Hg2.
simpl in Hg2.
apply Rlt_not_le with (1 := Hrg).
rewrite Hg1, Hg2.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
119
apply F2R_le_compat.
120
apply Zfloor_lub.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
121
apply Rmult_le_reg_r with (bpow (fexp ex)).
122
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
123
rewrite Rmult_assoc.
124
rewrite <- bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
125 126 127 128
rewrite Zplus_opp_l.
rewrite Rmult_1_r.
unfold F2R in Hg1.
simpl in Hg1.
129
rewrite <- Hg2.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
130 131
now rewrite <- Hg1.
(* - positive too small *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
132
replace (Zfloor (x * bpow (- fexp ex))) with Z0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
133
(* - . rounded *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
134
rewrite F2R_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
135
split.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
136
exact generic_format_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
137
split.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
138
apply Rle_trans with (2 := Hx1).
139
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
140 141 142 143
(* - . biggest *)
intros g ((gm, ge), (Hg1, Hg2)) Hgx.
apply Rnot_lt_le.
intros Hg3.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
144
destruct (ln_beta beta g) as (ge', Hg4).
145 146 147
simpl in Hg2.
specialize (Hg4 (Rgt_not_eq _ _ Hg3)).
rewrite Rabs_pos_eq in Hg4.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
148 149
apply (Rlt_not_le _ _ (Rle_lt_trans _ _ _ Hgx Hx2)).
apply Rle_trans with (bpow ge).
150
apply -> bpow_le.
151
simpl in Hg2.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
152
rewrite Hg2.
153
rewrite (proj2 (proj2 (prop_exp ex) He1) ge').
Guillaume Melquiond's avatar
Guillaume Melquiond committed
154 155
exact He1.
apply Zle_trans with (2 := He1).
156
cut (ge' - 1 < ex)%Z. omega.
157
apply <- bpow_lt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
158 159 160 161
apply Rle_lt_trans with (2 := Hx2).
apply Rle_trans with (2 := Hgx).
exact (proj1 Hg4).
rewrite Hg1.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
162 163
rewrite <- F2R_bpow.
apply F2R_le_compat.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
164
apply (Zlt_le_succ 0).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
165 166
apply F2R_lt_reg with beta ge.
now rewrite F2R_0, <- Hg1.
167
now apply Rlt_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
168 169
(* - . . *)
apply sym_eq.
170 171 172 173 174
apply Zfloor_imp.
simpl.
split.
apply Rmult_le_pos.
apply Rle_trans with (2 := Hx1).
175 176
apply bpow_ge_0.
apply bpow_ge_0.
177
apply Rmult_lt_reg_r with (bpow (fexp ex)).
178
apply bpow_gt_0.
179
rewrite Rmult_assoc.
180
rewrite <- bpow_add.
181 182
rewrite Zplus_opp_l.
rewrite Rmult_1_r, Rmult_1_l.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
183
apply Rlt_le_trans with (1 := Hx2).
184
now apply -> bpow_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
185 186 187 188
Qed.

Theorem generic_DN_pt_neg :
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
189 190
  (bpow (ex - 1) <= -x < bpow ex)%R ->
  Rnd_DN_pt generic_format x (F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex))).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
191 192 193 194 195 196
Proof.
intros x ex (Hx1, Hx2).
assert (Hx : (x < 0)%R).
apply Ropp_lt_cancel.
rewrite Ropp_0.
apply Rlt_le_trans with (2 := Hx1).
197
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
198
assert (Hbr : (F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)) <= x)%R).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
199 200 201 202 203
(* - bounded right *)
unfold F2R. simpl.
pattern x at 2 ; rewrite <- Rmult_1_r.
change R1 with (bpow Z0).
rewrite <- (Zplus_opp_l (fexp ex)).
204
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
205 206
rewrite <- Rmult_assoc.
apply Rmult_le_compat_r.
207
apply bpow_ge_0.
208
apply Zfloor_lb.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
209 210
destruct (Z_lt_le_dec (fexp ex) ex) as [He1|He1].
(* - negative big enough *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
211
assert (Hbl : (- bpow ex <= F2R (Float beta (Zfloor (x * bpow (- fexp ex))) (fexp ex)))%R).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
212 213 214
(* - . bounded left *)
unfold F2R. simpl.
pattern ex at 1 ; replace ex with (ex - fexp ex + fexp ex)%Z by ring.
215
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
216 217
rewrite <- Ropp_mult_distr_l_reverse.
apply Rmult_le_compat_r.
218
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
219
assert (Hp : (- bpow (ex - fexp ex) = Z2R (- Zpower (radix_val beta) (ex - fexp ex)))%R).
220 221
rewrite <- Z2R_Zpower.
now rewrite opp_Z2R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
222
omega.
223 224 225
rewrite Hp.
apply Z2R_le.
apply Zfloor_lub.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
226
rewrite <- Hp.
227
unfold Zminus.
228
rewrite bpow_add.
229 230
rewrite <- Ropp_mult_distr_l_reverse.
apply Rmult_le_compat_r.
231
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
232 233
apply Ropp_le_cancel.
rewrite Ropp_involutive.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
234 235 236 237 238
now apply Rlt_le.
split.
(* - . rounded *)
destruct (Rle_lt_or_eq_dec _ _ Hbl) as [Hbl2|Hbl2].
(* - . . not a radix power *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
239
eexists ; repeat split.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
240 241 242 243
simpl.
apply f_equal.
apply sym_eq.
apply ln_beta_unique.
244
rewrite Rabs_left.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
245
split.
246 247
apply Ropp_le_cancel.
rewrite Ropp_involutive.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
248
apply Rle_trans with (1 := Hbr).
249 250 251 252
apply Ropp_le_cancel.
now rewrite Ropp_involutive.
apply Ropp_lt_cancel.
now rewrite Ropp_involutive.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
253 254 255 256
apply Rle_lt_trans with (1 := Hbr).
exact Hx.
(* - . . a radix power *)
rewrite <- Hbl2.
257
generalize (proj1 (prop_exp _) He1).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
258 259 260
clear.
intros He2.
exists (Float beta (- Zpower (radix_val beta) (ex - fexp (ex + 1))) (fexp (ex + 1))).
261
unfold canonic, F2R. simpl.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
262 263 264
split.
clear -He2.
pattern ex at 1 ; replace ex with (ex - fexp (ex + 1) + fexp (ex + 1))%Z by ring.
265
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
266 267 268
rewrite <- Ropp_mult_distr_l_reverse.
rewrite opp_Z2R.
apply (f_equal (fun x => (- x * _)%R)).
269 270 271
apply sym_eq.
apply Z2R_Zpower.
omega.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
272 273 274
apply f_equal.
apply sym_eq.
apply ln_beta_unique.
275 276
rewrite Rabs_Ropp.
rewrite Rabs_right.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
277
split.
278
apply -> bpow_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
279
omega.
280
apply -> bpow_lt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
281 282
apply Zlt_succ.
apply Rle_ge.
283
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
284 285 286 287 288 289 290 291
split.
exact Hbr.
(* - . biggest *)
intros g ((gm, ge), (Hg1, Hg2)) Hgx.
apply Rnot_lt_le.
intros Hg3.
assert (Hg4 : (g < 0)%R).
now apply Rle_lt_trans with (1 := Hgx).
292
destruct (ln_beta beta g) as (ge', Hge).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
293
simpl in Hg2.
294
specialize (Hge (Rlt_not_eq _ _ Hg4)).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
295 296 297 298 299
apply Rlt_not_le with (1 := Hg3).
rewrite Hg1.
unfold F2R. simpl.
rewrite Hg2.
assert (Hge' : ge' = ex).
300
apply bpow_unique with (1 := Hge).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
301 302
split.
apply Rle_trans with (1 := Hx1).
303
rewrite Rabs_left with (1 := Hg4).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
304 305
now apply Ropp_le_contravar.
apply Ropp_lt_cancel.
306 307 308
rewrite Rabs_left with (1 := Hg4).
rewrite Ropp_involutive.
now apply Rle_lt_trans with (1 := Hbl).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
309 310
rewrite Hge'.
apply Rmult_le_compat_r.
311
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
312
apply Z2R_le.
313
apply Zfloor_lub.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
314
apply Rmult_le_reg_r with (bpow (fexp ex)).
315
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
316
rewrite Rmult_assoc.
317
rewrite <- bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
318 319 320 321 322 323 324
rewrite Zplus_opp_l.
rewrite Rmult_1_r.
rewrite <- Hge'.
rewrite <- Hg2.
unfold F2R in Hg1. simpl in Hg1.
now rewrite <- Hg1.
(* - negative too small *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
325
replace (Zfloor (x * bpow (- fexp ex))) with (-1)%Z.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
326 327 328 329 330
unfold F2R. simpl.
rewrite Ropp_mult_distr_l_reverse.
rewrite Rmult_1_l.
(* - . rounded *)
split.
331
destruct (proj2 (prop_exp _) He1) as (He2, _).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
332
exists (Float beta (- Zpower (radix_val beta) (fexp ex - fexp (fexp ex + 1))) (fexp (fexp ex + 1))).
333
unfold canonic, F2R. simpl.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
334 335 336
split.
rewrite opp_Z2R.
pattern (fexp ex) at 1 ; replace (fexp ex) with (fexp ex - fexp (fexp ex + 1) + fexp (fexp ex + 1))%Z by ring.
337
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
338 339
rewrite Ropp_mult_distr_l_reverse.
apply (f_equal (fun x => (- (x * _))%R)).
340 341 342
apply sym_eq.
apply Z2R_Zpower.
omega.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
343 344 345
apply f_equal.
apply sym_eq.
apply ln_beta_unique.
346 347
rewrite Rabs_Ropp.
rewrite Rabs_pos_eq.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
348 349 350
split.
replace (fexp ex + 1 - 1)%Z with (fexp ex) by ring.
apply Rle_refl.
351
apply -> bpow_lt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
352
apply Zlt_succ.
353
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
354 355
split.
(* - . smaller *)
356 357
apply Ropp_le_cancel.
rewrite Ropp_involutive.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
358 359
apply Rlt_le.
apply Rlt_le_trans with (1 := Hx2).
360
now apply -> bpow_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
361 362 363 364 365 366
(* - . biggest *)
intros g ((gm, ge), (Hg1, Hg2)) Hgx.
apply Rnot_lt_le.
intros Hg3.
assert (Hg4 : (g < 0)%R).
now apply Rle_lt_trans with (1 := Hgx).
367
destruct (ln_beta beta g) as (ge', Hge).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
368
simpl in Hg2.
369
specialize (Hge (Rlt_not_eq g 0 Hg4)).
370
rewrite (Rabs_left _ Hg4) in Hge.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
371 372
assert (Hge' : (ge' <= fexp ex)%Z).
cut (ge' - 1 < fexp ex)%Z. omega.
373
apply <- bpow_lt.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
374 375 376
apply Rle_lt_trans with (1 := proj1 Hge).
apply Ropp_lt_cancel.
now rewrite Ropp_involutive.
377
rewrite (proj2 (proj2 (prop_exp _) He1) _ Hge') in Hg2.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
378 379 380 381 382 383
rewrite <- Hg2 in Hge'.
apply Rlt_not_le with (1 := proj2 Hge).
rewrite Hg1.
unfold F2R. simpl.
rewrite <- Ropp_mult_distr_l_reverse.
replace ge with (ge - ge' + ge')%Z by ring.
384
rewrite bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
385 386 387
rewrite <- Rmult_assoc.
pattern (bpow ge') at 1 ; rewrite <- Rmult_1_l.
apply Rmult_le_compat_r.
388
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
389 390 391 392 393
rewrite <- opp_Z2R.
assert (1 <= -gm)%Z.
apply (Zlt_le_succ 0).
apply lt_Z2R.
apply Rmult_lt_reg_r with (bpow ge).
394
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
395
rewrite Rmult_0_l.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
396 397
change (0 < F2R (Float beta (-gm) ge))%R.
rewrite <- opp_F2R.
398 399 400
rewrite <- Hg1.
rewrite <- Ropp_0.
now apply Ropp_lt_contravar.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
401
apply Rle_trans with (1 * bpow (ge - ge'))%R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
402
rewrite Rmult_1_l.
403
apply -> (bpow_le beta 0).
404
omega.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
405
apply Rmult_le_compat_r.
406
apply bpow_ge_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
407 408 409
now apply (Z2R_le 1).
(* - . . *)
apply sym_eq.
410
apply Zfloor_imp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
411
simpl.
412
split.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
413
apply Rle_trans with (- bpow ex * bpow (- fexp ex))%R.
414
rewrite Ropp_mult_distr_l_reverse.
415
rewrite <- bpow_add.
416
apply Ropp_le_contravar.
417
apply (proj1 (bpow_le beta _ 0)).
418
omega.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
419
apply Rmult_le_compat_r.
420
apply bpow_ge_0.
421 422 423
apply Ropp_le_cancel.
rewrite Ropp_involutive.
now apply Rlt_le.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
424
rewrite <- (Rmult_0_l (bpow (- fexp ex))).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
425
apply Rmult_lt_compat_r.
426
apply bpow_gt_0.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
427 428 429
exact Hx.
Qed.

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
Theorem canonic_unicity :
  forall x f1 f2,
  canonic x f1 ->
  canonic x f2 ->
  f1 = f2.
Proof.
intros x (m1,e1) (m2,e2) (H1a,H1b) (H2a,H2b).
simpl in H1b, H2b.
rewrite H1b, <- H2b.
apply (f_equal (fun m => Float beta m e2)).
apply eq_Z2R.
apply Rmult_eq_reg_r with (bpow e1).
change (F2R (Float beta m1 e1) = F2R (Float beta m2 e1)).
now rewrite <- H1a, H1b, <- H2b.
apply Rgt_not_eq.
445
apply bpow_gt_0.
446 447
Qed.

448 449 450 451 452
Theorem canonic_sym :
  forall x m e,
  canonic x (Float beta m e) ->
  canonic (-x) (Float beta (-m) e).
Proof.
453 454 455 456 457 458 459 460 461 462
intros x m e.
destruct (Req_dec x 0) as [Hx|Hx].
(* . *)
rewrite Hx, Ropp_0.
intros (H1,H2).
split.
now rewrite <- opp_F2R, <- H1, Ropp_0.
exact H2.
(* . *)
intros (H1,H2).
463 464 465
split.
rewrite H1.
apply opp_F2R.
466
now rewrite ln_beta_opp.
467 468
Qed.

469 470
Theorem generic_format_sym :
  forall x, generic_format x -> generic_format (-x).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
471
Proof.
472 473 474
intros x ((m,e),H).
exists (Float beta (-m) e).
now apply canonic_sym.
475 476 477 478 479
Qed.

Theorem generic_format_satisfies_any :
  satisfies_any generic_format.
Proof.
480
split.
481
(* symmetric set *)
482
exact generic_format_0.
483
exact generic_format_sym.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
484
(* rounding down *)
485 486
intros x.
exists (match Req_EM_T x 0 with
487 488
  | left Hx => R0
  | right Hx =>
Guillaume Melquiond's avatar
Guillaume Melquiond committed
489
    let e := fexp (projT1 (ln_beta beta x)) in
490
    F2R (Float beta (Zfloor (x * bpow (Zopp e))) e)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
491
  end).
492 493
destruct (Req_EM_T x 0) as [Hx|Hx].
(* . *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
494
split.
495 496
apply generic_format_0.
rewrite Hx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
497 498
split.
apply Rle_refl.
499 500 501
now intros g _ H.
(* . *)
destruct (ln_beta beta x) as (ex, H1).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
502
simpl.
503 504
specialize (H1 Hx).
destruct (Rdichotomy _ _ Hx) as [H2|H2].
Guillaume Melquiond's avatar
Guillaume Melquiond committed
505
apply generic_DN_pt_neg.
506 507 508 509 510
now rewrite <- Rabs_left.
apply generic_DN_pt_pos.
rewrite Rabs_right in H1.
exact H1.
now apply Rgt_ge.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
511 512 513
Qed.

Theorem generic_DN_pt_small_pos :
514
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
515
  (bpow (ex - 1) <= x < bpow ex)%R ->
516 517 518 519 520
  (ex <= fexp ex)%Z ->
  Rnd_DN_pt generic_format x R0.
Proof.
intros x ex Hx He.
split.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
521
exact generic_format_0.
522 523
split.
apply Rle_trans with (2 := proj1 Hx).
524
apply bpow_ge_0.
525 526 527 528
(* . *)
intros g ((gm, ge), (Hg1, Hg2)) Hgx.
apply Rnot_lt_le.
intros Hg3.
529
destruct (ln_beta beta g) as (eg, Hg4).
530
simpl in Hg2.
531
specialize (Hg4 (Rgt_not_eq g 0 Hg3)).
532 533 534 535 536 537 538
rewrite Rabs_right in Hg4.
apply Rle_not_lt with (1 := Hgx).
rewrite Hg1.
apply Rlt_le_trans with (1 := proj2 Hx).
rewrite (proj2 (proj2 (prop_exp _) He) eg) in Hg2.
rewrite Hg2.
apply Rle_trans with (bpow (fexp ex)).
539
now apply -> bpow_le.
540 541
rewrite <- Hg2.
rewrite Hg1 in Hg3.
542
apply bpow_le_F2R.
543
apply F2R_gt_0_reg with (1 := Hg3).
544
apply bpow_lt_bpow with beta.
545 546 547
apply Rlt_le_trans with (bpow ex).
apply Rle_lt_trans with (2 := proj2 Hx).
now apply Rle_trans with g.
548
now apply -> bpow_le.
549 550 551 552
apply Rle_ge.
now apply Rlt_le.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
553
Theorem generic_UP_pt_small_pos :
554
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
555
  (bpow (ex - 1) <= x < bpow ex)%R ->
556 557 558 559
  (ex <= fexp ex)%Z ->
  Rnd_UP_pt generic_format x (bpow (fexp ex)).
Proof.
intros x ex Hx He.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
560
assert (bpow (fexp ex) = F2R (Float beta (Zpower (radix_val beta) (fexp ex - fexp (fexp ex + 1))) (fexp (fexp ex + 1)))).
561
unfold F2R. simpl.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
562
rewrite Z2R_Zpower.
563
rewrite <- bpow_add.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
564 565
apply f_equal.
ring.
566 567 568 569 570
generalize (proj1 (proj2 (prop_exp ex) He)).
omega.
split.
(* . *)
rewrite H.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
571
eexists ; repeat split.
572 573 574 575
simpl.
apply f_equal.
apply sym_eq.
rewrite <- H.
576
apply ln_beta_unique.
577 578 579
split.
replace (fexp ex + 1 - 1)%Z with (fexp ex) by ring.
apply RRle_abs.
580
rewrite Rabs_pos_eq.
581
apply -> bpow_lt.
582 583
apply Zle_lt_succ.
apply Zle_refl.
584
apply bpow_ge_0.
585 586 587 588
split.
(* . *)
apply Rlt_le.
apply Rlt_le_trans with (1 := proj2 Hx).
589
now apply -> bpow_le.
590 591 592 593 594 595
(* . *)
intros g ((gm, ge), (Hg1, Hg2)) Hgx.
assert (g <> R0).
apply Rgt_not_eq.
apply Rlt_le_trans with (2 := Hgx).
apply Rlt_le_trans with (2 := proj1 Hx).
596
apply bpow_gt_0.
597
destruct (ln_beta beta g) as (eg, Hg3).
598
simpl in Hg2.
599
specialize (Hg3 H0).
600 601 602 603 604 605
apply Rnot_lt_le.
intros Hgp.
apply Rlt_not_le with (1 := Hgp).
rewrite <- (proj2 (proj2 (prop_exp ex) He) eg).
rewrite <- Hg2.
rewrite Hg1.
606
apply bpow_le_F2R.
607
apply F2R_gt_0_reg with beta ge.
608 609 610
rewrite <- Hg1.
apply Rlt_le_trans with (2 := Hgx).
apply Rlt_le_trans with (2 := proj1 Hx).
611 612
apply bpow_gt_0.
apply bpow_lt_bpow with beta.
613 614 615 616 617 618
apply Rle_lt_trans with g.
rewrite <- (Rabs_right g).
apply Hg3.
apply Rle_ge.
apply Rle_trans with (2 := Hgx).
apply Rle_trans with (2 := proj1 Hx).
619
apply bpow_ge_0.
620 621 622
exact Hgp.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
623
Theorem generic_UP_pt_large_pos_le_pow :
624
  forall x xu ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
625
  (bpow (ex - 1) <= x < bpow ex)%R ->
626 627 628 629 630 631 632
  (fexp ex < ex)%Z ->
  Rnd_UP_pt generic_format x xu ->
  (xu <= bpow ex)%R.
Proof.
intros x xu ex Hx He (((dm, de), (Hu1, Hu2)), (Hu3, Hu4)).
apply Hu4 with (2 := (Rlt_le _ _ (proj2 Hx))).
exists (Float beta (Zpower (radix_val beta) (ex - fexp (ex + 1))) (fexp (ex + 1))).
633
unfold canonic, F2R. simpl.
634 635 636
split.
(* . *)
rewrite Z2R_Zpower.
637
rewrite <- bpow_add.
638 639 640 641 642 643 644 645 646 647 648 649
apply f_equal.
ring.
generalize (proj1 (prop_exp _) He).
omega.
(* . *)
apply f_equal.
apply sym_eq.
apply ln_beta_unique.
rewrite Rabs_pos_eq.
split.
ring_simplify (ex + 1 - 1)%Z.
apply Rle_refl.
650
apply -> bpow_lt.
651
apply Zlt_succ.
652
apply bpow_ge_0.
653 654
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
655 656
Theorem generic_UP_pt_pos :
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
657
  (bpow (ex - 1) <= x < bpow ex)%R ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
  Rnd_UP_pt generic_format x (F2R (Float beta (Zceil (x * bpow (Zopp (fexp ex)))) (fexp ex))).
Proof.
intros x ex Hx1.
apply Rnd_DN_UP_pt_sym.
apply generic_format_satisfies_any.
unfold Zceil.
rewrite <- Ropp_mult_distr_l_reverse.
rewrite opp_F2R.
rewrite Zopp_involutive.
apply generic_DN_pt_neg.
now rewrite Ropp_involutive.
Qed.

Theorem generic_UP_pt_neg :
  forall x ex,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
673
  (bpow (ex - 1) <= - x < bpow ex)%R ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
674 675 676 677 678 679 680 681 682 683 684 685
  Rnd_UP_pt generic_format x (F2R (Float beta (Zceil (x * bpow (Zopp (fexp ex)))) (fexp ex))).
Proof.
intros x ex Hx1.
apply Rnd_DN_UP_pt_sym.
apply generic_format_satisfies_any.
unfold Zceil.
rewrite <- Ropp_mult_distr_l_reverse.
rewrite opp_F2R.
rewrite Zopp_involutive.
now apply generic_DN_pt_pos.
Qed.

686
End RND_generic.
687

688 689
Theorem canonic_fun_eq :
  forall beta : radix, forall f1 f2 : Z -> Z, forall x f,
690
  f1 (projT1 (ln_beta beta x)) = f2 (projT1 (ln_beta beta x)) ->
691 692 693 694 695 696 697 698
  canonic beta f1 x f -> canonic beta f2 x f.
Proof.
intros beta f1 f2 x f Hf (Hx1,Hx2).
split.
exact Hx1.
now rewrite <- Hf.
Qed.

699 700
Theorem generic_format_fun_eq :
  forall beta : radix, forall f1 f2 : Z -> Z, forall x,
701
  f1 (projT1 (ln_beta beta x)) = f2 (projT1 (ln_beta beta x)) ->
702 703
  generic_format beta f1 x -> generic_format beta f2 x.
Proof.
704
intros beta f1 f2 x Hf (f,Hx).
705
exists f.
706
now apply canonic_fun_eq with (1 := Hf).
707
Qed.