(** Definition and properties of pred and succ *)

Definition pred_pos x :=
  if Req_bool x (bpow (ln_beta beta x - 1)) then
    (x - bpow (fexp (ln_beta beta x - 1)))%R
  else
    (x - ulp x)%R.Definition succ x :=
   if (Rle_bool 0 x) then
          (x+ulp x)%R
   else
     (- pred_pos (-x))%R.Definition pred x := (- succ (-x))%R.

(* Specifications: *)
Theorem pred_eq_pos: forall x, (0 <= x)%R -> (pred x = pred_pos x)%R.
Theorem succ_eq_pos: forall x, (0 <= x)%R -> (succ x = x + ulp x)%R.
Lemma pred_eq_opp_succ_opp: forall x, pred x = (- succ (-x))%R.
Lemma succ_eq_opp_pred_opp: forall x, succ x = (- pred (-x))%R.
Lemma succ_opp: forall x, (succ (-x) = - pred x)%R.
Lemma pred_opp: forall x, (pred (-x) = - succ x)%R.
Theorem id_m_ulp_ge_bpow : forall x e, F x -> x <> ulp x -> (bpow e < x)%R -> (bpow e <= x - ulp x)%R.
Theorem id_p_ulp_le_bpow : forall x e, (0 < x)%R -> F x -> (x < bpow e)%R -> (x + ulp x <= bpow e)%R.
Lemma generic_format_pred_aux1: forall x, (0 < x)%R -> F x -> x <> bpow (ln_beta beta x - 1) -> F (x - ulp x).
Lemma generic_format_pred_aux2 : forall x, (0 < x)%R -> F x -> let e := ln_beta_val beta x (ln_beta beta x) in x = bpow (e - 1) -> F (x - bpow (fexp (e - 1))).
Theorem generic_format_succ_aux1 : forall x, (0 < x)%R -> F x -> F (x + ulp x).
Theorem generic_format_pred_pos : forall x, F x -> (0 < x)%R -> F (pred_pos x).
Theorem generic_format_succ : forall x, F x -> F (succ x).
Theorem generic_format_pred : forall x, F x -> F (pred x).
Theorem pred_pos_lt_id : forall x, (x <> 0)%R -> (pred_pos x < x)%R.
Theorem succ_gt_id : forall x, (x <> 0)%R -> (x < succ x)%R.
Theorem pred_lt_id : forall x, (x <> 0)%R -> (pred x < x)%R.
Theorem succ_ge_id : forall x, (x <= succ x)%R.
Theorem pred_le_id : forall x, (pred x <= x)%R.
Theorem pred_pos_ge_0 : forall x, (0 < x)%R -> F x -> (0 <= pred_pos x)%R.
Theorem pred_ge_0 : forall x, (0 < x)%R -> F x -> (0 <= pred x)%R.
Lemma pred_pos_plus_ulp_aux1 : forall x, (0 < x)%R -> F x -> x <> bpow (ln_beta beta x - 1) -> ((x - ulp x) + ulp (x-ulp x) = x)%R.
Lemma pred_pos_plus_ulp_aux2 : forall x, (0 < x)%R -> F x -> let e := ln_beta_val beta x (ln_beta beta x) in x = bpow (e - 1) -> (x - bpow (fexp (e-1)) <> 0)%R -> ((x - bpow (fexp (e-1))) + ulp (x - bpow (fexp (e-1))) = x)%R.
Lemma pred_pos_plus_ulp_aux3 : forall x, (0 < x)%R -> F x -> let e := ln_beta_val beta x (ln_beta beta x) in x = bpow (e - 1) -> (x - bpow (fexp (e-1)) = 0)%R -> (ulp 0 = x)%R.
Theorem pred_pos_plus_ulp : forall x, (0 < x)%R -> F x -> (pred_pos x + ulp (pred_pos x) = x)%R.
Theorem ln_beta_plus_eps: forall x, (0 < x)%R -> F x -> forall eps, (0 <= eps < ulp x)%R -> ln_beta beta (x + eps) = ln_beta beta x :> Z.
Theorem round_DN_plus_eps_pos: forall x, (0 <= x)%R -> F x -> forall eps, (0 <= eps < ulp x)%R -> round beta fexp Zfloor (x + eps) = x.
Theorem round_UP_plus_eps_pos : forall x, (0 <= x)%R -> F x -> forall eps, (0 < eps <= ulp x)%R -> round beta fexp Zceil (x + eps) = (x + ulp x)%R.
Theorem round_UP_pred_plus_eps_pos : forall x, (0 < x)%R -> F x -> forall eps, (0 < eps <= ulp (pred x) )%R -> round beta fexp Zceil (pred x + eps) = x.
Theorem round_DN_minus_eps_pos : forall x, (0 < x)%R -> F x -> forall eps, (0 < eps <= ulp (pred x))%R -> round beta fexp Zfloor (x - eps) = pred x.
Theorem round_DN_plus_eps: forall x, F x -> forall eps, (0 <= eps < if (Rle_bool 0 x) then (ulp x) else (ulp (pred (-x))))%R -> round beta fexp Zfloor (x + eps) = x.
Theorem round_UP_plus_eps : forall x, F x -> forall eps, (0 < eps <= if (Rle_bool 0 x) then (ulp x) else (ulp (pred (-x))))%R -> round beta fexp Zceil (x + eps) = (succ x)%R.
Lemma le_pred_pos_lt : forall x y, F x -> F y -> (0 <= x < y)%R -> (x <= pred_pos y)%R.
Theorem succ_le_lt_aux: forall x y, F x -> F y -> (0 <= x)%R -> (x < y)%R -> (succ x <= y)%R.
Theorem succ_le_lt: forall x y, F x -> F y -> (x < y)%R -> (succ x <= y)%R.
Theorem le_pred_lt : forall x y, F x -> F y -> (x < y)%R -> (x <= pred y)%R.
Theorem lt_succ_le : forall x y, (y <> 0)%R -> (x <= y)%R -> (x < succ y)%R.
Theorem pred_lt_le : forall x y, (x <> 0)%R -> (x <= y)%R -> (pred x < y)%R.
Theorem succ_pred_aux : forall x, F x -> (0 < x)%R -> succ (pred x)=x.
Theorem pred_ulp_0 : pred (ulp 0) = 0%R.
Theorem succ_0 : succ 0 = ulp 0.
Theorem pred_0 : pred 0 = Ropp (ulp 0).
Theorem pred_succ_aux : forall x, F x -> (0 < x)%R -> pred (succ x) = x.
Theorem succ_pred : forall x, F x -> succ (pred x) = x.
Theorem pred_succ : forall x, F x -> pred (succ x) = x.
Theorem round_UP_pred_plus_eps : forall x, F x -> forall eps, (0 < eps <= if (Rle_bool x 0) then (ulp x) else (ulp (pred x)))%R -> round beta fexp Zceil (pred x + eps) = x.
Theorem round_DN_minus_eps: forall x, F x -> forall eps, (0 < eps <= if (Rle_bool x 0) then (ulp x) else (ulp (pred x)))%R -> round beta fexp Zfloor (x - eps) = pred x.
Theorem error_lt_ulp : forall rnd { Zrnd : Valid_rnd rnd } x, (x <> 0)%R -> (Rabs (round beta fexp rnd x - x) < ulp x)%R.
Theorem error_le_ulp : forall rnd { Zrnd : Valid_rnd rnd } x, (Rabs (round beta fexp rnd x - x) <= ulp x)%R.
Theorem error_le_half_ulp : forall choice x, (Rabs (round beta fexp (Znearest choice) x - x) <= /2 * ulp x)%R.
Theorem ulp_DN : forall x, (0 < round beta fexp Zfloor x)%R -> ulp (round beta fexp Zfloor x) = ulp x.
Theorem round_neq_0_negligible_exp: negligible_exp=None -> forall rnd { Zrnd : Valid_rnd rnd } x, (x <> 0)%R -> (round beta fexp rnd x <> 0)%R.
Theorem error_lt_ulp_round : forall { Hm : Monotone_exp fexp } rnd { Zrnd : Valid_rnd rnd } x, ( x <> 0)%R -> (Rabs (round beta fexp rnd x - x) < ulp (round beta fexp rnd x))%R.
Theorem error_le_half_ulp_round : forall { Hm : Monotone_exp fexp }, forall choice x, (Rabs (round beta fexp (Znearest choice) x - x) <= /2 * ulp (round beta fexp (Znearest choice) x))%R.
Theorem pred_le : forall x y, F x -> F y -> (x <= y)%R -> (pred x <= pred y)%R.
Theorem succ_le: forall x y, F x -> F y -> (x <= y)%R -> (succ x <= succ y)%R.
Theorem pred_le_inv: forall x y, F x -> F y -> (pred x <= pred y)%R -> (x <= y)%R.
Theorem succ_le_inv: forall x y, F x -> F y -> (succ x <= succ y)%R -> (x <= y)%R.
Theorem pred_lt : forall x y, F x -> F y -> (x < y)%R -> (pred x < pred y)%R.
Theorem succ_lt : forall x y, F x -> F y -> (x < y)%R -> (succ x < succ y)%R.
Theorem le_round_DN_lt_UP : forall x y, F y -> (y < round beta fexp Zceil x -> y <= round beta fexp Zfloor x)%R.
Theorem round_UP_le_gt_DN : forall x y, F y -> (round beta fexp Zfloor x < y -> round beta fexp Zceil x <= y)%R.
Theorem pred_UP_le_DN : forall x, (pred (round beta fexp Zceil x) <= round beta fexp Zfloor x)%R.
Theorem pred_UP_eq_DN : forall x, ~ F x -> (pred (round beta fexp Zceil x) = round beta fexp Zfloor x)%R.
Theorem succ_DN_eq_UP : forall x, ~ F x -> (succ (round beta fexp Zfloor x) = round beta fexp Zceil x)%R.
Theorem round_DN_eq_betw: forall x d, F d -> (d <= x < succ d)%R -> round beta fexp Zfloor x = d.
Theorem round_UP_eq_betw: forall x u, F u -> (pred u < x <= u)%R -> round beta fexp Zceil x = u.
Theorem round_N_le_midp: forall choice u v, F u -> (v < (u + succ u)/2)%R -> (round beta fexp (Znearest choice) v <= u)%R.
Theorem round_N_ge_midp: forall choice u v, F u -> ((u + pred u)/2 < v)%R -> (u <= round beta fexp (Znearest choice) v)%R.
Lemma round_N_eq_DN: forall choice x, let d:=round beta fexp Zfloor x in let u:=round beta fexp Zceil x in (x<(d+u)/2)%R -> round beta fexp (Znearest choice) x = d.
Lemma round_N_eq_DN_pt: forall choice x d u, Rnd_DN_pt F x d -> Rnd_UP_pt F x u -> (x<(d+u)/2)%R -> round beta fexp (Znearest choice) x = d.
Lemma round_N_eq_UP: forall choice x, let d:=round beta fexp Zfloor x in let u:=round beta fexp Zceil x in ((d+u)/2 < x)%R -> round beta fexp (Znearest choice) x = u.
Lemma round_N_eq_UP_pt: forall choice x d u, Rnd_DN_pt F x d -> Rnd_UP_pt F x u -> ((d+u)/2 < x)%R -> round beta fexp (Znearest choice) x = u.
Theorem pred_eq_pos: forall x, (0 <= x)%R -> (pred x = pred_pos x)%R.
Theorem succ_eq_pos: forall x, (0 <= x)%R -> (succ x = x + ulp x)%R.
Lemma pred_eq_opp_succ_opp: forall x, pred x = (- succ (-x))%R.
Lemma succ_eq_opp_pred_opp: forall x, succ x = (- pred (-x))%R.
Lemma succ_opp: forall x, (succ (-x) = - pred x)%R.
Lemma pred_opp: forall x, (pred (-x) = - succ x)%R.
Theorem id_m_ulp_ge_bpow : forall x e, F x -> x <> ulp x -> (bpow e < x)%R -> (bpow e <= x - ulp x)%R.
Theorem id_p_ulp_le_bpow : forall x e, (0 < x)%R -> F x -> (x < bpow e)%R -> (x + ulp x <= bpow e)%R.
Lemma generic_format_pred_aux1: forall x, (0 < x)%R -> F x -> x <> bpow (ln_beta beta x - 1) -> F (x - ulp x).
Lemma generic_format_pred_aux2 : forall x, (0 < x)%R -> F x -> let e := ln_beta_val beta x (ln_beta beta x) in x = bpow (e - 1) -> F (x - bpow (fexp (e - 1))).
Theorem generic_format_succ_aux1 : forall x, (0 < x)%R -> F x -> F (x + ulp x).
Theorem generic_format_pred_pos : forall x, F x -> (0 < x)%R -> F (pred_pos x).
Theorem generic_format_succ : forall x, F x -> F (succ x).
Theorem generic_format_pred : forall x, F x -> F (pred x).
Theorem pred_pos_lt_id : forall x, (x <> 0)%R -> (pred_pos x < x)%R.
Theorem succ_gt_id : forall x, (x <> 0)%R -> (x < succ x)%R.
Theorem pred_lt_id : forall x, (x <> 0)%R -> (pred x < x)%R.
Theorem succ_ge_id : forall x, (x <= succ x)%R.
Theorem pred_le_id : forall x, (pred x <= x)%R.
Theorem pred_pos_ge_0 : forall x, (0 < x)%R -> F x -> (0 <= pred_pos x)%R.
Theorem pred_ge_0 : forall x, (0 < x)%R -> F x -> (0 <= pred x)%R.
Lemma pred_pos_plus_ulp_aux1 : forall x, (0 < x)%R -> F x -> x <> bpow (ln_beta beta x - 1) -> ((x - ulp x) + ulp (x-ulp x) = x)%R.
Lemma pred_pos_plus_ulp_aux2 : forall x, (0 < x)%R -> F x -> let e := ln_beta_val beta x (ln_beta beta x) in x = bpow (e - 1) -> (x - bpow (fexp (e-1)) <> 0)%R -> ((x - bpow (fexp (e-1))) + ulp (x - bpow (fexp (e-1))) = x)%R.
Lemma pred_pos_plus_ulp_aux3 : forall x, (0 < x)%R -> F x -> let e := ln_beta_val beta x (ln_beta beta x) in x = bpow (e - 1) -> (x - bpow (fexp (e-1)) = 0)%R -> (ulp 0 = x)%R.
Theorem pred_pos_plus_ulp : forall x, (0 < x)%R -> F x -> (pred_pos x + ulp (pred_pos x) = x)%R.
Theorem ln_beta_plus_eps: forall x, (0 < x)%R -> F x -> forall eps, (0 <= eps < ulp x)%R -> ln_beta beta (x + eps) = ln_beta beta x :> Z.
Theorem round_DN_plus_eps_pos: forall x, (0 <= x)%R -> F x -> forall eps, (0 <= eps < ulp x)%R -> round beta fexp Zfloor (x + eps) = x.
Theorem round_UP_plus_eps_pos : forall x, (0 <= x)%R -> F x -> forall eps, (0 < eps <= ulp x)%R -> round beta fexp Zceil (x + eps) = (x + ulp x)%R.
Theorem round_UP_pred_plus_eps_pos : forall x, (0 < x)%R -> F x -> forall eps, (0 < eps <= ulp (pred x) )%R -> round beta fexp Zceil (pred x + eps) = x.
Theorem round_DN_minus_eps_pos : forall x, (0 < x)%R -> F x -> forall eps, (0 < eps <= ulp (pred x))%R -> round beta fexp Zfloor (x - eps) = pred x.
Theorem round_DN_plus_eps: forall x, F x -> forall eps, (0 <= eps < if (Rle_bool 0 x) then (ulp x) else (ulp (pred (-x))))%R -> round beta fexp Zfloor (x + eps) = x.
Theorem round_UP_plus_eps : forall x, F x -> forall eps, (0 < eps <= if (Rle_bool 0 x) then (ulp x) else (ulp (pred (-x))))%R -> round beta fexp Zceil (x + eps) = (succ x)%R.
Lemma le_pred_pos_lt : forall x y, F x -> F y -> (0 <= x < y)%R -> (x <= pred_pos y)%R.
Theorem succ_le_lt_aux: forall x y, F x -> F y -> (0 <= x)%R -> (x < y)%R -> (succ x <= y)%R.
Theorem succ_le_lt: forall x y, F x -> F y -> (x < y)%R -> (succ x <= y)%R.
Theorem le_pred_lt : forall x y, F x -> F y -> (x < y)%R -> (x <= pred y)%R.
Theorem lt_succ_le: forall x y, F x -> F y -> (y <> 0)%R -> (x <= y)%R -> (x < succ y)%R.
Theorem succ_pred_aux : forall x, F x -> (0 < x)%R -> succ (pred x)=x.
Theorem pred_succ_aux_0 : (pred (succ 0)=0)%R.
Theorem pred_succ_aux : forall x, F x -> (0 < x)%R -> pred (succ x)=x.
Theorem succ_pred : forall x, F x -> succ (pred x)=x.
Theorem pred_succ : forall x, F x -> pred (succ x)=x.
Theorem round_UP_pred_plus_eps : forall x, F x -> forall eps, (0 < eps <= if (Rle_bool x 0) then (ulp x) else (ulp (pred x)))%R -> round beta fexp Zceil (pred x + eps) = x.
Theorem round_DN_minus_eps: forall x, F x -> forall eps, (0 < eps <= if (Rle_bool x 0) then (ulp x) else (ulp (pred x)))%R -> round beta fexp Zfloor (x - eps) = pred x.
Theorem error_lt_ulp : forall rnd { Zrnd : Valid_rnd rnd } x, (x <> 0)%R -> (Rabs (round beta fexp rnd x - x) < ulp x)%R.
Theorem error_le_ulp : forall rnd { Zrnd : Valid_rnd rnd } x, (Rabs (round beta fexp rnd x - x) <= ulp x)%R.
Theorem error_le_half_ulp : forall choice x, (Rabs (round beta fexp (Znearest choice) x - x) <= /2 * ulp x)%R.
Theorem ulp_DN : forall x, (0 < round beta fexp Zfloor x)%R -> ulp (round beta fexp Zfloor x) = ulp x.
Theorem round_neq_0_negligible_exp: negligible_exp=None -> forall rnd { Zrnd : Valid_rnd rnd } x, (x <> 0)%R -> (round beta fexp rnd x <> 0)%R.
Theorem error_lt_ulp_round : forall { Hm : Monotone_exp fexp } rnd { Zrnd : Valid_rnd rnd } x, ( x <> 0)%R -> (Rabs (round beta fexp rnd x - x) < ulp (round beta fexp rnd x))%R.
Theorem error_le_half_ulp_round : forall { Hm : Monotone_exp fexp }, forall choice x, (Rabs (round beta fexp (Znearest choice) x - x) <= /2 * ulp (round beta fexp (Znearest choice) x))%R.
Theorem pred_le: forall x y, F x -> F y -> (x <= y)%R -> (pred x <= pred y)%R.
Theorem succ_le: forall x y, F x -> F y -> (x <= y)%R -> (succ x <= succ y)%R.
Theorem pred_le_inv: forall x y, F x -> F y -> (pred x <= pred y)%R -> (x <= y)%R.
Theorem succ_le_inv: forall x y, F x -> F y -> (succ x <= succ y)%R -> (x <= y)%R.
Theorem le_round_DN_lt_UP : forall x y, F y -> (y < round beta fexp Zceil x -> y <= round beta fexp Zfloor x)%R.
Theorem round_UP_le_gt_DN : forall x y, F y -> (round beta fexp Zfloor x < y -> round beta fexp Zceil x <= y)%R.
Theorem pred_UP_le_DN : forall x, (pred (round beta fexp Zceil x) <= round beta fexp Zfloor x)%R.
Theorem pred_UP_eq_DN : forall x, ~ F x -> (pred (round beta fexp Zceil x) = round beta fexp Zfloor x)%R.
Theorem succ_DN_eq_UP : forall x, ~ F x -> (succ (round beta fexp Zfloor x) = round beta fexp Zceil x)%R.
Theorem round_DN_eq_betw: forall x d, F d -> (d <= x < succ d)%R -> round beta fexp Zfloor x = d.
Theorem round_UP_eq_betw: forall x u, F u -> (pred u < x <= u)%R -> round beta fexp Zceil x = u.
Theorem round_N_le_midp: forall choice u v, F u -> (v < (u + succ u)/2)%R -> (round beta fexp (Znearest choice) v <= u)%R.
Theorem round_N_ge_midp: forall choice u v, F u -> ((u + pred u)/2 < v)%R -> (u <= round beta fexp (Znearest choice) v)%R.
Lemma round_N_eq_DN: forall choice x, let d:=round beta fexp Zfloor x in let u:=round beta fexp Zceil x in (x<(d+u)/2)%R -> round beta fexp (Znearest choice) x = d.
Lemma round_N_eq_DN_pt: forall choice x d u, Rnd_DN_pt F x d -> Rnd_UP_pt F x u -> (x<(d+u)/2)%R -> round beta fexp (Znearest choice) x = d.
Lemma round_N_eq_UP: forall choice x, let d:=round beta fexp Zfloor x in let u:=round beta fexp Zceil x in ((d+u)/2 < x)%R -> round beta fexp (Znearest choice) x = u.
Lemma round_N_eq_UP_pt: forall choice x d u, Rnd_DN_pt F x d -> Rnd_UP_pt F x u -> ((d+u)/2 < x)%R -> round beta fexp (Znearest choice) x = u.