Library Stdlib.ZArith.BinIntDef
From Stdlib Require Export BinNums.
From Stdlib Require Import BinPos BinNat.
From Stdlib Require Export BinNums.IntDef.
Local Open Scope Z_scope.
Local Notation "0" :=
Z0.
Local Notation "1" := (
Zpos 1).
Local Notation "2" := (
Zpos 2).
Binary Integers, Definitions of Operations
Initial author: Pierre Crégut, CNET, Lannion, France
Nicer names Z.pos and Z.neg for constructors
Definition zero := 0.
Definition one := 1.
Definition two := 2.
Definition sgn z :=
match z with
| 0 => 0
|
pos p => 1
|
neg p =>
neg 1
end.
Boolean equality and comparisons
Nota:
geb and
gtb are provided for compatibility,
but
leb and
ltb should rather be used instead, since
more results will be available on them.
Definition geb x y :=
match x ?= y with
|
Lt =>
false
|
_ =>
true
end.
Definition gtb x y :=
match x ?= y with
|
Gt =>
true
|
_ =>
false
end.
Infix "=?" :=
eqb (
at level 70,
no associativity) :
Z_scope.
Infix "<=?" :=
leb (
at level 70,
no associativity) :
Z_scope.
Infix "<?" :=
ltb (
at level 70,
no associativity) :
Z_scope.
Infix ">=?" :=
geb (
at level 70,
no associativity) :
Z_scope.
Infix ">?" :=
gtb (
at level 70,
no associativity) :
Z_scope.
Conversions
From
Z to
nat via absolute value
From Z to N via absolute value
From Z to N by rounding negative numbers to 0
Conversion with a decimal representation for printing/parsing
Iteration of a function
By convention, iterating a negative number of times is identity.
Division by two
quot2 performs rounding toward zero, it is hence a particular
case of
quot, and for all relative number
n we have:
n = 2 * quot2 n + if odd n then sgn n else 0.
NB:
Z.quot2 used to be named
Z.div2 in Coq <= 8.3
Base-2 logarithm
A generalized gcd, also computing division of a and b by gcd.
Definition ggcd a b :
Z*(Z*Z) :=
match a,
b with
| 0,
_ =>
(abs b,(0
, sgn b))
|
_, 0 =>
(abs a,(sgn a, 0
))
|
pos a,
pos b =>
let '
(g,(aa,bb)) :=
Pos.ggcd a b in (pos g, (pos aa, pos bb))
|
pos a,
neg b =>
let '
(g,(aa,bb)) :=
Pos.ggcd a b in (pos g, (pos aa, neg bb))
|
neg a,
pos b =>
let '
(g,(aa,bb)) :=
Pos.ggcd a b in (pos g, (neg aa, pos bb))
|
neg a,
neg b =>
let '
(g,(aa,bb)) :=
Pos.ggcd a b in (pos g, (neg aa, neg bb))
end.
Bitwise functions
When accessing the bits of negative numbers, all functions
below will use the two's complement representation. For instance,
-1 will correspond to an infinite stream of true bits. If this
isn't what you're looking for, you can use
abs first and then
access the bits of the absolute value.
testbit : accessing the
n-th bit of a number
a.
For negative
n, we arbitrarily answer
false.
Bitwise operations lor land ldiff lxor
Re-export the notation for those who just Import BinIntDef