Library FroundDivSqrt

Require Export AllFloat.
Require Export Classical.

Section FroundDiv.
Variable b : Fbound.
Variable radix : Z.
Variable precision : nat.

Let FtoRradix := FtoR radix.
Coercion FtoRradix : float >-> R.
Hypothesis radixMoreThanOne : (1 < radix)%Z.

Let radixMoreThanZERO := Zlt_1_O _ (Zlt_le_weak _ _ radixMoreThanOne).
Hint Resolve radixMoreThanZERO: zarith.
Hypothesis precisionGreaterThanOne : 1 < precision.
Hypothesis pGivesBound : Zpos (vNum b) = Zpower_nat radix precision.

Theorem NearestInteger :
 forall (r : R) (n : Z),
 (forall z : Z, (Rabs (n - r) <= Rabs (z - r))%R) ->
 (Rabs (n - r) <= / 2)%R.

Theorem errorBoundedModulo_aux :
 forall (x y : float) (n : Z),
 Fbounded b x ->
 Fcanonic radix b x ->
 Fbounded b y ->
 Fcanonic radix b y ->
 FtoRradix y <> 0%R ->
 (0 < y)%R ->
 (0 <= x)%R ->
 (forall z : Z, (Rabs (n - x / y) <= Rabs (z - x / y))%R) ->
 Fbounded b (Fminus radix x (Fmult (Float n 0) y)).

Theorem errorBoundedModulo_aux_y :
 forall (x y : float) (n : Z),
 Fbounded b x ->
 Fcanonic radix b x ->
 Fbounded b y ->
 Fcanonic radix b y ->
 FtoRradix y <> 0%R ->
 (0 <= x)%R ->
 (forall z : Z, (Rabs (n - x / y) <= Rabs (z - x / y))%R) ->
 Fbounded b (Fminus radix x (Fmult (Float n 0) y)).

Theorem errorBoundedModuloCan :
 forall (x y : float) (n : Z),
 Fbounded b x ->
 Fcanonic radix b x ->
 Fbounded b y ->
 Fcanonic radix b y ->
 FtoRradix y <> 0%R ->
 (forall z : Z, (Rabs (n - x / y) <= Rabs (z - x / y))%R) ->
 Fbounded b (Fminus radix x (Fmult (Float n 0) y)).

Theorem errorBoundedRem :
 forall (x y : float) (n : Z),
  (Fbounded b x) -> (Fbounded b y) ->
   y <> 0 :>R ->
  (forall z : Z, (Rabs (n - x / y) <= Rabs (z - x / y))%R) ->
    (Fbounded b
      (Fminus radix (Fnormalize radix b precision x)
      (Fmult (Float n 0) (Fnormalize radix b precision y)))).

Theorem errorBoundedDiv :
 forall (x y q : float) P,
  (RoundedModeP b radix P) ->
  (Fbounded b x) -> (Fbounded b y) -> (Fbounded b q) ->
  y <> 0 :>R -> (P (x / y)%R q) ->
  (- dExp b <= Fexp q + Fexp y)%Z ->
  (Rabs q) <> (powerRZ radix (- dExp b)) \/
   ((powerRZ radix (- dExp b)) / 2%nat <= Rabs (x / y))%R ->
      (Fbounded b (Fminus radix x (Fmult q y))).

Theorem errorBoundedDivSimplHyp :
 forall (x y q : float) P,
 RoundedModeP b radix P ->
 Fbounded b x ->
 Fbounded b y ->
 Fbounded b q ->
 FtoRradix y <> 0%R ->
 P (x / y)%R q ->
 (- dExp b <= Fexp (Fnormalize radix b precision x) - precision)%Z ->
 (- dExp b <= Fexp q + Fexp y)%Z.

Theorem errorBoundedDivClosest :
 forall x y q : float,
 Fbounded b x ->
 Fbounded b y ->
 Fbounded b q ->
 FtoRradix y <> 0%R ->
 Closest b radix (x / y) q ->
 (- dExp b <= Fexp q + Fexp y)%Z -> Fbounded b (Fminus radix x (Fmult q y)).

Theorem errorBoundedDivToZero :
 forall x y q : float,
 Fbounded b x ->
 Fbounded b y ->
 Fbounded b q ->
 FtoRradix y <> 0%R ->
 ToZeroP b radix (x / y) q ->
 (- dExp b <= Fexp q + Fexp y)%Z -> Fbounded b (Fminus radix x (Fmult q y)).

Theorem errorBoundedSqrt :
 forall x q : float,
 (Fbounded b x) -> (Fbounded b q) ->
 (0 <= x)%R -> (Closest b radix (sqrt x) q) ->
 (- dExp b <= Fexp q + Fexp q)%Z ->
   (Fbounded b (Fminus radix x (Fmult q q))).

End FroundDiv.