(** First, a division for positive numbers. Even if the second
argument is a Z, the answer is arbitrary if it isn't a Zpos. *)
Fixpoint pos_div_eucl (a:positive) (b:Z) : Z * Z := match a with
| xH => if leb 2 b then (0, 1) else (1, 0)
| xO a' => let (q, r) := pos_div_eucl a' b in let r' := 2 * r in if ltb r' b then (2 * q, r') else (2 * q + 1, r' - b)
| xI a' => let (q, r) := pos_div_eucl a' b in let r' := 2 * r + 1 in if ltb r' b then (2 * q, r') else (2 * q + 1, r' - b) end.
(** Then the general euclidean division *)
Definition div_eucl (a b:Z) : Z * Z := match a, b with
| 0, _ => (0, 0)
| _, 0 => (0, a)
| Zpos a', Zpos _ => pos_div_eucl a' b
| Zneg a', Zpos _ => let (q, r) := pos_div_eucl a' b in match r with
| 0 => (- q, 0)
| _ => (- (q + 1), b - r) end
| Zneg a', Zneg b' => let (q, r) := pos_div_eucl a' (Zpos b') in (q, - r)
| Zpos a', Zneg b' => let (q, r) := pos_div_eucl a' (Zpos b') in match r with
| 0 => (- q, 0)
| _ => (- (q + 1), b + r) end end.
Definition div (a b:Z) : Z := let (q, _) := div_eucl a b in q. Definition modulo (a b:Z) : Z := let (_, r) := div_eucl a b in r.
Definition quotrem (a b:Z) : Z * Z := match a, b with
| 0, _ => (0, 0)
| _, 0 => (0, a)
| Zpos a, Zpos b => let (q, r) := N.pos_div_eucl a (Npos b) in (of_N q, of_N r)
| Zneg a, Zpos b => let (q, r) := N.pos_div_eucl a (Npos b) in (-of_N q, - of_N r)
| Zpos a, Zneg b => let (q, r) := N.pos_div_eucl a (Npos b) in (-of_N q, of_N r)
| Zneg a, Zneg b => let (q, r) := N.pos_div_eucl a (Npos b) in (of_N q, - of_N r) end.
Definition quot a b := fst (quotrem a b). Definition rem a b := snd (quotrem a b).
Infix"÷" := quot (at level 40, left associativity) : Z_scope. (** No infix notation for rem, otherwise it becomes a keyword *)
(** ** Parity functions *)
Definition even z := match z with
| 0 => true
| Zpos (xO _) => true
| Zneg (xO _) => true
| _ => false end.
(** ** Division by two *)
(** [div2] performs rounding toward bottom, it is hence a particular caseof[div],andforallrelativenumber[n]wehave:
[n = 2 * div2 n + if odd n then 1 else 0]. *)
Definition div2 z := match z with
| 0 => 0
| Zpos 1 => 0
| Zpos p => Zpos (Pos.div2 p)
| Zneg p => Zneg (Pos.div2_up p) end.
(** ** Square root *)
Definition sqrtrem n := match n with
| 0 => (0, 0)
| Zpos p => match Pos.sqrtrem p with
| (s, Pos.IsPos r) => (Zpos s, Zpos r)
| (s, _) => (Zpos s, 0) end
| Zneg _ => (0,0) end.
Definition lor a b := match a, b with
| 0, _ => b
| _, 0 => a
| Zpos a, Zpos b => Zpos (Pos.lor a b)
| Zneg a, Zpos b => Zneg (N.succ_pos (N.ldiff (Pos.pred_N a) (Npos b)))
| Zpos a, Zneg b => Zneg (N.succ_pos (N.ldiff (Pos.pred_N b) (Npos a)))
| Zneg a, Zneg b => Zneg (N.succ_pos (N.land (Pos.pred_N a) (Pos.pred_N b))) end.
Definition land a b := match a, b with
| 0, _ => 0
| _, 0 => 0
| Zpos a, Zpos b => of_N (Pos.land a b)
| Zneg a, Zpos b => of_N (N.ldiff (Npos b) (Pos.pred_N a))
| Zpos a, Zneg b => of_N (N.ldiff (Npos a) (Pos.pred_N b))
| Zneg a, Zneg b => Zneg (N.succ_pos (N.lor (Pos.pred_N a) (Pos.pred_N b))) end.
Definition lxor a b := match a, b with
| 0, _ => b
| _, 0 => a
| Zpos a, Zpos b => of_N (Pos.lxor a b)
| Zneg a, Zpos b => Zneg (N.succ_pos (N.lxor (Pos.pred_N a) (Npos b)))
| Zpos a, Zneg b => Zneg (N.succ_pos (N.lxor (Npos a) (Pos.pred_N b)))
| Zneg a, Zneg b => of_N (N.lxor (Pos.pred_N a) (Pos.pred_N b)) end.
End Z.
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.