Contents
!mpz data type
The !mpz data type can store integer numbers of arbitrary size, up to the maximum memory allowed by PostgreSQL data types (about 1GB).
Every PostgreSQL integer type (!int16, !int32, !int64) can be converted to !mpz. Not integer types (!float4, !float8, !numeric) are truncated. Cast from integer types are automatic, whereas non integer require explicit cast (but are implicitly converted in assignment).
Casting !mpz to PostgreSQL integer types and !numeric works as expected and will overflow if the value is too big for their range. Casting to !float4 and !float8 works as well: in case of overflow the value will be Infinity.
!mpz values can be compared using the regular PostgreSQL comparison operators. Indexes on !mpz columns can be created using the btree or the hash method.
!mpz textual input/output
Arithmetic Operators and Functions
These operators can either work on !mpz arguments or take an integer argument that will be implicitly converted. Operators taking a argument always use an integer as right argument.
Note
GMP defines many structures in terms of !long or !unsigned long, whose definitions may vary across platforms. PostgreSQL instead offers data types with a defined number of bytes (e.g. !int4, !int8). For this reason, functions taking an integer as argument are defined as !int8, but they may actually fail if the server is 32 bit and the argument doesn't fit into an !int4.
| Operator | Description | Example | Return |
|---|---|---|---|
| !- | Unary minus | !- 5::mpz | -5 |
| !+ | Unary plus | !+ 5::mpz | 5 |
| !+ | Addition | !2::mpz + 3::mpz | 5 |
| !- | Subtraction | !2::mpz - 3::mpz | -1 |
| !* | Multiplication | !7::mpz * 3::mpz | 21 |
| !<< | Multiplication by | !3::mpz << 2 | 12 |
| !^ | Power (1) | !3::mpz ^ 2 | 9 |
Notes:
- See also the exponentiation functions.
Division Operators and Functions
For all the division-related operators , and will satisfy , and will satisfy
System Message: ERROR/3 (/home/pgxn/www/src/pgmp/pgmp-1.0.0b3/docs/mpz.rst, line 127)
Unknown LaTeX command: lt
0 \le |r| \lt |d|
Note
Only the truncating division and reminder (!/ and !%) have the correct precedence respect to addition, subtraction and multiplication. See the PostgreSQL precedence table for further details.
| Operator | Description | Example | Return |
|---|---|---|---|
| !/ | Division quotient Rounding towards zero |
! 7::mpz / 3::mpz !-7::mpz / 3::mpz |
2 -2 |
| !% | Division reminder Rounding towards zero |
! 7::mpz % 3::mpz !-7::mpz % 3::mpz |
1 -1 |
| +/ | Division quotient Rounding towards +infinity |
! 7::mpz +/ 3::mpz !-7::mpz +/ 3::mpz |
3 -2 |
| +% | Division reminder Rounding towards +infinity |
! 7::mpz +% 3::mpz !-7::mpz +% 3::mpz |
-2 -1 |
| !-/ | Division quotient Rounding towards -infinity |
! 7::mpz -/ 3::mpz !-7::mpz -/ 3::mpz |
2 -3 |
| !-% | Division reminder Rounding towards -infinity |
! 7::mpz -% 3::mpz !-7::mpz -% 3::mpz |
1 2 |
| /? | Divisible (1) | !21::mpz /? 7::mpz | !true |
| /! | Exact division (2) | !21::mpz /! 7::mpz | 3 |
Notes:
- See also the function divisible().
- The exact division operator (!/!) produces correct results only when it is known in advance that divides . The operator is much faster than the other division operators, and is the best choice when exact division is known to occur, for example reducing a rational to lowest terms.
| Operator | Description | Example | Return |
|---|---|---|---|
| !>> | Quotient of division by Rounding towards zero |
! 1027::mpz >> 3 !-1027::mpz >> 3 |
128 -128 |
| !%> | Remainder of division by Rounding towards zero |
! 1027::mpz %> 3 !-1027::mpz %> 3 |
3 -3 |
| !+>> | Quotient of division by Rounding towards +infinity |
! 1027::mpz +>> 3 !-1027::mpz +>> 3 |
129 -128 |
| !+%> | Remainder of division by Rounding towards +infinity |
! 1027::mpz +%> 3 !-1027::mpz +%> 3 |
-5 -3 |
| !->> | Quotient of division by Rounding towards -infinity |
! 1027::mpz ->> 3 !-1027::mpz ->> 3 |
128 -129 |
| !-%> | Remainder of division by Rounding towards -infinity |
! 1027::mpz -%> 3 !-1027::mpz -%> 3 |
3 5 |
| >>? | Divisible by (1) | !64::mpz >>? 3 | !true |
- See also the function divisible_2exp().
Exponentiation Functions
Root Extraction Functions
Number Theoretic Functions
Logical and Bit Manipulation Functions
These functions behave as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). The least significant bit is number 0.
| Operator | Description | Example | Return |
|---|---|---|---|
| !& | Bitwise and | !'0b10001'::mpz & '0b01001'::mpz | !'0b1'::mpz |
| !| | Bitwise inclusive-or | !'0b10001'::mpz | '0b01001'::mpz | !'0b11001'::mpz |
| !# | Bitwise exclusive-or | !'0b10001'::mpz # '0b01001'::mpz | !'0b11000'::mpz |
Random number functions
Sequences of pseudo-random numbers are generated using an internal per-session variable, which holds an algorithm selection and a current state. Such a variable must be initialized by a call to one of the !randinit*() functions, and can be seeded with the randseed() function.