Commit 72dcf8b0 authored by Guillaume Melquiond's avatar Guillaume Melquiond
Browse files

Added exponent parameter to rounding functions.

parent fb290c2c
...@@ -528,11 +528,11 @@ Qed. ...@@ -528,11 +528,11 @@ Qed.
Theorem inbetween_float_rounding : Theorem inbetween_float_rounding :
forall rnd choice, forall rnd choice,
( forall x m l, inbetween_int m x l -> Zrnd rnd x = choice m l ) -> ( forall x m e l, inbetween_int m x l -> Zrnd rnd x e = choice m e l ) ->
forall x m l, forall x m l,
let e := canonic_exponent beta fexp x in let e := canonic_exponent beta fexp x in
inbetween_float m e x l -> inbetween_float m e x l ->
rounding beta fexp rnd x = F2R (Float beta (choice m l) e). rounding beta fexp rnd x = F2R (Float beta (choice m e l) e).
Proof. Proof.
intros rnd choice Hc x m l e Hl. intros rnd choice Hc x m l e Hl.
unfold rounding, F2R. simpl. unfold rounding, F2R. simpl.
...@@ -549,9 +549,9 @@ Theorem inbetween_float_DN : ...@@ -549,9 +549,9 @@ Theorem inbetween_float_DN :
inbetween_float m e x l -> inbetween_float m e x l ->
rounding beta fexp ZrndDN x = F2R (Float beta m e). rounding beta fexp ZrndDN x = F2R (Float beta m e).
Proof. Proof.
apply inbetween_float_rounding with (choice := fun m l => m). apply inbetween_float_rounding with (choice := fun m e l => m).
intros x m l Hl. intros x m e l Hl.
apply Zfloor_imp. refine (Zfloor_imp m _ _).
apply inbetween_bounds with (2 := Hl). apply inbetween_bounds with (2 := Hl).
apply Z2R_lt. apply Z2R_lt.
apply Zlt_succ. apply Zlt_succ.
...@@ -571,8 +571,8 @@ Theorem inbetween_float_UP : ...@@ -571,8 +571,8 @@ Theorem inbetween_float_UP :
inbetween_float m e x l -> inbetween_float m e x l ->
rounding beta fexp ZrndUP x = F2R (Float beta (cond_incr (round_UP l) m) e). rounding beta fexp ZrndUP x = F2R (Float beta (cond_incr (round_UP l) m) e).
Proof. Proof.
apply inbetween_float_rounding with (choice := fun m l => cond_incr (round_UP l) m). apply inbetween_float_rounding with (choice := fun m e l => cond_incr (round_UP l) m).
intros x m l Hl. intros x m e l Hl.
assert (Hl': l = loc_Exact \/ (l <> loc_Exact /\ round_UP l = true)). assert (Hl': l = loc_Exact \/ (l <> loc_Exact /\ round_UP l = true)).
case l ; try (now left) ; now right ; split. case l ; try (now left) ; now right ; split.
destruct Hl' as [Hl'|(Hl1, Hl2)]. destruct Hl' as [Hl'|(Hl1, Hl2)].
...@@ -580,7 +580,7 @@ destruct Hl' as [Hl'|(Hl1, Hl2)]. ...@@ -580,7 +580,7 @@ destruct Hl' as [Hl'|(Hl1, Hl2)].
rewrite Hl'. rewrite Hl'.
destruct Hl ; try easy. destruct Hl ; try easy.
rewrite H. rewrite H.
apply Zceil_Z2R. exact (Zceil_Z2R _).
(* not Exact *) (* not Exact *)
rewrite Hl2. rewrite Hl2.
simpl. simpl.
...@@ -604,14 +604,14 @@ Theorem inbetween_float_NE : ...@@ -604,14 +604,14 @@ Theorem inbetween_float_NE :
inbetween_float m e x l -> inbetween_float m e x l ->
rounding beta fexp ZrndNE x = F2R (Float beta (cond_incr (round_NE (Zeven m) l) m) e). rounding beta fexp ZrndNE x = F2R (Float beta (cond_incr (round_NE (Zeven m) l) m) e).
Proof. Proof.
apply inbetween_float_rounding with (choice := fun m l => cond_incr (round_NE (Zeven m) l) m). apply inbetween_float_rounding with (choice := fun m e l => cond_incr (round_NE (Zeven m) l) m).
intros x m l Hl. intros x m e l Hl.
inversion_clear Hl as [Hx|l' Hx Hl']. inversion_clear Hl as [Hx|l' Hx Hl'].
(* Exact *) (* Exact *)
rewrite Hx. rewrite Hx.
now rewrite Zrnd_Z2R. now rewrite Zrnd_Z2R.
(* not Exact *) (* not Exact *)
unfold Zrnd, ZrndNE, ZrndN, Znearest. unfold Zrnd, ZrndNE, ZrndN, Znearest, mkZrounding2.
assert (Hm: Zfloor x = m). assert (Hm: Zfloor x = m).
apply Zfloor_imp. apply Zfloor_imp.
exact (conj (Rlt_le _ _ (proj1 Hx)) (proj2 Hx)). exact (conj (Rlt_le _ _ (proj1 Hx)) (proj2 Hx)).
......
...@@ -294,9 +294,9 @@ Qed. ...@@ -294,9 +294,9 @@ Qed.
Section Fcore_generic_rounding_pos. Section Fcore_generic_rounding_pos.
Record Zrounding := mkZrounding { Record Zrounding := mkZrounding {
Zrnd : R -> Z ; Zrnd : R -> Z -> Z ;
Zrnd_monotone : forall x y, (x <= y)%R -> (Zrnd x <= Zrnd y)%Z ; Zrnd_monotone : forall x y e, (x <= y)%R -> (Zrnd x e <= Zrnd y e)%Z ;
Zrnd_Z2R : forall n, Zrnd (Z2R n) = n Zrnd_Z2R : forall n e, Zrnd (Z2R n) e = n
}. }.
Variable rnd : Zrounding. Variable rnd : Zrounding.
...@@ -305,18 +305,18 @@ Let Zrnd_monotone := Zrnd_monotone rnd. ...@@ -305,18 +305,18 @@ Let Zrnd_monotone := Zrnd_monotone rnd.
Let Zrnd_Z2R := Zrnd_Z2R rnd. Let Zrnd_Z2R := Zrnd_Z2R rnd.
Theorem Zrnd_DN_or_UP : Theorem Zrnd_DN_or_UP :
forall x, Zrnd x = Zfloor x \/ Zrnd x = Zceil x. forall x e, Zrnd x e = Zfloor x \/ Zrnd x e = Zceil x.
Proof. Proof.
intros x. intros x e.
destruct (Zle_or_lt (Zrnd x) (Zfloor x)) as [Hx|Hx]. destruct (Zle_or_lt (Zrnd x e) (Zfloor x)) as [Hx|Hx].
left. left.
apply Zle_antisym with (1 := Hx). apply Zle_antisym with (1 := Hx).
rewrite <- (Zrnd_Z2R (Zfloor x)). rewrite <- (Zrnd_Z2R (Zfloor x) e).
apply Zrnd_monotone. apply Zrnd_monotone.
apply Zfloor_lb. apply Zfloor_lb.
right. right.
apply Zle_antisym. apply Zle_antisym.
rewrite <- (Zrnd_Z2R (Zceil x)). rewrite <- (Zrnd_Z2R (Zceil x) e).
apply Zrnd_monotone. apply Zrnd_monotone.
apply Zceil_ub. apply Zceil_ub.
rewrite Zceil_floor_neq. rewrite Zceil_floor_neq.
...@@ -328,7 +328,7 @@ apply Zlt_irrefl with (1 := Hx). ...@@ -328,7 +328,7 @@ apply Zlt_irrefl with (1 := Hx).
Qed. Qed.
Definition rounding x := Definition rounding x :=
F2R (Float beta (Zrnd (scaled_mantissa x)) (canonic_exponent x)). F2R (Float beta (Zrnd (scaled_mantissa x) (canonic_exponent x)) (canonic_exponent x)).
Theorem rounding_monotone_pos : Theorem rounding_monotone_pos :
forall x y, (0 < x)%R -> (x <= y)%R -> (rounding x <= rounding y)%R. forall x y, (0 < x)%R -> (x <= y)%R -> (rounding x <= rounding y)%R.
...@@ -365,14 +365,14 @@ apply Zrnd_monotone. ...@@ -365,14 +365,14 @@ apply Zrnd_monotone.
apply Rmult_le_compat_r. apply Rmult_le_compat_r.
apply bpow_ge_0. apply bpow_ge_0.
exact Hxy. exact Hxy.
apply Rle_trans with (F2R (Float beta (Zrnd (bpow (ey - 1) * bpow (- fexp ey))%R) (fexp ey))). apply Rle_trans with (F2R (Float beta (Zrnd (bpow (ey - 1) * bpow (- fexp ey)) (fexp ey)) (fexp ey))).
rewrite <- bpow_add. rewrite <- bpow_add.
rewrite <- (Z2R_Zpower beta (ey - 1 + -fexp ey)). 2: omega. rewrite <- (Z2R_Zpower beta (ey - 1 + -fexp ey)). 2: omega.
rewrite Zrnd_Z2R. rewrite Zrnd_Z2R.
destruct (Zle_or_lt ex (fexp ex)) as [Hx1|Hx1]. destruct (Zle_or_lt ex (fexp ex)) as [Hx1|Hx1].
apply Rle_trans with (F2R (Float beta 1 (fexp ex))). apply Rle_trans with (F2R (Float beta 1 (fexp ex))).
apply F2R_le_compat. apply F2R_le_compat.
rewrite <- (Zrnd_Z2R 1). rewrite <- (Zrnd_Z2R 1 (fexp ex)).
apply Zrnd_monotone. apply Zrnd_monotone.
apply Rlt_le. apply Rlt_le.
exact (proj2 (mantissa_small_pos _ _ Hex Hx1)). exact (proj2 (mantissa_small_pos _ _ Hex Hx1)).
...@@ -381,7 +381,7 @@ rewrite Z2R_Zpower. 2: omega. ...@@ -381,7 +381,7 @@ rewrite Z2R_Zpower. 2: omega.
rewrite <- bpow_add, Rmult_1_l. rewrite <- bpow_add, Rmult_1_l.
apply -> bpow_le. apply -> bpow_le.
omega. omega.
apply Rle_trans with (F2R (Float beta (Zrnd (bpow ex * bpow (- fexp ex))%R) (fexp ex))). apply Rle_trans with (F2R (Float beta (Zrnd (bpow ex * bpow (- fexp ex)) (fexp ex)) (fexp ex))).
apply F2R_le_compat. apply F2R_le_compat.
apply Zrnd_monotone. apply Zrnd_monotone.
apply Rmult_le_compat_r. apply Rmult_le_compat_r.
...@@ -441,7 +441,7 @@ intros x ex He Hx. ...@@ -441,7 +441,7 @@ intros x ex He Hx.
unfold rounding, scaled_mantissa. unfold rounding, scaled_mantissa.
rewrite (canonic_exponent_fexp_pos _ _ Hx). rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl. unfold F2R. simpl.
destruct (Zrnd_DN_or_UP (x * bpow (- fexp ex))) as [Hr|Hr] ; rewrite Hr. destruct (Zrnd_DN_or_UP (x * bpow (- fexp ex)) (fexp ex)) as [Hr|Hr] ; rewrite Hr.
(* DN *) (* DN *)
split. split.
replace (ex - 1)%Z with (ex - 1 + - fexp ex + fexp ex)%Z by ring. replace (ex - 1)%Z with (ex - 1 + - fexp ex + fexp ex)%Z by ring.
...@@ -500,7 +500,7 @@ intros x ex He Hx. ...@@ -500,7 +500,7 @@ intros x ex He Hx.
unfold rounding, scaled_mantissa. unfold rounding, scaled_mantissa.
rewrite (canonic_exponent_fexp_pos _ _ Hx). rewrite (canonic_exponent_fexp_pos _ _ Hx).
unfold F2R. simpl. unfold F2R. simpl.
destruct (Zrnd_DN_or_UP (x * bpow (-fexp ex))) as [Hr|Hr] ; rewrite Hr. destruct (Zrnd_DN_or_UP (x * bpow (-fexp ex)) (fexp ex)) as [Hr|Hr] ; rewrite Hr.
(* DN *) (* DN *)
left. left.
apply Rmult_eq_0_compat_r. apply Rmult_eq_0_compat_r.
...@@ -556,21 +556,23 @@ Section Zrounding_opp. ...@@ -556,21 +556,23 @@ Section Zrounding_opp.
Variable rnd : Zrounding. Variable rnd : Zrounding.
Definition Zrnd_opp x := Zopp (Zrnd rnd (-x)). Definition Zrnd_opp x e := Zopp (Zrnd rnd (-x) e).
Lemma Zrnd_opp_le : Lemma Zrnd_opp_le :
forall x y, (x <= y)%R -> (Zrnd_opp x <= Zrnd_opp y)%Z. forall x y e, (x <= y)%R -> (Zrnd_opp x e <= Zrnd_opp y e)%Z.
Proof. Proof.
intros x y Hxy. intros x y e Hxy.
unfold Zrnd_opp. unfold Zrnd_opp.
generalize (Zrnd_monotone rnd _ _ (Ropp_le_contravar _ _ Hxy)). apply Zopp_le_cancel.
omega. rewrite 2!Zopp_involutive.
apply Zrnd_monotone.
now apply Ropp_le_contravar.
Qed. Qed.
Lemma Zrnd_opp_Z2R : Lemma Zrnd_opp_Z2R :
forall n, Zrnd_opp (Z2R n) = n. forall n e, Zrnd_opp (Z2R n) e = n.
Proof. Proof.
intros n. intros n e.
unfold Zrnd_opp. unfold Zrnd_opp.
rewrite <- opp_Z2R, Zrnd_Z2R. rewrite <- opp_Z2R, Zrnd_Z2R.
apply Zopp_involutive. apply Zopp_involutive.
...@@ -592,8 +594,11 @@ Qed. ...@@ -592,8 +594,11 @@ Qed.
End Zrounding_opp. End Zrounding_opp.
Definition ZrndDN := mkZrounding Zfloor Zfloor_le Zfloor_Z2R. Definition mkZrounding2 rnd (mono : forall x y, (x <= y)%R -> (rnd x <= rnd y)%Z) (z2r : forall n, rnd (Z2R n) = n) :=
Definition ZrndUP := mkZrounding Zceil Zceil_le Zceil_Z2R. mkZrounding (fun x _ => rnd x) (fun x y _ => mono x y) (fun n _ => z2r n).
Definition ZrndDN := mkZrounding2 Zfloor Zfloor_le Zfloor_Z2R.
Definition ZrndUP := mkZrounding2 Zceil Zceil_le Zceil_Z2R.
Theorem rounding_DN_or_UP : Theorem rounding_DN_or_UP :
forall rnd x, forall rnd x,
...@@ -602,7 +607,7 @@ Proof. ...@@ -602,7 +607,7 @@ Proof.
intros rnd x. intros rnd x.
unfold rounding. unfold rounding.
unfold Zrnd at 2 4. simpl. unfold Zrnd at 2 4. simpl.
destruct (Zrnd_DN_or_UP rnd (scaled_mantissa x)) as [Hx|Hx]. destruct (Zrnd_DN_or_UP rnd (scaled_mantissa x) (canonic_exponent x)) as [Hx|Hx].
left. now rewrite Hx. left. now rewrite Hx.
right. now rewrite Hx. right. now rewrite Hx.
Qed. Qed.
...@@ -629,7 +634,7 @@ now apply Ropp_le_contravar. ...@@ -629,7 +634,7 @@ now apply Ropp_le_contravar.
(* . 0 <= y *) (* . 0 <= y *)
apply Rle_trans with R0. apply Rle_trans with R0.
apply F2R_le_0_compat. simpl. apply F2R_le_0_compat. simpl.
rewrite <- (Zrnd_Z2R rnd 0). rewrite <- (Zrnd_Z2R rnd 0 (canonic_exponent x)).
apply Zrnd_monotone. apply Zrnd_monotone.
simpl. simpl.
rewrite <- (Rmult_0_l (bpow (- fexp (projT1 (ln_beta beta x))))). rewrite <- (Rmult_0_l (bpow (- fexp (projT1 (ln_beta beta x))))).
...@@ -637,7 +642,7 @@ apply Rmult_le_compat_r. ...@@ -637,7 +642,7 @@ apply Rmult_le_compat_r.
apply bpow_ge_0. apply bpow_ge_0.
now apply Rlt_le. now apply Rlt_le.
apply F2R_ge_0_compat. simpl. apply F2R_ge_0_compat. simpl.
rewrite <- (Zrnd_Z2R rnd 0). rewrite <- (Zrnd_Z2R rnd 0 (canonic_exponent y)).
apply Zrnd_monotone. apply Zrnd_monotone.
apply Rmult_le_pos. apply Rmult_le_pos.
exact Hy. exact Hy.
...@@ -647,7 +652,7 @@ rewrite Hx. ...@@ -647,7 +652,7 @@ rewrite Hx.
rewrite rounding_0. rewrite rounding_0.
apply F2R_ge_0_compat. apply F2R_ge_0_compat.
simpl. simpl.
rewrite <- (Zrnd_Z2R rnd 0). rewrite <- (Zrnd_Z2R rnd 0 (canonic_exponent y)).
apply Zrnd_monotone. apply Zrnd_monotone.
apply Rmult_le_pos. apply Rmult_le_pos.
now rewrite <- Hx. now rewrite <- Hx.
...@@ -1135,7 +1140,7 @@ rewrite opp_Z2R. ...@@ -1135,7 +1140,7 @@ rewrite opp_Z2R.
apply Rplus_comm. apply Rplus_comm.
Qed. Qed.
Definition ZrndN := mkZrounding Znearest Znearest_monotone Znearest_Z2R. Definition ZrndN := mkZrounding2 Znearest Znearest_monotone Znearest_Z2R.
Theorem Znearest_N_strict : Theorem Znearest_N_strict :
forall x, forall x,
......
...@@ -77,10 +77,10 @@ apply Hex. ...@@ -77,10 +77,10 @@ apply Hex.
apply Hey. apply Hey.
(* *) (* *)
assert (Hr: ((F2R (Float beta (- (Ztrunc (scaled_mantissa beta (FLX_exp prec) x) * assert (Hr: ((F2R (Float beta (- (Ztrunc (scaled_mantissa beta (FLX_exp prec) x) *
Ztrunc (scaled_mantissa beta (FLX_exp prec) y)) + Zrnd rnd (scaled_mantissa beta (FLX_exp prec) (x * y)) * Ztrunc (scaled_mantissa beta (FLX_exp prec) y)) + Zrnd rnd (scaled_mantissa beta (FLX_exp prec) (x * y)) (canonic_exponent beta (FLX_exp prec) (x * y)) *
radix_val beta ^ (cexp (x * y)%R - (cexp x + cexp y))) (cexp x + cexp y))) = f - x * y)%R). radix_val beta ^ (cexp (x * y)%R - (cexp x + cexp y))) (cexp x + cexp y))) = f - x * y)%R).
rewrite Hx at 6. rewrite Hx at 7.
rewrite Hy at 6. rewrite Hy at 7.
rewrite <- mult_F2R. rewrite <- mult_F2R.
simpl. simpl.
unfold f, rounding, Rminus. unfold f, rounding, Rminus.
......
...@@ -30,7 +30,7 @@ rewrite Z2R_Zpower. 2: omega. ...@@ -30,7 +30,7 @@ rewrite Z2R_Zpower. 2: omega.
rewrite <- bpow_add. rewrite <- bpow_add.
apply (f_equal (fun v => Z2R m * bpow v)%R). apply (f_equal (fun v => Z2R m * bpow v)%R).
ring. ring.
exists ((Zrnd rnd (Z2R m * bpow (e - e'))) * Zpower (radix_val beta) (e' - e))%Z. exists ((Zrnd rnd (Z2R m * bpow (e - e')) e') * Zpower (radix_val beta) (e' - e))%Z.
unfold F2R. simpl. unfold F2R. simpl.
rewrite mult_Z2R. rewrite mult_Z2R.
rewrite Z2R_Zpower. 2: omega. rewrite Z2R_Zpower. 2: omega.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment