Fixpoint Zdigits_aux (nb pow : Z) (n : nat) { struct n } : Z :=
  match n with
  | O => nb
  | S n => if Zlt_bool p pow then nb else Zdigits_aux (nb + 1) (Zmult beta pow) n
  end.(** Number of digits of an integer *)

Definition Zdigits n :=
  match n with
  | Z0 => Z0
  | Zneg p => Zdigits_aux (Zpos p) 1 beta (digits2_Pnat p)
  | Zpos p => Zdigits_aux n 1 beta (digits2_Pnat p)
  end.

(* Specifications: *)
Theorem Zdigits_correct : forall n, (Zpower beta (Zdigits n - 1) <= Zabs n < Zpower beta (Zdigits n))%Z.
Theorem Zdigits_unique : forall n d, (Zpower beta (d - 1) <= Zabs n < Zpower beta d)%Z -> Zdigits n = d.
Theorem Zdigits_abs : forall n, Zdigits (Zabs n) = Zdigits n.
Theorem Zdigits_gt_0 : forall n, n <> Z0 -> (0 < Zdigits n)%Z.
Theorem Zdigits_ge_0 : forall n, (0 <= Zdigits n)%Z.
Theorem Zdigit_out : forall n k, (Zdigits n <= k)%Z -> Zdigit n k = Z0.
Theorem Zdigit_digits : forall n, n <> Z0 -> Zdigit n (Zdigits n - 1) <> Z0.
Theorem Zdigits_slice : forall n k l, (0 <= l)%Z -> (Zdigits (Zslice n k l) <= l)%Z.
Theorem Zdigits_mult_Zpower : forall m e, m <> Z0 -> (0 <= e)%Z -> Zdigits (m * Zpower beta e) = (Zdigits m + e)%Z.
Theorem Zdigits_Zpower : forall e, (0 <= e)%Z -> Zdigits (Zpower beta e) = (e + 1)%Z.
Theorem Zdigits_le : forall x y, (0 <= x)%Z -> (x <= y)%Z -> (Zdigits x <= Zdigits y)%Z.
Theorem lt_Zdigits : forall x y, (0 <= y)%Z -> (Zdigits x < Zdigits y)%Z -> (x < y)%Z.
Theorem Zpower_le_Zdigits : forall e x, (e < Zdigits x)%Z -> (Zpower beta e <= Zabs x)%Z.
Theorem Zdigits_le_Zpower : forall e x, (Zabs x < Zpower beta e)%Z -> (Zdigits x <= e)%Z.
Theorem Zpower_gt_Zdigits : forall e x, (Zdigits x <= e)%Z -> (Zabs x < Zpower beta e)%Z.
Theorem Zdigits_gt_Zpower : forall e x, (Zpower beta e <= Zabs x)%Z -> (e < Zdigits x)%Z.
Theorem Zdigits_mult_strong : forall x y, (0 <= x)%Z -> (0 <= y)%Z -> (Zdigits (x + y + x * y) <= Zdigits x + Zdigits y)%Z.
Theorem Zdigits_mult : forall x y, (Zdigits (x * y) <= Zdigits x + Zdigits y)%Z.
Theorem Zdigits_mult_ge : forall x y, (x <> 0)%Z -> (y <> 0)%Z -> (Zdigits x + Zdigits y - 1 <= Zdigits (x * y))%Z.
Theorem Zdigits_div_Zpower : forall m e, (0 <= m)%Z -> (0 <= e <= Zdigits m)%Z -> Zdigits (m / Zpower beta e) = (Zdigits m - e)%Z.
Theorem Z_of_nat_S_digits2_Pnat : forall m : positive, Z_of_nat (S (digits2_Pnat m)) = Zdigits radix2 (Zpos m).