4.27.2.1 Arithmetic types
SWI-Prolog defines the following numeric types:
- integer
If SWI-Prolog is built using the GNU multiple precision arithmetic library (GMP), integer arithmetic is unbounded, which means that the size of integers is limited by available memory only. Without GMP, SWI-Prolog integers are 64-bits, regardless of the native integer size of the platform. The type of integer support can be detected using the Prolog flags bounded, min_integer and max_integer. As the use of GMP is default, most of the following descriptions assume unbounded integer arithmetic.Internally, SWI-Prolog has three integer representations. Small integers (defined by the Prolog flag max_tagged_integer) are encoded directly. Larger integers are represented as 64-bit values on the global stack. Integers that do not fit in 64 bits are represented as serialised GNU MPZ structures on the global stack.
- rational number
Rational numbers (Q) are quotients of two integers (N/M). Rational arithmetic is only provided if GMP is used (see above). Rational numbers satisfy the type tests rational/1, number/1 and atomic/1 and may satisfy the type test integer/1, i.e., integers are considered rational numbers. Rational numbers are always kept in canonical representation, which means M is positive and N and M have no common divisors. Rational numbers are introduced into the computation using the functions rational/1, rationalize/1 or the rdiv/2 (rational division) function. If the Prolog flag prefer_rationals istrue
(default), division (//2) and integer power (^/2) also produce a rational number. - float
Floating point numbers are represented using the C typedouble
. On most of today's platforms these are 64-bit IEEE floating point numbers.
Arithmetic functions that require integer arguments accept, in addition to integers, rational numbers with (canonical) denominator‘1'. If the required argument is a float the argument is converted to float. Note that conversion of integers to floating point numbers may raise an overflow exception. In all other cases, arguments are converted to the same type using the order below.
integer -> rational number -> floating point number